When you pass an array as an argument to a function what is actually passed?

Just like variables, array can also be passed to a function as an argument . In this guide, we will learn how to pass the array to a function using call by value and call by reference methods.

To understand this guide, you should have the knowledge of following C Programming topics:

  1. C – Array
  2. Function call by value in C
  3. Function call by reference in C

Passing array to function using call by value method

As we already know in this type of function call, the actual parameter is copied to the formal parameters.

#include <stdio.h>
void disp( char ch)
{
   printf("%c ", ch);
}
int main()
{
   char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
   for (int x=0; x<10; x++)
   {
       /* I’m passing each element one by one using subscript*/
       disp (arr[x]);
   }

   return 0;
}

Output:

a b c d e f g h i j

Passing array to function using call by reference

When we pass the address of an array while calling a function then this is called function call by reference. When we pass an address as an argument, the function declaration should have a pointer as a parameter to receive the passed address.

#include <stdio.h>
void disp( int *num)
{
    printf("%d ", *num);
}

int main()
{
     int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
     for (int i=0; i<10; i++)
     {
         /* Passing addresses of array elements*/
         disp (&arr[i]);
     }

     return 0;
}

Output:

1 2 3 4 5 6 7 8 9 0

How to pass an entire array to a function as an argument?

In the above example, we have passed the address of each array element one by one using a for loop in C. However you can also pass an entire array to a function like this:

Note: The array name itself is the address of first element of that array. For example if array name is arr then you can say that arr is equivalent to the &arr[0].

If a function only looks at the contents of an array, and does not change what is in the array, you usually indicates that by adding const to the parameter. For example, the following function firstPositive does not change what is in array A.
  // firstPositive(A,n) returns the first of
  // A[0], ..., A[n-1] that is positive.  If
  // none of them is positive, then it returns 0.

  int firstPositive(const int* A, int n)
  {
    for(int i = 0; i < n; i++)
    {
      if(A[i] > 0)
      {
        return A[i];
      }
    }
    return 0;
  }

If you want to pass a single-dimension array as an argument in a function, you would have to declare a formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received. Similarly, you can pass multi-dimensional arrays as formal parameters.

Way-1

Formal parameters as a pointer −

void myFunction(int *param) {
   .
   .
   .
}

Way-2

Formal parameters as a sized array −

void myFunction(int param[10]) {
   .
   .
   .
}

Way-3

Formal parameters as an unsized array −

void myFunction(int param[]) {
   .
   .
   .
}

Example

Now, consider the following function, which takes an array as an argument along with another argument and based on the passed arguments, it returns the average of the numbers passed through the array as follows −

double getAverage(int arr[], int size) {

   int i;
   double avg;
   double sum = 0;

   for (i = 0; i < size; ++i) {
      sum += arr[i];
   }

   avg = sum / size;

   return avg;
}

Now, let us call the above function as follows −

#include 
 
/* function declaration */
double getAverage(int arr[], int size);

int main () {

   /* an int array with 5 elements */
   int balance[5] = {1000, 2, 3, 17, 50};
   double avg;

   /* pass pointer to the array as an argument */
   avg = getAverage( balance, 5 ) ;
 
   /* output the returned value */
   printf( "Average value is: %f ", avg );
    
   return 0;
}

When the above code is compiled together and executed, it produces the following result −

Average value is: 214.400000

As you can see, the length of the array doesn't matter as far as the function is concerned because C performs no bounds checking for formal parameters.

technically, neither. In C function arguments are always passed by value. In case of an array (variable), while passed as a function argument, it decays to the pointer to the first element of the array. The pointer is then passed-by-value, as usual.

However, just as any other pointer type argument, if, from the called function you alter any value pointed to by the pointer (or, a derived pointer via pointer arithmetic, as long as you stay within the valid range), the actual array element values inside the caller function is also affected.

When you pass an array name as an argument to a function what is actually being passed C++?

When you pass an array name as an argument to a function, what is actually being passed? The array's beginning memory address. How so you establish a parallel relationship between two or more arrays? By using the "Same Subscript Value" for each array.

When you pass an array as an argument to a function what will actually be passed quizlet?

Terms in this set (10) Arrays as function arguments concept: To pass an array as an argument to a function, pass the name of the array.

When an array is passed to a function what is actually passed Java?

To pass an array to a function, just pass the array as function's parameter (as normal variables), and when we pass an array to a function as an argument, in actual the address of the array in the memory is passed, which is the reference.