Passing Arrays to Function in C – User Defined Functions

Like the values of simple variables, it is also possible to pass the values of an array to a function. To passing arrays to function in C a one dimensional an 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. for example, the call largest(a,n) will pass the whole array a to the called function. The called function expection this call must be appropriately defined. The largest 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. The declaration of the formal argument array is made as follows: float array[]; The pair of brackets informs the compiler that the argument array is an array of numbers. It is not necessary to specify the size of the array here.

Let us consider a problem of finding the largest value in an array of elements. Passing arrays to function in C The program is as follows:

main()
{
      float largest(float a[], int n);
      float value[4] = {2.5,-4.75,1.2,3.67};
      print(%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);
}

One Dimensional Arrays

When the function call largest(value,4) is made, the values of all elements of array value become the corresponding elements of array a in the called function. The largest function finds the largest value in the array abd return result to the main.

In C, the name of the array represents the address of its first element. By passing the array name, we are, in fact, passing the addres of the array to the called function. The array in the called function now refers to the same array stored in the memory. Therefore, any changes in the array in the called function will be reflected in the original array.

Passing address of parameters to the functions is referred to as pass by address (or pass by pointers). Note that we cannot pass a whole array by value as we did in the case of ordinary variables.

A multifunction program consisting of main, std_dev, and mean functions is shown in program. main reads the elements of the array value from the terminal and calls the function std_dev to print the standard deviation of the elements. Std_dev in turn, calls another function mean to supply the average value of the array elements.

Both std_dev and mean are defined as floats and therefore they are declared floats in the global section of the program.

Read More Topics
Passing pointers to functions in C++
Arrays structures and unions in C
Static data member in C++
Pointers to pointer in C++

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply