There are two parameter passing methods passing (or calling) data to functions in C language, i.e., Call by Value or Call by Reference (also Called pass by value and pass by reference). The most significant distinction between call by value and call by reference is that in the call by value, passing an actual value of the parameter. While, in a call by reference, passing the reference of the parameter; hence some change made to formal arguments will also reflect in actual arguments.
Let’s understand call by value and call by reference in c language.
In C, all function arguments are passed “by value” because C doesn’t support references as C++ and Java do. In C, both the calling and called functions don’t share any memory they have their copy, along with the called function can’t directly change a variable in the calling function; it may only alter its private, temporary copy.
In C, we use pointers to attain call by reference. In C++, we could use pointers or references to for pass by reference. In Java, primitive types passed as values, and non-primitive types are constant references.
We’ll be covering the following topics in this tutorial:
Call by value in C
• In call by value, a copy of actual arguments passed to formal arguments and the two types of parameters stored in different stack memory locations. So any changes made inside functions parameter, it is changed for the current function only. It will not change the value of a variable inside the caller method such as main().
• In call by value function, we can’t alter the value of the actual parameter from the formal parameter.
• In call by value, distinct memory allocated to actual and formal parameters because the value of the actual parameter replicated into the formal parameter.
• The actual parameter is the argument that’s used at the function call, whereas the formal parameter is the argument used in the function definition.
Consider the following example for the Call by Value in C
If the parameter passed by value, the parameter replicated from the variable used in, for example, main() into a variable used by the function. So if the parameter passed altered within the function, the value only changed from the variable used within the function. Let’s take us to look at a call by value example:
#include <stdio.h> void pass_by_value(int p) { printf("Inside pass_by_value p = %d before adding 10.\n", p); p += 10; printf("Inside pass_by_value p = %d after adding 10.\n", p); } int main() { int Var = 10; printf("Var = %d before function pass_by_value.\n", Var); pass_by_value(Var); printf("Var = %d after function pass_by_value.\n", Var); return 0; }
The output of this call by value code example:
Var = 10 before function pass_by_value. Inside pass_by_value p = 10 before adding 10. Inside pass_by_value p = 20 after adding 10. Var = 10 after function pass_by_value.
Let us take a look at what is going on within this pass-by-value source code example. From the main() we create an integer with the value of 10. We print some information at each point, starting by printing our variable Var. Then function pass_by_value is called, and we enter the variable Var. This variable (Var) subsequently copied to the function variable p. At the function, we add 10 to p (and call some print statements). Then when another statement called in main() the value of variable Var printed. We can understand that the value of variable Var isn’t affected by the call of the function pass_by_value().
Call by reference in C
• Simply call by reference method, the location (address) of the variable passed to the function call as the actual parameter.
• The value of the actual parameters could be altered by altering the formal parameters because the address of the actual parameters passed.
• In call by reference method, Both the actual and formal parameters refer to the same locations. Any changes made inside function performed on the value stored at the address of the actual parameters and the modified value gets stored at the same address.
Consider the following example for the call by reference.
If data is passed by reference, a pointer to the data is copied instead of the actual variable as is done in a call by value. Because a pointer is copied, if the value at that pointers address is changed in the function, the value is also changed in main(). Let’s take a look at a code example:
#include <stdio.h> void pass_by_reference(int *p) { printf("Inside pass_by_reference p = %d before adding 10.\n", *p); (*p) += 10; printf("Inside pass_by_reference p = %d after adding 10.\n", *p); } int main() { int Var = 10; printf("Var = %d before function pass_by_reference.\n", Var); pass_by_reference(&amp;amp;amp;amp;amp;amp;Var); printf("Var = %d after function pass_by_reference.\n", Var); return 0; }
The output of this call by reference source code example will look like this:
Var = 10 before function pass_by_reference. Inside pass_by_reference p = 10 before adding 10. Inside pass_by_reference p = 20 after adding 10. Var = 20 after function pass_by_reference.
Let’s explain what’s going on within this source code example. We begin with an integer Var with the value 10. The function pass_by_reference() called, and also the address of the variable Var passed to the function. In a function, there’s some before and after a print statement is completed and there is 10 added to value at memory pointed by p. Thus, at the end of the function, value is 20. Then in main(), we print the variable Var, and as you can see, the value is altered (as expected) to 20.
Difference between call by value and call by reference in C
Call By Value | Call by Reference |
A copy of the variable passed into the function. Such functions are called “Call By Values”. | An address of variables(location of variables) passed into the function. Such functions called “Call By References”. |
Parameters passed by calling functions don’t change by altering by called functions. | Called functions may alter parameters passed by calling functions. |
Actual and formal arguments created at a different memory location. | Actual and formal arguments created at the same memory location. |
It is the default method in programming languages. | It is supported only Java language. |
It uses a straightforward method for passing the variable value. | It uses pointers to store the address of variables. |
Call by value does not modify original value. | Call by reference modify original value. |
In the Call by value method, Actual arguments stay safe as they can’t be altered accidentally. | In the Call by reference method, Actual arguments aren’t Safe. They may be unintentionally altered, and that means you have to take care of arguments operations attentively. |
When to Use Call by Value and When to use Call by Reference?
One Benefit of the call by reference method is that it is using pointers, so there’s is no doubling of the memory used by the variables (like the copy of the call by value method). It’s, naturally, good, lowering the memory footprint is almost always a good thing. So why don’t we create all the parameters call by reference?
There are two reasons why this isn’t a good idea, and you have to select between call by value and call by reference. The reason is the side Impacts and privacy. Unwanted side effects are often due to accidentally changes that made into a call by reference parameter. Additionally, typically, you would like the data to be private and that someone calling a function can change if you want it. So it’s better to use a call by value by default and only use call by reference if data changes expected.
Advantages of using Call by value method
• The method does not alter the original value.; therefore, it’s keeping data.
• Whenever a function is called it, never affect the real contents of the actual arguments.
• Value of actual arguments passed into the formal arguments; therefore, any modifications made in the formal argument doesn’t impact the actual instances.
Advantages of using Call by reference method
• The function may alter the value of the argument, which can be very helpful.
• It doesn’t create replicate data for holding only one value which can help you to conserve memory space.
• Inside this method, there isn’t any copy of the argument created. Therefore it’s processed extremely fast.
• Helps you to avoid alterations done by mistake.
• A individual studying the code never understands that the value could be modified at the function.
Disadvantages of using Call by value method
• Changes to actual parameters may also alter corresponding argument variables.
• Inside this method, arguments should be variables.
• You can not directly alter a variable in a function body.
• Sometime argument may be complicated expressions.
• You will find two copies made for the same variable that’s is not memory efficient.
Disadvantages of using Call by reference method
• Powerful non-null guarantee. A function taking at a reference ought to be certain the input is non-null. Therefore, the null check does not need to be made.
• Passing by reference makes the function maybe not pure theoretically.
• A lifetime warranty is a large issue with references. It can be especially risky when working with lambdas and multi-threaded programs.