Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In any programming language like C, we use functions. Each function performs a specific task and also makes the code more structured. These functions are called as and when required. Functions can be invoked in two ways: Call by Value and Call by Reference in C. Functions also have parameters, the parameters which are passed to the function are called actual parameters, whereas the parameters which are received by the function are called formal parameters. Call by reference and Call by value are differentiated by the type of values passed to them as parameters.
It is a method used to call a function when we do not want any changes in the actual parameters. In the call by value method, the value of actual parameters are copied to the formal parameters,i.e., the different memory areas are allocated for actual parameters which are used in the function call and formal parameters which are used in the function definition, as a result, any changes made in the formal parameters don't affect the actual parameters.
Example
#include<stdio.h>
void Increment(int x){
x+=1;
printf("Value of X from the function : %d",x);
}
int main(){
int x=10;
Increment(x);
printf("\nValue of X from the main : %d",x);
}
Output
Value of X from the function : 11
Value of X from the main : 10
Explanation
In the above code, we first passed the variable x to the Increment function using the call by value method. The variable x inside the main doesn't reflect any changes made to the variable x inside the Increment function. Therefore, the value of x is different when we print it from the Increment function and from the main function.
It is a method used to call a function when any changes made to the formal parameters are to be reflected in the actual parameters. In the call by reference method, the address of the actual parameters is passed to the function call as the formal parameters, i.e., memory allocation for both the actual and formal parameters is the same. Therefore any changes made to formal parameters are actually being made to the actual parameters only, and hence their values are in sync.
Example
#include<stdio.h>
void Increment(int *x){
*x+=1;
printf("Value of X from the function : %d",*x);
}
int main(){
int x=10;
Increment(&x);
printf("\nValue of X from the main : %d",x);
}
Output
Value of X from the function : 11
Value of X from the main : 11
Explanation
In the above code, we have passed the address of the variable x to the Increment function using the call by reference method. The variable x inside the main will reflect any changes made to the variable x inside the Increment function. Therefore, the value of x is the same when we print it from the Increment function and from the main function.
Difference between Call by Value and Call by Reference
Call By Value
Call By Reference
In the call by value method, a copy of the variable is passed to the function.
In the call by reference method, the address of the variable is passed to the function.
In the case of call by value method, any changes made inside the function to the copy of the variable passed do not affect the value of the variable outside the function.
In the case of call by reference method, any changes made inside the function to the variable passed also affect the value of the variable outside the function.
In this, actual and formal parameters have separate memory locations.
In this, actual and formal parameters both have the same memory location.
This method is used as a default method for calling a function in programming languages like C++.
This method is supported by programming languages like C++, Java, C not it is not a default method.
Which is faster: call by value or call by reference?
The call by reference method is faster than the call by value method as the call by reference method uses pointers which are typically faster than passing the value.
Out of call by value and call by reference method, which uses less memory?
The call by reference method uses less space as compared to call by value as no separate copy of the parameters is created, the actual and formal parameters both point to the same memory.
Which is the default method of calling a function in the C programming language?
The call by value method is the default method for calling a function in the C programming language.
Key Takeaways
In this blog, we have covered the following things:
Firstly, we discussed the different ways of calling a function in the C programing language are.
Then we discussed the Call by Value and Call by Reference method with examples in detail.
We also discussed the difference between call by value and call by reference method.
Arrays and other data structures used in the C programming language can also be passed as arguments to function using these methods, you can study more about it from this.