Skip to content
Official PYQ · 1994

GATE CS 1994 Q1.20 — Programming in C

Explain with AI

In which of the following cases is it possible to obtain different results for call-by-reference and call-by-name parameter passing methods?

Choose one option
View worked solution

Answer: C

To understand why the results differ, let us analyze how parameters are bound in both evaluation strategies:

  • Call-by-Reference: The address of the actual parameter is evaluated once at the time of the function call. Inside the function, any access to the formal parameter directly uses this pre-computed address.
  • Call-by-Name: The actual parameter expression is textually substituted for the formal parameter. It is re-evaluated every time the formal parameter is accessed or modified during execution.

Example Demonstration

Suppose we have a global index variable ii, an array AA, and a function foo(x):

int i = 1;
int A[5] = {10, 20, 30, 40, 50};

void foo(int x) {
    i = 2;
    x = 99;
}

foo(A[i]); // Passing an array element

1. Under Call-by-Reference:

  • When foo(A[i]) is called, i=1i = 1, so the address of A[1]A[1] is passed.
  • Inside foo, i is updated to 22.
  • The assignment x = 99 writes directly to the reference of A[1]A[1].
  • Result: A[1]A[1] becomes 9999.

2. Under Call-by-Name:

  • The formal parameter x is textually replaced by A[i].
  • Inside foo, the body effectively becomes:
    i = 2;
    A[i] = 99; // x is replaced by A[i]
    
  • Since ii was updated to 22 in the first step, the assignment A[i] = 99 evaluates to A[2] = 99.
  • Result: A[2]A[2] becomes 9999, while A[1]A[1] remains unchanged.

Since the index variable of the array element can change inside the function, passing an array element (Option C) can yield different results under the two parameter-passing techniques.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions