Call by value and Call by reference in C Language – CsTutorialpoint
Hello Friends In today’s article we are going to talk about Call by value and Call by reference. In the previous article, we had known what is function and how function is used.
If you have not read that article, then definitely read it once because, in order to better understand Call by value and Call by reference, you need to know about what is Function.
Along with this, you also need to know about what are Arguments and what are the types of Arguments.
You can read about the Arguments here: Arguments in C
Now Let’s understand what is Call by value and Call by reference.
Call by value and Call by reference in C
As we know, without calling the function it does not come into the program and is not executed. If we want to use any function in our program, then first we have to call it.
When we call that function by writing the name of the function, then it is called Function Calling.
While calling the function, if any argument or value has to be passed to that function, then we pass that argument or value by writing it in parenthesis() while calling the function.
In C language we can pass two types of value in parenthesis of function while calling function -: call by value and call by reference.
Before telling about call by value and call by reference, I want to tell you about what are Actual parameters and Formal parameters or Actual arguments and Formal arguments.
Actual parameters or arguments -: While calling a function, when we write some value or argument in its parenthesis, then it is called Actual parameters or arguments.
Formal parameters or arguments -: While defining the function, when we write or define some argument in parenthesis, then it is called formal parameters or arguments.
Now Let’s understand Call by value and Call by reference one by one.
What is Call by value in C
While calling the function, when you pass a value or the value of a variable by writing it in the parenthesis of that function, then it is called call by value.
In this, the value of the actual parameters is passed or assigned to the formal parameters so that the function can use that value to complete its task.
There is no change in the original value in Call by value because both the values are stored in different locations.
Example -: Area of rectangle
#include<stdio.h>
int area(int l, int b); //Function Declaration
void main()
{
int var1 = 10;
int var2 = 20;
int var3 = area(var1, var2); //Function Call
/*
Here we are passing the stored value inside it to the function by writing the variable name.
*/
printf("Area of rectangle is : %d", var3);
}
int area(int l, int b) //Function Definition
{
int A = l*b;
return A;
}
Output -:
Area of rectangle is : 200
Take a look at the above example once, in that the variable name we have written in the parenthesis while calling the function is called Actual Parameter and whatever is written in the parenthesis while defining the function is called Formal Parameter.
In Call by value, the value of the Actual Parameter is copied or assigned to the Formal Parameter so that the function can use those values to accomplish its task.
In Call by value, the function can use only those values and cannot make any changes in it like in this example.
The function performs calculation using the value passed while calling And whatever result comes, Returns it back to the main() function from where it was called.
Advantages of Call by value
- It does not change the value of original variable, which keeps the data secure.
- When the function is called, it has no effect on the actual parameter’s argument.
- In this, the value of the actual parameter goes to the formal parameter, so that if any change is made, its effect is only on the formal parameter, it does not have any effect on the actual parameter.
Disadvantages of Call by value
- In this, you cannot change the value of the original variable inside the function.
- The actual argument must be a variable.
- The same value is stored in two different variables which are not memory efficient.
What is Call by reference?
When we pass the address of a variable while calling a function, it is called Call by reference or Call by address.
In this, the address of a variable is passed to the pointer variable (which can store the address of any variable) kept in the formal parameters, by doing this the function can also change the value kept in that address.
In call by reference, the original value also changes because both values are stored in the same location.
In this, the address of variable kept in the actual parameters is stored in the pointer variable kept in the formal parameters.
In this, the address of variable kept in the actual parameters is stored in the pointer variable.
Example -: Swapping the values of the two variables
#include <stdio.h>
void swap(int *a, int *b); //function declaration
void main()
{
// variable definition
int x = 100;
int y = 200;
printf("Before swap, value of x : %d\n", x );
printf("Before swap, value of y : %d\n", y );
swap(&x, &y); // calling a function to swap the values
printf("After swap, value of x : %d\n", x );
printf("After swap, value of y : %d\n", y );
}
void swap(int *a, int *b) //Function Definition
{
int temp;
temp = *a; //save the value of a
*a = *b; //put b into a
*b = temp; /* put temp into b */
}
Output -:
Before swap, value of x : 100
Before swap, value of y : 200
After swap, value of x : 200
After swap, value of y : 100
See in the above example, in that we have created a function named swap whose parenthesis ( ) contains a pointer variable which holds the address of another variable, or we can say that the variable whose address is in the pointer variable, the pointer points to that variable.
When we called the swap function in the main() function, we passed the address of the x and y variables to the swap function by doing &x, and &y in its parenthesis.
By doing this, the address of the x and y variables are stored in pointers named a and b.
With the help of these pointer variables (which are pointing to the x and y variables), we can change the value of x and y variables. As in this example.
In this example, we have changed the value of x and y variables by using pointer variables a and b, which you can see in the output.
If you want to know about what is a pointer, then you can read about it in detail from here – Pointer in C |
In call by reference, the original value also changes because both values are stored in the same location.
In this, the address of variable kept in the actual parameters is stored in the pointer variable which is held in the formal parameters.
Advantages of Call by reference
- It does not have to create duplicate variables to store the same value.
- This does not cause memory loss.
- In this, we can change the value of the original variable inside the function.
- By passing the address, we are able to change the value of that variable.
Disadvantages of Call by reference
- Variable changes inside the function affect the original variable.
- Call by reference can sometimes cause problems in the program and the program can be complex.
When to use Call by value and Call by reference?
When to use Call by value and Call by reference, it depends on different circumstances, below I have given some circumstances according to which you can use Call by value and Call by reference.
When to use Call by value?
- When we do not want to change the original value of the variable, then we use call by value.
- We use it when we do not want any kind of side effect in the value of the actual parameter.
- When we have to pass only value to the function to do its work, then we use call by value.
When to use Call by reference?
- When we want to make any change in the original value of the variable then we use call by reference.
- We use it when we want to change the value of the actual parameter.
- When we have to pass a reference to that variable for the function to do its work, then we use call by reference.
Difference Between Call by value and Call by reference
Following is the difference between Call by value and Call by reference –
Call by value | Call by reference |
The copy of the value of the variable is passed to the function. | The address of the variable is passed to the function. |
The change in value within a function is limited to that function only. Changes to the formal parameter do not affect the actual parameter. | The change in value inside a function is not limited to that function. changes in the formal parameter affect the actual parameter. |
Actual and formal arguments are located in different locations in memory so that changes in one do not affect the other. | Actual and formal arguments are located in the same location in memory so that changes in one affect the other. |
There is no change in the original value. | The original value also changes. |
A normal variable is used to store the value passed to the function. | A pointer variable is needed to store the address of the variable passed to the function. |
Read More -:
- What is Function in C
- Function Arguments in C
- Actual Argument and Formal Argument in C
- Command Line Arguments in C
- Recursion in C
- Storage Classes in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, I hope that after reading this article you will know very well, Call by value and Call by reference in C
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
If you liked this post, then do not forget to share this post with your friends so that they can also get information about Call by value and Call by reference in C Language
If you still have any questions or doubts related to Call by value and Call by reference, then you must tell me, I will answer all your questions and you can contact us for more information.
Subscribe to this website newsletter to get information related to this new technology, Programming Language, Coding, C Language, C++, Python Course, Java Tutorial.