There are two ways to pass the data into the function in C language:
- method :call by value
- method :call by reference.
Call by value in C
- In this method, we pass the values of variables (the value of the actual parameters) into the formal parameters.
- Here, the formal parameter cannot modify the value of the actual parameter.
- The formal parameter gets a copy of the value of the actual parameter, therefore the memory allocation is different for actual and formal parameters.
- The actual parameter is the argument that is used in the function call, whereas the formal parameter is the argument that is used in the function definition.
Example: Swapping values on integers
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameters do not change by changing the formal parameters in call by value, a = 10, b = 20
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b = 10
}
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20
Here, the value of a and b remain unchanged in the main function, but that of function swap() is getting exchanged.
Call by reference in C
- In call by reference, the addresses of actual arguments in the calling function are copied into the formal arguments of the called function
- Using this method, we have access to the actual arguments and hence we can be able to manipulate them.
- The memory allocation is the same for both formal parameters and actual parameters.
Example: Swapping integer values
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameters do not change by changing the formal parameters in call by value, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a= *b;
*b=temp;
}
Output
Before swapping the values in main a = 10, b = 20
After swapping values in main a = 20, b = 10
The above code manages to exchange the values of actual parameters.
Note: also read about the Passing arguments between functions in C & Functions in C
Follow Me
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment