For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
In C programming, functions can receive arguments in two primary ways: Call by Value and Call by Reference. These mechanisms determine how the arguments are passed to functions and how modifications to those arguments inside the function reflect in the calling function.
The main difference between Call by Value and Call by Reference is that in Call by Value, a copy of the actual parameter is passed to the function, meaning any changes made inside the function do not affect the original data. On the other hand, Call by Reference passes the address of the argument, allowing the function to modify the actual value of the argument.
Interested in making a career in programming? If so, pursue online Software Development courses from top universities!
In this article, we will dive deep into the differences between Call by Value and Call by Reference methods. We will explore what each method is, explore examples, discuss when each should be used, and much more.
Must Explore: Introduction to C Tutorial!
For better understanding, let's explore the difference between Call by Value and Call by Reference in a tabular format:
Benchmark | Call by Value | Call by Reference |
Definition | A copy of the actual argument is passed to the function. | The memory address (reference) of the argument is passed. |
Effect on Original Argument | Does not modify the original value. | Modifies the original value directly. |
Memory Usage | More memory is used because a copy of the argument is created | More efficient as only the reference (address) is passed. |
Function Argument | The function works with a local copy of the argument. | The function works directly with the original variable. |
Speed | Slower for large data types due to copying data. | Faster as no data copying is involved. |
Scope of Changes | Changes made in the function do not affect the caller. | Changes made in the function are reflected in the caller. |
When to Use | When you do not want to modify the original data. | When you want to modify the original data or pass large data efficiently. |
Data Types | Generally used for simple data types (e.g., int, float). | Typically used for complex data types (e.g., arrays, structs). |
Example/Use Case | Passing a small integer or character to a function. | Passing an array or struct to a function for modification. |
Common Issue | Less efficient for large data structures due to copying. | Can lead to unintended changes to the original data |
Also Read: Call by Value and Call by Reference in C++ article for a better understanding!
In Call by Value, when a function is called, the actual parameters (the arguments) are passed to the function by making a copy of the values. The function works with these copies, and any changes made inside the function do not affect the original arguments. Essentially, the values of the variables are "isolated" inside the function, and any modification within the function will only alter the local copy of the data.
This method is simple and ensures that the original data remains unaffected. It is commonly used when the function only needs to read the value and not modify the original argument.
Must Read: Functions in C article.
#include <stdio.h>
void addTen(int num) {
num = num + 10;
printf("Value inside the function: %d\n", num);
}
int main() {
int x = 5;
addTen(x);
printf("Value outside the function: %d\n", x);
return 0;
}
Output:
Value inside the function: 15
Value outside the function: 5
Explanation: In this example, x is passed to the addTen() function by value. The function modifies its local copy of x, but the original x in the main() function remains unchanged.
Also Explore: Difference Between Arguments And Parameters article.
Call by Value is useful when:
This method is also suitable for situations where data integrity is important, and you want to avoid side effects.
Must Read: Master User Defined Function in C: Practical Examples & Best Practices
In Call by Reference, instead of passing a copy of the argument, the memory address (or reference) of the actual parameter is passed to the function. This allows the function to access and modify the original data directly. Since the function operates on the actual data, changes made within the function will affect the original variable in the calling function.
Call by Reference is commonly used when you need to modify the original data inside the function or when working with large data structures like arrays or structures, as it avoids the overhead of copying large amounts of data.
Pursue DBA in Digital Leadership from Golden Gate University, San Francisco!
#include <stdio.h>
void addTen(int *num) {
*num = *num + 10;
printf("Value inside the function: %d\n", *num);
}
int main() {
int x = 5;
addTen(&x);
printf("Value outside the function: %d\n", x);
return 0;
}
Output:
Value inside the function: 15
Value outside the function: 15
Explanation: In this example, the address of x is passed to the addTen() function. Inside the function, we dereference the pointer *num to modify the value of x. As a result, the change is reflected in the original variable.
Check out the Executive Diploma in Data Science & AI with IIIT-B!
Call by Reference is preferred when:
It is also ideal for recursive functions where you need to modify the arguments and reflect those changes in the recursive calls.
Also Read: Function Pointer in C article!
#include <stdio.h>
void modifyByValue(int num) {
// This function will modify only the local copy of 'num'
num = num + 5;
printf("Inside modifyByValue: num = %d\n", num); // Changes only local 'num'
}
void modifyByReference(int *num) {
// This function will modify the original 'num' using its reference
*num = *num + 5;
printf("Inside modifyByReference: num = %d\n", *num); // Changes the original 'num'
}
int main() {
int x = 10; // Initial value of x
printf("Before calling modifyByValue: x = %d\n", x);
modifyByValue(x); // Call by Value - 'x' won't be affected
printf("After calling modifyByValue: x = %d\n", x); // 'x' remains the same
printf("\nBefore calling modifyByReference: x = %d\n", x);
modifyByReference(&x); // Call by Reference - 'x' will be affected
printf("After calling modifyByReference: x = %d\n", x); // 'x' is changed
return 0;
}
Explanation of Code:
Output:
Note:
Also Read: Logical Operators in C article!
Here are some of the key differences between Call by Value and Call by Reference methods:
Call by Value and Call by Reference are two different ways of passing arguments to functions in C. Call by Value passes a copy of the argument, preventing any changes to the original data, while Call by Reference passes the memory address, allowing the function to modify the original data. Understanding the differences and when to use each method can significantly impact both the efficiency and behavior of your code.
Yes, a function in Call by Reference can modify the value of the argument passed by reference, and thus, the changes will persist outside the function. However, since the function itself does not return a value, the updated value is reflected in the actual argument passed.
Call by Value offers better security since the original values cannot be modified by the called function. It avoids side effects in the caller function, making debugging easier. It also prevents the modification of variables that should remain constant.
The primary limitation is that the changes made inside the function are not reflected outside the function. If you need to modify the original data, Call by Value is not suitable.
No, Call by Reference cannot be used with constant variables because the function will try to modify the passed arguments, but constant variables cannot be modified.
Call by Reference is used when you want the function to modify the original data. It's more memory-efficient because no copying of the data occurs, particularly with large data structures like arrays and structs.
Arrays in C are always passed by reference, even when using Call by Value. This means that although you're passing the array "by value" in the function signature, the function still gets access to the array’s memory address, meaning it can modify the array elements.
In Call by Reference, pointers play a crucial role because they allow functions to access and modify the memory location of the arguments. The function accepts pointers to the variables, which means changes to the variables inside the function will directly affect the caller's variables.
In Call by Value, each argument is copied into the function's local variable, consuming more memory, especially with large data types like arrays. This can be inefficient for large amounts of data.
Call by Reference is generally faster than Call by Value, especially when working with large data structures, as it doesn't require copying the data into the function's memory space. However, the speed difference might not be noticeable for small data types.
In Call by Value, since the values of variables are passed as copies, there's no risk of unintended side effects during recursion. Each recursive call works with its own copy of the variables, which makes debugging easier in recursive functions.
Mixing both methods in a function can lead to confusion and unexpected behavior. It’s usually better to choose one method based on the function's requirements. However, it’s common to see Call by Value for simple data types (like int or float) and Call by Reference for complex data types (like structs or arrays).
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.