passing array to function in C programming Language

  1.  Fig. Shows Passing array to function as reference or base address.
The fig shows how array is declared and passed to the function.
The array variable salary has declared as integer and initialized to 5 elements.
Next the statement total(salary,n) is the calling function which in turn calls the function total() declared and defined above.
The base address of salary array and size 5 is passed to the function total().
Then the function void total(int a[],int size)
{
.....
.....
}
executes.
The body of function total() is to be developed as per the requirement.
Note:Entire array can not be passed at a time to the function.Reference or base address is suficient in array to pass all array elements in the function.

1. C program to pass array to Function as refernce.

The following example given is a simple implementation of passing as reference array to function.

#include <stdio.h>
void total(int a[],int size)
{
  int i=0,tot=0;
  printf("\n Array Elements");
  for(i=0;i<size;i++)
    {
     printf("\n %d",a[i]);
     tot=tot+a[i];
   }
  printf("\n Sum of array elements=%d",tot);
}
int main()
{
 int n=5;
 int salary[]={4000,4500,2300,4623,1540};
 total(salary,n);
 return(0);
}
Output:
Array Elements:
4000
4500
2300
4623
1540
Sum of array elements=16693.

Explanation:In the given program above we have declared and defined the function total(int a[],int size) .which accepts entire array and size as argument.The Function displays the array elements,calculates the total of array elements and finally displays the sum of array elements.
1.  int n=5;
  n is the integer variable which is assigned a value 5 and has been used as the total size of an array.  int salary[]={4000,4500,2300,4623,1540};
The salary has been declared as int array variable. The five different unique salaries has been assigned to salary array.
2. total(salary,n);
 the statement above given calls the total() function.
The parameters salary(as reference) and n as size are passed to the function.
The function total() is executes and calculates the result.
void total(int a[],int size)
{
  int i=0,tot=0;
  printf("\n Array Elements");
  for(i=0;i<size;i++)
    {
     printf("\n %d",a[i]);
     tot=tot+a[i];
   }
  printf("\n Sum of array elements=%d",tot);
}
The function total() now have all the array element and the size.
Next the the printf("\n Array Elements"); displays the "Array Elements" on the console.
  for(i=0;i<size;i++)
    {
     printf("\n %d",a[i]);
     tot=tot+a[i];
   }
The loop given above displays all array element on console and sums those array elements and store them in tot variable.
and finally the "printf("\n Sum of array elements=%d",tot);"
function shows the output "Sum of array elements=16693"


Passing array to function as call by value method.

Passing the actual value of variable into function is known as "Passing by value" .
The compiler copies the value of an actual argument in a local variable When we use pass by value.
The parameters in the called or local function are initialized with the value of the passed arguments.

2. C program to pass array to Function as value.

In the following example, the main () function passes individual values of array to the show(). The function show() receives copies of these variables values and accesses them by the identifier p. The function show() modifies the value of p. When control passes back to main(), the actual values of a[] are not changed.

/*
This example illustrates calling a function by array values
*/
#include <stdio.h>
void show (int p)
{
  p += 5;
 printf("In add function, p = %d \n", p);
}
int main(void)
{
 int a[5] ={12,34,54,21,43},i;
 for(i=0;i<5;i++)
 {
    show(a[i]);
   printf("In main function, a = %d ", a[i]);
 }
 return 0;
}

Previous Topic:-->> Recursion Functions in C || Next topic:-->>Scope,visibility and Life time of variable.