C Programming • Arrays & Functions
C Programming / Call by value

Call by value

Notes 3 Arrays & Functions

Definition: In call by value, a copy of the actual argument's value is passed to the function. The function works on this separate copy.

Notes

Call by Value

Definition: In call by value, a copy of the actual argument's value is passed to the function. The function works on this separate copy.

💡 Example: If x = 10 is passed to a function, the function receives a copy of the value 10. Changing the parameter inside the function does not change the original x.

How Call by Value Works

The value of the actual argument is copied into the formal parameter. Both variables therefore hold separate values.

Original Variable ↓ Copy of Value ↓ Formal Parameter ↓ Function Works on Copy ↓ Original Variable Unchanged

Example

#include <stdio.h>

void change(int a)
{
    a = 20;                 // change only the local copy
}

int main()
{
    int x = 10;

    change(x);              // pass value of x

    printf("x = %d
", x);

    return 0;
}

Output

x = 10
📌 Why? The function changed its own parameter a, not the original variable x.

Memory Idea

In call by value, the formal parameter gets a separate copy of the value supplied by the caller.

Before function call:

x = 10


After calling change(x):

Original variable       Function parameter
     x = 10                  a = 10

     ↓ copy of value ↓

Changing a to 20:

x = 10                  a = 20

Important Characteristics

  • A copy of the value is passed to the function.
  • The formal parameter is separate from the original variable.
  • Changes to the parameter do not affect the original variable.
  • It is useful when the function only needs to use a value.

Real-World Example

Imagine giving someone a photocopy of a document. They can write on the copy, but the original document remains unchanged.

💡 Example: Original document = original variable
Photocopy = copied value passed to the function

Practical Example — Swap Using Call by Value

Problem Statement

Write a C program to demonstrate that swapping two values inside a function using call by value does not change the original values in the calling function.

Learning Outcomes

  • Understand how values are copied into function parameters.
  • Observe that changes to formal parameters do not affect originals.
  • Differentiate between local parameter changes and caller variables.

Hint

Pass two integer variables to a function and swap the received parameters using a temporary variable.

Theory

In call by value, the function receives copies of the arguments. Therefore, swapping the formal parameters changes only those copies and the original variables remain unchanged.

Program

#include <stdio.h>

void swap(int a, int b)
{
    int temp;

    temp = a;               // store a temporarily
    a = b;
    b = temp;               // swap the copied values
}

int main()
{
    int x = 10, y = 20;

    printf("Before function call: x = %d, y = %d
", x, y);

    swap(x, y);             // pass values of x and y

    printf("After function call: x = %d, y = %d
", x, y);

    return 0;
}

Expected Output

Before function call: x = 10, y = 20
After function call: x = 10, y = 20

Note

The swap occurs only inside the function because a and b are copies. The original x and y remain unchanged.

Call by Value vs Original Variables

Inside Function Calling Function
Works with formal parameter. Contains original variable.
Parameter contains a copy of the value. Original value remains unchanged by parameter assignment.
Example: a = 20 Original x can remain 10.

Call by Value vs Call by Reference

Definition: Call by value passes a value to a function, while the commonly used "call by reference" technique in C passes an address using a pointer so the function can work with the caller's original variable.

💡 Example:

Call by Value → change(x)
Address-based modification → change(&x)

Important Points for Exam

  • Call by value passes a copy of the argument's value.
  • The function works on its own parameter.
  • Changing the parameter does not change the original variable.
  • It is simple and safe when the original data should remain unchanged.
  • To modify the caller's variable in C, an address/pointer technique is used.
🎯 Easy Rule:

Call by Value = Copy goes to the function
Change in copy ≠ Change in original

Quick Revision

Concept Remember
What is passed? Copy of the value.
Parameter Separate from original variable.
Original variable Not changed by parameter assignment.
Typical example Swap using ordinary parameters does not swap originals.

Important Exam Questions

Short Answer Questions

  1. What is call by value?
  2. What is passed to a function in call by value?
  3. Does changing a formal parameter change the original variable?
  4. Why does the original variable remain unchanged?
  5. Write a simple example of call by value.
  6. What happens when two variables are swapped using call by value?

Long Answer Questions

  1. Explain call by value with syntax and suitable example.
  2. Write a C program to demonstrate call by value using a swap function.
  3. Explain why changes made to formal parameters do not affect the original variables in call by value.
  4. Differentiate between call by value and the address-based technique commonly called call by reference in C.
🎥 Recommended Learning

Watch a beginner-friendly explanation of call by value in C.

▶ Watch: Call by Value in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for call by value will be provided here.

🧠 Mind Map

Use the mind map for quick revision of value copying, formal parameters and original variables.