MCS-201 · Unit 5

Functions

Section 7 of 8

Block 2 · Unit 5Functions9 min · Section 7 of 8

5.7Call by Value

Covers IGNOU MCS-201 §5.8

When you pass a variable to a function, does the function work on your variable or on a copy? In C, the answer is always a copy. This explains a famous puzzle: a swap function that swaps nothing.

What call by value means

In call by value, the formal arguments are new local variables of the called function. They are created when the function is called, initialised with the values of the actual arguments, and destroyed when the function returns.

Memory during z = mul(x, y) with x = 2, y = 3
main's variables              mul's variables (copies)
  x: 2      ---- copy --->      a: 2
  y: 3      ---- copy --->      b: 3
  z: 6      <--- return ---     c: 6

The swap that does not work

Advantages and disadvantages

AdvantagesDisadvantages
Safe: a function cannot accidentally change the caller’s variablesA function cannot change the caller’s variables even when that is wanted (as in swap)
Simple to understand and reason aboutCopying large data items takes extra memory and time
Any expression can be an argument: square(x + 1), square(5)Only one result can come back, through return

How the problem is solved in C

To let a function change the caller’s variable, the caller passes the variable’s address, and the function uses that address to reach the original. This needs pointers (Unit 7), where the working swap is written as swap(&x, &y). It is the same mechanism by which a function can modify an array passed to it (Unit 4).

Key takeaways

  • In call by value, formal arguments are copies of the actual arguments.
  • Changes to parameters are lost when the function returns.
  • It is safe and simple, but a function cannot directly modify the caller’s variables.
  • Passing addresses with pointers (Unit 7) gives the effect of call by reference.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In call by value, what does the function receive?

Show answer

Answer: B. A copy of each argument’s value

The value of each actual argument is copied into a new local variable, the parameter.

Question 2 · multiple choice

What does this program print?

C · add_ten.c
#include <stdio.h>

void add_ten(int n)
{
    n = n + 10;
}

int main(void)
{
    int x = 5;
    add_ten(x);
    printf("%d\n", x);
    return 0;
}
Show answer

Answer: C. 5

n is a copy of x; adding 10 to the copy leaves x unchanged at 5.

Question 3 · multiple choice

How could add_ten in question 2 deliver its result without pointers?

Show answer

Answer: D. By returning n + 10 and writing x = add_ten(x);

A return value carries one result back to the caller. Renaming the parameter does not link it to the caller’s variable.

Question 4 · multiple choice

When are the formal arguments of a function destroyed?

Show answer

Answer: A. When the function returns

Parameters are automatic variables of the called function, so they exist only while it is executing.

Question 5 · multiple choice

Which is an advantage of call by value?

Show answer

Answer: C. The caller’s variables are protected from accidental change

Working on copies keeps the originals safe. Extra memory is used for the copies, and arguments may be any expressions.

Question 6 · short answer

For void change(int a) { a = a * 2; printf("%d ", a); } and int main(void) { int v = 7; change(v); printf("%d\n", v); return 0; }, give the output and explain it.

Show worked answer

The output is 14 7. When change(v) is called, the value 7 is copied into the parameter a. Inside the function a becomes 14 and is printed. When the function returns, a is destroyed. The variable v in main was never modified, so the second printf prints 7.

↑ ↓ move↵ openTab switch type