---
title: "Call by value versus call by reference"
subject: "Computer Programming"
chapter: 6
key: "semester_1/computer_programming:call-by-value-vs-reference"
source: https://www.bctengineer.com/q/semester_1/computer_programming/call-by-value-vs-reference
site: BCT Engineering
---

# Call by value versus call by reference

Computer Programming · Chapter 6: User-Defined Functions

- Asked in: 2073 Chaitra, 3 marks; 2070 Chaitra, 4 marks; 2070 Asar, 2 marks; 2069 Chaitra, 4 marks; 2072 Chaitra, 6 marks; 2069 Asar, 6 marks; 2067 Asar, 6 marks; 2067 Asoj, 3 marks (asked 8 times)
- Video: [Call By Value & Call By Reference in C](https://www.youtube.com/watch?v=HEiPxjVR8CU) — Neso Academy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/call-by-value-vs-reference
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-6

## Question

Differentiate between call by value and call by reference (pass by value and pass by reference) with suitable example.

## Short answer

Call by value passes a copy of arguments; changes inside the function do not affect the caller. Call by reference passes the address (pointer), so modifications directly alter the original variables.

## Answer

| Call by value | Call by reference |
|---|---|
| Copies of the actual arguments are passed | Addresses of the actual arguments are passed |
| Formal parameters are ordinary variables | Formal parameters are pointers |
| Changes in the function do not affect the caller's variables | Changes are made on the caller's variables |
| Uses extra memory for copies | No copy of data is made; saves memory for large data |
| Call: `swap(x, y)` | Call: `swap(&x, &y)` |
| Safe: original data is protected | Original data can be changed (risky) |

C itself always passes by value; "call by reference" is obtained by passing pointers.

```c
#include <stdio.h>
void swap_val(int a, int b)
{
    int t = a; a = b; b = t;
    printf("Inside swap_val: a=%d b=%d\n", a, b);
}
void swap_ref(int *a, int *b)
{
    int t = *a; *a = *b; *b = t;
}
int main(void)
{
    int x = 10, y = 20;
    swap_val(x, y);
    printf("After call by value: x=%d y=%d\n", x, y);
    swap_ref(&x, &y);
    printf("After call by reference: x=%d y=%d\n", x, y);
    return 0;
}
```
Output:
```text
Inside swap_val: a=20 b=10
After call by value: x=10 y=20
After call by reference: x=20 y=10
```

`swap_val` swaps only its local copies, so x and y are unchanged in `main`. `swap_ref` uses the addresses, so the values are really exchanged.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What's passed?
   A. Addresses
   B. Names
   C. Types
   D. Copies of actual arguments
   Answer: D. Copies. (Key point: Call by value)

2. After swap_val(x,y), x and y in main?
   A. Unchanged 10, 20
   B. Swapped
   C. 0
   D. Garbage
   Answer: A. Local copies only. (Key point: Effect)

3. How in C?
   A. Use &x param
   B. Pass pointers/addresses
   C. Use ref keyword
   D. Global
   Answer: B. C is always by value. (Key point: By reference)

4. Call by reference swap?
   A. swap(x, y)
   B. swap(*x,*y)
   C. swap(&x, &y)
   D. swap(x&,y&)
   Answer: C. Addresses passed. (Key point: Call syntax)

## Related in Computer Programming

- [Pointers in pass by reference; 2D array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-passing-by-reference-2d) — Computer Programming, chapter 9

## Related in other subjects

- [Namespace with pass by reference program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Anamespace-pass-by-ref-program) — Object Oriented Programming, chapter 2
- [OS abstraction, extended machine, system call](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Aos-abstraction-system-call) — Operating System, chapter 1
- [Cost vs value; factors affecting valuation](https://www.bctengineer.com/question.md?key=semester_8%2Fconstruction_management%3Acost-vs-value-factors) — Construction Management, chapter 13
- [Francis versus Kaplan applicability](https://www.bctengineer.com/question.md?key=semester_8%2Fpower_plant_design%3Afrancis-vs-kaplan) — Power Plant Design, chapter 3
