Call by value and Call by reference in C

In this tutorial, we will learn about Call by Value and Call by Reference in C. In Call by Value, the function gets a copy of the variable. This means any changes inside the function do not affect the original value. In Call by Reference, the function gets the memory address of the variable. This allows it to change the original value. Let’s look at some examples!

Contents:

  1. Introduction to Call by Value and Call by Reference in C
  2. What is Call by Value in C?
  3. Examples of Call by Value in C
  4. Advantages and Disadvantages of Call by Value in C
  5. What is Call by Reference in C?
  6. Examples of Call by Reference in C
  7. Advantages and Disadvantages of Call by Reference in C
  8. Call by Value Vs Call by Reference in C
  9. When to Use Call by Value vs Call by Reference in C?
  10. FAQs on Call by value and Call by reference in C

Introduction to Call by Value and Call by Reference in C

In C, there are two ways to pass arguments to a function: Call by Value and Call by Reference. These methods decide how data is sent to functions. Knowing how they work helps in writing clear and efficient programs.

What is Call by Value in C?

Call by Value is a method of passing arguments to a function in C. In this approach, the function receives a copy of the actual parameter. As a result, any modifications inside the function do not change the original variable in the calling function.

How Does Call by Value Work?

advertisement
  • The function gets a copy of the argument.
  • Any changes inside the function affect only the copy, not the original variable.
  • The original variable stays the same after the function call.
  • It uses more memory if large data structures are passed because copies are made.

Syntax:

return_type function_name(data_type parameter)
{
    // Function body
}

Examples of Call by Value in C

Example 1:

Free 30-Day Python Certification Bootcamp is Live. Join Now!
#include <stdio.h>
 
void updateMarks(int marks) {  // Function receives a copy of 'marks'
    marks += 10;               // Modify the copy
    printf("Updated Marks inside function: %d\n", marks);
}
 
int main()
{
    int studentMarks = 50;  // Original marks
    updateMarks(studentMarks);  // Passing by value
    printf("Original Marks in main: %d\n", studentMarks);
    return 0;
}

Output:

Updated Marks inside function: 60
Original Marks in main: 50

This program shows Call by Value in C. The function updateMarks(int marks) gets a copy of the variable. It adds 10 and prints the new value, but the original variable in main() stays the same. In main(), studentMarks is set to 50 and passed to updateMarks(). The function prints 60, but studentMarks remains 50 after the function call. This proves that Call by Value does not change the original variable.

Example 2: Swapping Two Numbers

#include <stdio.h>
 
void swap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
    printf("Inside function: a = %d, b = %d\n", a, b);
}
 
int main() {
    int x = 10, y = 20;
    swap(x, y);
    printf("Outside function: x = %d, y = %d\n", x, y);
    return 0;
}

Output:

Inside function: a = 20, b = 10
Outside function: x = 10, y = 20

This program demonstrates Call by Value in C using a swap function. The function swap(int a, int b) receives copies of the original variables. It swaps their values and prints them inside the function, but the original variables remain unchanged in main(). In main(), x = 10 and y = 20 are passed to swap(). Inside the function, a and b are swapped, displaying a = 20, b = 10. However, after the function call, x and y remain 10 and 20, proving that Call by Value does not modify the original variables.

advertisement

Advantages and Disadvantages of Call by Value in C

Advantages of Call by Value in C

  • Original Data Stays Safe: The function works with a copy, so the original variable does not change.
  • No Unwanted Changes: Any updates inside the function affect only the copy, keeping global data safe.
  • Easy to Use: It is simple to understand and does not involve pointers, making it great for beginners.
  • More Secure: The function cannot change sensitive data from the main program, reducing risks.
  • Good for Small Data: Works well for small values like int, float, and char since it uses little memory.

Disadvantages of Call by Value in C

  • Uses More Memory: A new copy is made for each function call, increasing memory use, especially for large data.
  • Slower for Large Data: Copying big structures or arrays takes time and can slow down the program.
  • Not Good for Modifications: Changes inside the function do not affect the original variable, making it less useful when updates are needed.
  • Cannot Change the Original Variable: If a function must update a variable, Call by Reference (using pointers) is a better choice.

What is Call by Reference in C?

Call by Reference is a way to pass arguments to a function by sending the memory address of the variable. This allows the function to change the original value instead of working on a copy.

How Does Call by Reference Work?

  • Instead of passing a copy, we send the memory address (pointer) of the variable.
  • The function accesses the original memory location and modifies the actual value.
  • Any changes made inside the function affect the original variable.
  • It is efficient for large data because no extra copy is created.

Syntax:

return_type function_name(datatype *parameter) {
    // Function body
}
  • The parameter is a pointer (*) that stores the address of the actual variable.
  • The function modifies the original variable through its address.

Examples of Call by Reference in C

Example 1: Updating Student Marks

#include <stdio.h>
 
void updateMarks(int *marks) {
    *marks += 10;  // Increase the original marks by 10
}
 
int main() {
    int studentMarks = 50;
    updateMarks(&studentMarks);  // Passing address
    printf("Updated Marks: %d\n", studentMarks);
    return 0;
}

Output:

Updated Marks: 60

This program shows Call by Reference in C. The function updateMarks(int *marks) gets the memory address of the variable. It adds 10 to the original value. In main(), studentMarks is set to 50. Its address is sent to updateMarks(), so the function updates the actual variable. After the function call, studentMarks becomes 60. This proves that Call by Reference changes the original value.

Example 2: Swapping Two Numbers

#include <stdio.h>
 
void swap(int *a, int *b) { 
    int temp = *a;
    *a = *b;
    *b = temp;
}
 
int main() {
    int x = 10, y = 20;
    swap(&x, &y);
    printf("After swapping: x = %d, y = %d\n", x, y);
    return 0;
}

Output:

After swapping: x = 20, y = 10

This program shows Call by Reference in C using a swap function. The function swap(int *a, int *b) gets the memory addresses of two variables and swaps their values. In main(), x = 10 and y = 20. Their addresses are passed to swap(), so the function swaps the actual values. After the function call, x becomes 20 and y becomes 10. This proves that Call by Reference allows direct changes to the original variables.

Advantages and Disadvantages of Call by Reference in C

Advantages of Call by Reference in C

  • Changes the Original Data: The function works with the real variable, so any changes remain after the function call.
  • Saves Memory: No extra copies are made, reducing memory use. This is helpful for large data like arrays and structures.
  • Faster Execution: Since no data is copied, the function runs faster, especially with large data.
  • Can Return Multiple Values: A function can change many variables at once, unlike Call by Value, which returns only one.
  • Good for Large Data: Best for arrays, structures, and linked lists, avoiding extra memory use.
  • Useful for Recursion and Data Structures: It helps in working with linked lists, trees, and graphs without copying data.

Disadvantages of Call by Reference in C

  • Risk of Unwanted Changes: The function directly changes the original variable, which can cause errors.
  • Less Secure: If not handled well, the function may change the wrong memory location, causing crashes.
  • Uses Pointers: You need to understand pointers, which can be hard for beginners.
  • Harder to Debug: Since the function changes real data, finding errors is more difficult.
  • Unexpected Effects: If different functions change the same variable, it can create confusing bugs.
  • More Complex Code: Using pointers makes the code harder to read and increases the risk of memory issues.

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

Here is a key differences between Call by Value and Call by Reference in C:

Feature Call by Value Call by Reference
How Data is Passed A copy of the variable is sent to the function. The memory address of the variable is sent.
Effect on Original Data The original variable stays the same. The function changes the original variable.
Memory Use Uses more memory because it makes a copy. Uses less memory since no copy is made.
Execution Speed Can be slower for large data since copies take time. Faster because no extra copying is needed.
Security Safer, as the original data is not changed. Less safe since the function can change the actual variable.
Usage When the original value should not change. When you need to modify the original variable.
Return Values Can return only one value. Can update multiple variables.
Complexity Simple and easy to use (no pointers needed). More complex because it requires pointers.

When to Use Call by Value vs Call by Reference in C?

Use Call by Value when:

  • You want to keep the original data unchanged.
  • The function does not need to modify the variable.
  • You are working with small data types (like int, char, float).
  • Simplicity is important, and you want to avoid pointers.
  • Security matters, and you want to prevent accidental data modifications.

Use Call by Reference when:

  • The function needs to modify the original variable.
  • You are working with large data structures (arrays, structs) to avoid memory overhead.
  • You need to return multiple values from a function.
  • Performance is a priority, and avoiding unnecessary copies is important.
  • You need direct memory access for advanced data structures like linked lists and trees.

FAQs on Call by value and Call by reference in C

1. What is Call by Value in C?
Call by Value means passing a copy of a variable’s value to a function. The function works on this copy, so any changes inside the function do not affect the original variable.

2. What is Call by Reference in C?
Call by Reference means passing a variable’s memory address to a function. The function accesses the original memory location, allowing it to modify the actual variable.

3. What is the main difference between Call by Value and Call by Reference?
In Call by Value, the function works on a copy, leaving the original data unchanged. In Call by Reference, the function works on the actual variable, so changes affect the original data.

4. Does C support Call by Reference?
C does not have built-in Call by Reference like C++. However, it achieves the same effect by passing pointers, which allow functions to modify original values.

5. When should I use Call by Value or Call by Reference?
Use Call by Value when you want to protect the original data from changes. Use Call by Reference when the function needs to modify the variable or when working with large data structures to save memory.

6. Is Call by Reference safer than Call by Value?
No, Call by Reference can be risky because it changes the original data, which may cause unintended modifications. Call by Value is safer because it does not affect the original variable.

7. Can Call by Reference be used to return multiple values?
Yes, a function can modify multiple variables by using pointers, making it possible to return multiple values.

Key Points to Remember

Here is the list of key points we need to remember about “Call by value and Call by reference in C”.

  • A copy of the variable is passed in Call by Value, whereas Call by Reference sends the memory address.
  • The original variable remains unchanged in Call by Value, but Call by Reference modifies it directly.
  • Since Call by Value creates copies, it increases memory usage, while Call by Reference is more efficient.
  • Security is higher with Call by Value because it prevents unintended changes, unlike Call by Reference.
  • Using Call by Reference requires pointers, which add complexity but allow direct data modification.
  • When a function needs to return multiple values, Call by Reference is the better choice.
  • Small data types work well with Call by Value, whereas large structures benefit from Call by Reference.
  • In swapping functions, Call by Value fails to modify the original numbers, while Call by Reference succeeds.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.