How to pass variables and arrays into function in C ?

Generally there are two ways of passing values in a function:
1. Call by Value
2. Call by Reference

Call by Value:
In call by value a copy of actual arguments is passed to respective formal arguments. So the changes made in formal arguments are not reflected to the actual arguments.
Example:

void function(int a)
{
a=a+2;
printf("\nFrom function value=%d",a);
}
int main()
{
int x=5;
printf("\nFrom main before function=%d",x);
function(x);
printf("\nFrom main after function=%d",x);
return 0;
}

Output:

From main before function=5
From function value=7
From main after function=5

But this call by value doesnot work with arrays, look at the example below:

void function(int a[5])                         // void function(int a[], int size)
{
int i;
for(i=0;i<5;i++)                                    //for(i=0;i< size;i++)              
{
a[i]=a[i]+2;
}
printf("\nFrom function value:");
for(i=0;i<5;i++)                                   //for(i=0;i< size;i++)
{
printf("\na[i]=%d",a[i]);
}
}
int main()
{
int j, x[5]={1,2,3,4,5};                 
printf("\nFrom main before function:");
for(j=0;j<5;j++)
{
printf("\nx[j]=%d",x[j]);
}
function(x);                                      //function(x,5);
printf("\nFrom main after function:");
for(j=0;j<5;j++)
{
printf("\nx[j]=%d",x[j]);
}
return 0;
}

Output:

From main before function:
x[0]=1
x[1]=2
x[2]=3
x[3]=4
x[4]=5
From function value:
a[0]=3
a[1]=4
a[2]=5
a[3]=6
a[4]=7
From main after function:
x[0]=3
x[1]=4
x[2]=5
x[3]=6
x[4]=7

Note: Replace all lines of code of comment is another method of passing array.

Here we can see, changes made in the formal arguments are reflected in the actual argument. But Why?
It is because name of array is the base address of array i.e the address of the first element of array.

Call by Reference:
In call by reference the location (address) of actual arguments is passed to formal arguments, hence any change made to formal arguments will also reflect in actual arguments.
Example:

void function(int  *a)
{
a=a+2;
printf("\nFrom function value=%d",a);
}
int main()
{
int x=5;
printf("\nFrom main before function=%d",x);
function(&x);
printf("\nFrom main after function=%d",x);
return 0;
}

Output:

From main before function=5
From function value=7
From main after function=7

Call by reference works same even for the arrays,look at the example below:

void function(int *a)
{
int i;
for(i=0;i<5;i++)
{
*a[i]=*a[i]+2;
}
printf("\nFrom function value:");
for(i=0;i<5;i++)
{
printf("\na[i]=%d",*a[i]);
}
}
int main()
{
int j, x[5]={1,2,3,4,5};
printf("\nFrom main before function:");
for(j=0;j<5;j++)
{
printf("\nx[j]=%d",x[j]);
}
function(x);                                  // function(&x[0]);   means address of first element of array
printf("\nFrom main after function:");
for(j=0;j<5;j++)
{
printf("\nx[j]=%d",x[j]);
}
return 0;
}

Output:

From main before function:
x[0]=1
x[1]=2
x[2]=3
x[3]=4
x[4]=5
From function value:
a[0]=3
a[1]=4
a[2]=5
a[3]=6
a[4]=7
From main after function:
x[0]=3
x[1]=4
x[2]=5
x[3]=6
x[4]=7

You can see the difference between all the methods, these doesnot require any explanation.
Note: Header files are not included in the above examples.

Hope you liked the article, Subscribe our blog and stay updated 🙂

Leave a Reply

Your email address will not be published.