2. • When passing an array to a function, we need to tell the
compiler what the type of the array is and give it a
variable name, similar to an array declaration
float a[] parameter
• We need to specify the name of the array without any
brackets
int myArray[24];
myFunction(myArray,24);
-Arrays passed call-by-reference
-Name of array is address of first element
-Function knows where the array is stored(Modifies
original memory locations)
3. ARRAYS ARE ALWAYS PASSED BY REFERENCE
• Arrays are automatically passed by reference. Do not use &.
• If the function modifies the array, it is also modified in the
calling environment
void Zero(int arr[], int N)
{
for (int k=0; k<N; k++)
arr[k]=0;
Function prototype:
void modifyArray( int b[], int arraySize );
-Parameter names optional in prototype
int b[] could be written int []
int arraySize could be simply int
4. /*Example Number 1*/
#include <iostream.h>
int Display(intint data[],int N)
{ int k;
cout<<“Array contains”<<endl;
for (k=0; k<N; k++)
cout<<data[k]<<“ “;
cout<<endl;
}
int main()
{
int a[4] = { 11, 33, 55, 77 };
Display(a, 4);
}
An int array parameter
of unknown size
Size of the array
The array argument
5. /*Example Number 2*/
#include <iostream.h>
int sum(int data[], int n);
void main()
{
int a[] = { 11, 33, 55, 77 };
int size = sizeof(a)/sizeof(int);
cout << "sum(a,size) = " << sum(a,size) << endl;
}
int sum(int data[], int n)
{
int sum=0;
for (int i=0; i<n;i++)
sum += data[i];
return sum;
}
The array argument
An int parameter of
unkown size
Size of the array