Skip to study material
← Question index

QUESTION 12 · Function Communication

Explain call by value in C with an example.

5 marks

Answer in brief

C uses pass by value: each parameter receives a value supplied by the corresponding argument. An ordinary integer parameter is a separate object from the caller’s integer variable.

Working

  1. The caller evaluates the argument expression.
  2. The resulting value is used for the parameter according to the declared type.
  3. The function works with its parameter object. Assigning a new value to that parameter does not assign to the caller’s variable.

Example trace

main starts with number = 10 and calls change(number). The parameter n receives 10. Inside change, n = 25 changes only n, so the first line prints 25. After the function returns, number in main still contains 10. This shows the difference between a copied value and the original object.

Advantages

A function can use and update a working parameter without unintentionally replacing the caller’s ordinary value. This helps keep calculations separate. For example, a digit-processing function can repeatedly divide its local copy while the original input remains available to main.

Pointer clarification

Passing an address does not change C into pass by reference. The address is a pointer value, and that value is copied into a pointer parameter. The function can use the copied pointer to access the same caller-owned object. Changing the pointer parameter itself is different from changing the object to which it points.

Complexity

The fixed-size integer example uses constant work and a fixed number of objects, so time and extra space are O(1). The copying cost of an argument depends on its type; do not assume every possible argument is a single small integer.

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
void change(int n)
{
    n = 25;
    printf("Inside = %d\n", n);
}
int main(void)
{
    int number = 10;
    change(number);
    printf("Outside = %d\n", number);
    return 0;
}

Sample input and output

This example uses values already written in the program.

Output

Inside = 25
Outside = 10

Related exam questions

Related questions in the function arguments category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.

  • March/April 2023 · Q1(g) · 2 marks

    Define formal arguments.

    Paper P02 · page 1
  • March/April 2023 · Q8(b) · 5 marks

    Write a C program using functions to swap two numbers using global variables and the call-by-reference concept.

    Paper P02 · page 2

View this category in the exam question bank →