Example HTML page

# Passing arguments to a function in “C”

In this section, i am going to write how to pass arguments to the function in “C” programming.

There are mainly two ways to pass the arguments from the calling function to called function.  They are

1. Pass by Value
2. Pass by Reference

In the first case the value is passed directly to the called function, and the address is passed to the function in the second case.

Pass by Value:

Function in C passes all arguments by value. When a single value is passed to a function via an actual argument, the value of the actual argument is copied into the function. Therefore, the value of the corresponding formal argument can be altered within the function, but the value of the actual argument within the calling routine will not change. This procedure for passing the value of an argument to a function is known as passing by value.

Program to find the addition of Two numbers by passing value:

```#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter the value for a and b: \n");
scanf("%d %d",&a,&b);
printf("The sum of %d and %d is : %d",a,b,c);
return 0;
}
int add (int p, int q)
{
int result;
result = p+q;
return(result);
}
```
Addition of Two numbers (Pass by Value)

### Output:

Command Prompt

Enter the value for a and b:
5
6
The sum of 5 and 6 is : 11

Pass by Reference:

When pass by reference technique is used, the address of the data item is passed to the called function. Using “&” operator we can determine the address of the data item. Note that function once receives a data item by reference, it acts on data item and the changes made to the data item also reflects on the calling function. Here you don’t need to return anything to calling function.

```#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter the value for a and b: \n");
scanf("%d %d",&a,&b);
printf("The sum of %d and %d is : %d",a,b,c);
return 0;
}
{
int result;
result = *p+*q;
return(result);
}
```
Addition of Two numbers (Pass by Reference)

### Output:

Command Prompt

Enter the value for a and b:
5
6
The sum of 5 and 6 is : 11

Example HTML page

### One thought on “Passing arguments to a function in “C””

• November 20, 2018 at 4:17 am