How to Pass Arrays to Functions?


One Dimensional Arrays:

Like the values of simple variables, it is also possible to pass the values of an array to a function.

To pass a one dimension array to a called function, it is sufficient to list the name of the array, without any subscripts, and the size of the array as arguments.

Ex: largest (a, n);

Will pass the whole away a to the called function. The larger function header might look like.

float largest ( float array [ ], int size);

The function largest is defined to take two arguments, the array name and the size of the array to specify the number of elements in the array.

float array [ ];

The pair of brackets informs the compiler that the argument array is an array of numbers. If It is not necessary to specify the size of the array here.

Ex: Find the largest value in an array

main ( )
{
float largest ( float a [ ], int n);
float value [4] = {2,5,–4.75,1.2,3,6,7};
printf (“%f \n”, largest ( value , 4));
}

float largest ( float a [ ], int n)
{
int i;
float max,
max = a [0];
for ( i =1; i < n; i++)
if ( max < a[i])
max =a[i];
return  ( max);
}

The largest function finds the largest value in the array and returns the result to the main.

Rules to pass an array to a function:

1. The function must be called by passing only the name of the array.

2. In the function definition the formal parameter must be an array type, the size of the array does not need to be specified.

3. The function prototype must show that the argument is an array.

Two Dimensional Arrays:

Like simple arrays, we can also pass multi dimensional arrays to functions. The rules are

1. The function must be called by passing only the array name.

2. In the function definition, we must indicate that the array has two dimensions. By including two sets of brackets.

3. The size of the 2nd dimension must be specified.

4. The prototype declaration should be similar to the function header.

Ex: Calculate the average of the values in a two dimensional matrix.

main()
{
int M=3, N =2;
double avg ( int [ ] [N], int, int ) ;
double mean ;
int matrix [M] [N] = { {1,2} {3,4},{5,6}},
mean = avg ( matrix M, N);
}

double avg ( int x [ ] [N], int m, int n)
{
int i,j;
double sum = 0.0;
for ( i =0; i < M, i++)
for ( j =0; j < N; j++)
sum + = x[i][j];
return ( sum / (m *n));
}

Passing Strings to Functions:

The strings are treated as characters arrays in C and The rules for passing strings to functions are very similar to those for passing arrays to functions.

Basic Rules are : 

1. The string to be passed must be declared as a formal argument of the function when it is defined.

Ex: 

void display ( char item_name [ ] )
{……………
…………….
}

2. The function prototype must show that the argument is a string .For the above function definition, the prototype can be written as

void display ( char str [ ] );

3. A call to the function must have a string array name without subscripts as its actual argument.

Ex: display ( name);

Where name is a properly declared strings array in the calling function.

Note: arrays and strings in C cannot be passed by value to functions.


Related

C Language 1227858836737434829

Post a Comment

emo-but-icon

item