C++ Array Arguments
Passing Array as Arguments to a Function:
An array variable can also he passed to the function as an argument. When an array is passed to the function only the starting address of the array is passed to the function. The C++ does not make a separate copy of the array in the body of the function. It only assigns the starting address of the same memory area of the array to the array name used in the decelerator of the function.
The function can change the contents of the array by directly accessing the memory cells where the array’s elements are stored. Thus, although the starting address of the array is passed the elements of the array can be changed as if they have been passed to the function.
Declaration of a Function with Array Arguments:
When an array variable is used as an argument of a function, it is mentioned in the function declaration.
For example, if a function is to use two arrays as arguments, one of float type one-dimensional array and the other of integer type one-dimensional array, it is declared as:
If two-dimensional arrays are to be used as arguments, two square brackets are used as shown below:
void temp (float [ ] [ ],
Function Definition with Array Arguments:
The name of the array and its type is given in decelerator of the function definition. The type of the array in decelerator must correspond with its type given in the prototype of the function.
For example, the function definition with array arguments is written
void max ( float xy [ ], int ab [ ] )
{
body of the function
}
The size of the array can he given both in the function declaration and in the function definition but its use is optional.
Calling Function with Array Arguments:
When a function that uses an array as argument is called, then only the name of the array without subscripts is given. This is because only the memory address of the array is passed to the function. The data is accessed from the same memory location.
The name of the array used in the function definition and in the function call may he different or same. Because, it is the name of the array in the function definition to which the address of the array is passed.