Passing arrays and individual array elements to functions. - C++ Function

C++ examples for Function:Function Parameter

Description

Passing arrays and individual array elements to functions.

Demo Code

#include <iostream> 
#include <iomanip> 
using namespace std; 

void modifyArray( int [], int ); // appears strange; array and size 
void modifyElement( int ); // receive array element value 

int main() // ww w .j  a v a  2s  .c  o  m
{ 
   const int arraySize = 5; // size of array a 
   int a[ arraySize ] = { 0, 1, 2, 3, 4 }; // initialize array a 

   cout << "\nThe values of the original array are:\n"; 

   // output original array elements 
   for ( int i = 0; i < arraySize; ++i ) 
       cout << setw( 3 ) << a[ i ]; 

   cout << endl; 

   // pass array a to modifyArray by reference 
   modifyArray( a, arraySize ); 

   cout << "The values of the modified array are:\n"; 

   for ( int j = 0; j < arraySize; ++j ) 
       cout << setw( 3 ) << a[ j ]; 

   cout << "\n\nEffects of passing array element by value:" << "\na[3] before modifyElement: " << a[ 3 ] << endl; 

   modifyElement( a[ 3 ] ); // pass array element a[ 3 ] by value 
   cout << "a[3] after modifyElement: " << a[ 3 ] << endl; 
}

//"b" points to the original array "a" in memory 
void modifyArray( int b[], int sizeOfArray ) { 
   // multiply each array element by 2 
   for ( int k = 0; k < sizeOfArray ; ++k ) 
       b[ k ] *= 2; 
}

// "e" is a local copy of array element a[ 3 ] passed from main 
void modifyElement( int e ) 
{ 
   // multiply parameter by 2 
   cout << "Value of element in modifyElement: " << ( e *= 2 ) << endl; 
}

Result


Related Tutorials