View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Difference Between Call By Value and Call By Reference

Updated on 10/04/20252,287 Views

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!

Tabular Difference Between Call by Value and Call by Reference in C

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!

What is Call by Value in C?

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. 

Example of Call by Value in C

#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. 

When should you use the call by value in C?

Call by Value is useful when:

  • You do not want the function to modify the original data.
  • You are working with simple, small data types like integers or characters.
  • You need to preserve the original value for other operations after the function call.

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

What is Call by Reference in C?

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!

Example of Call by Reference in C

#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!

When should you use the call by reference in C?

Call by Reference is preferred when:

  • You need to modify the original data.
  • You are working with large data structures like arrays or structures, and passing by reference is more memory-efficient.
  • You want to return multiple values from a function using pointers.

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!

Code Example Demonstrating Both Call by Value and Call by Reference

#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:

  1. modifyByValue function: This function takes an integer parameter num and modifies its value by adding 5. However, since the function is using Call by Value, it only modifies the local copy of num. The original variable passed in main() remains unaffected.
  2. modifyByReference function: This function takes a pointer to an integer (int *num). It dereferences the pointer (*num) and modifies the original value passed from main(). Since it uses Call by Reference, the actual argument is modified.

Output:

  • Before calling modifyByValue: x = 10
  • Inside modifyByValue: num = 15
  • After calling modifyByValue: x = 10
  • Before calling modifyByReference: x = 10
  • Inside modifyByReference: num = 15
  • After calling modifyByReference: x = 15

Note:

  • Call by Value (modifyByValue) does not affect the original variable x because only a copy of the value is passed to the function.
  • Call by Reference (modifyByReference) modifies the original variable x because the function has access to the variable’s memory address.

Also Read: Logical Operators in C article!

Key Differences Between Call by Value and Call by Reference in C

Here are some of the key differences between Call by Value and Call by Reference methods:

  • In the Call by Value method, the function works with a copy of the actual argument, while in Call by Reference method, the function works with the original argument through its reference (memory address).
  • The Call by Value method does not modify the original variable, whereas the Call by Reference method allows the function to modify the original value.
  • The Call by Value method is simpler and safer because it isolates changes to the function's local scope, avoiding unintended side effects. In contrast, the Call by Reference method is more efficient for large data types since it does not require copying the data.
  • The Call by Value method is commonly used when you don’t need to modify the original data. In contrast, the Call by Reference method is used when you need to alter the original data or pass large structures efficiently.

Conclusion

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.

FAQS

1. Can a function return a value in Call by Reference?

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.

2. What are the advantages of Call by Value in C?

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.

3. What are the limitations of Call by Value in C?

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.

4. Can Call by Reference be used with constant variables in C?

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.

5. Why is Call by Reference used in C?

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.

6. Can an array be passed by Call by Value in C?

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.

7. What is the role of pointers in Call by Reference in C?

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.

8. What is the impact on memory when using Call by Value?

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.

9. Is Call by Reference faster than Call by Value in C?

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.

10. Can Call by Value cause problems in recursion?

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.

11. What happens if both Call by Value and Call by Reference are mixed in a function?

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).

image

Take a Free C Programming Quiz

Answer quick questions and assess your C programming knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Start Learning For Free

Explore Our Free Software Tutorials and Elevate your Career.

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

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.