C++ Learning Resource Network!

 
 
 C++ Tutorials  >  Functions  >  Passing Arrays to Functions
 

Arrays are inherently passed to functions by the call by address method. For instance, if an array num_array of size ten is to be passed to a function called stringfunc(), then it would be passed in the following way:

stringfunc(num_array);

Recall that num_array is actually the address of the first element of the array, num_array[0].

The parameter of the called function, say number_list, can be declared in any of the following ways:

stringfunc(int number_list[]) //No subscript
{

}


OR

stringfunc(int number_list[10]) //Same size as num_array
{

}


OR

stringfunc(int *number_list) //int type pointer
{

}


While passing arrays to functions, the address of the array is passed from the calling function. Since the address of the array is always passed to a function, it is easy to alter the contents of the array directly in the called function. Therefore, arrays do not have to be returned from a function explicitly using the return statement.

 
 

| Bookmark This Site |   | Links |   | Search |   | Submit |   | Contact |

  Copyright 2008 - 3008  CppTutor.com All rights reserved.