GATE / CS ARCHIVE

SUBJECT COLLECTION

Programming in C · Functions & Parameter Passing

16 questions, newest first

GATE CSE has asked 16 questions on Functions & Parameter Passing (Programming in C) from 1996 to 2025, in 12 of the 46 papers; the most recent is in GATE CSE 2025 Set 2. In the last five years it carried 0.4 marks per 100-mark paper on average, against 0.3 since 2003. Since 2014 the questions have been 5 NAT and 1 MCQ. 4 groups of questions here are solved the same way; “One per method” keeps the newest of each.

Browse topics
2 marks · NAT
Report issue

Functions & Parameter Passing

Question 63, GATE 2025: A C program defines g(n)=n+10 and f(n)=g(n*2). It initializes sum=0 and executes for(n=1;n<3;n++) sum += g(f(n)); then prints sum. What integer is printed?
Tap to enlarge
Answer & solution

Official answer: 46

Resolve the function composition from the innermost call outward. Since g(t)=t+10, f(n)=g(2n)=2n+10. Applying g again gives g(f(n))=f(n)+10=2n+20. The two additions of 10 arise from two separate g calls; the outer one does not double its input.

The loop condition is n<3, so it runs for n=1 and n=2 only. At n=1, f(1)=12 and g(f(1))=22, taking sum from 0 to 22. At n=2, f(2)=14 and g(f(2))=24, taking sum to 46. After incrementing n to 3 the condition fails, and no n=3 term is added. The printed value is 46.

Original paper, page 47 · Answer key

1 mark · MCQ
Report issue

Trace argument evaluation separately from function execution

Functions & Parameter Passing

Question 13, GATE 2024: Functions g(p) and h(q) each print their integer argument with no separator and return it. Function f(x,y) calls g(x), then h(y). main calls f(g(10),h(20)). Assume argument expressions are evaluated right to left. Output choices: A 20101020; B 10202010; C 20102010; D 10201020.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Step through it (13 steps)

Approach: Separate the calls that evaluate arguments from the calls inside f.

1. Derivation. Using the stipulated right-to-left argument evaluation, h(20) prints 20, then g(10) prints 10. Their return values bind x=10, y=20 in f. Its sequential statements then print10 and 20. With no separators the output is 20101020, option A. Right-to-left evaluation is an explicit premise here, not a portable C-language guarantee.

2. Explanation and checks. The event trace is h(20): print 20, return 20; g(10): print 10, return 10; enter f with x=10,y=20; g(x): print 10; h(y): print 20. Reversing evaluation order does not swap the formal parameters: the first argument still supplies x. The explicit right-to-left assumption applies to evaluating the argument expressions, not to reversing the two statements in the body of f.

Original paper, page 10 · Answer key

1 mark · NAT
Report issue

Tracing value parameters and successive assignments

Functions & Parameter Passing

Question 18, GATE 2019: Function jumble(int x,int y) assigns x=2*x+y and returns x. Main initializes x=2,y=5, then executes y=jumble(y,x), then x=jumble(y,x), and finally prints main's x. Find the printed integer.
Tap to enlarge
Answer & solution

Official answer: 26 to26

Our answer: 26

Method: keep the function's local parameter names separate from the caller's variables, and evaluate statements in sequence.

1. Initially main has (x,y)=(2,5). In y=jumble(y,x), the first argument is main's y=5 and the second is main's x=2. Inside the function, local (x,y) therefore starts as (5,2).
2. The function computes local x=2×5+2=12 and returns 12. C passes these integers by value, so changing the local x does not alter main's x. The assignment updates only main's y, leaving main at (2,12).
3. The second call, x=jumble(y,x), now passes (12,2). The returned value is 2×12+2=26, and the enclosing assignment stores it in main's x.
4. Main is now (26,12), so printing x displays 26. Reusing the function's parameter name x does not make it an alias of the caller's x.

Result: 26. The official range 26 to 26 accepts this exact value.

Original paper, page 7 · Answer key

1 mark · NAT
Report issue

Functions & Parameter Passing

Question 12, GATE 2016: C-like code: void f(int* p,int m){m=m+5; *p=*p+m; return;} void main(){int i=5,j=10; f(&i,j); printf("%d",i+j);} Find the printed value under the intended ordinary integer execution.
Tap to enlarge
Answer & solution

Official answer: 30.0 : 30.0

Our answer: 30

The value parameter m changes from 10 to 15 locally. Writing through p updates i from 5 to 20, while caller j stays 10. Hence i+j=30. Routine library declarations and the intended void-main extension are assumed.

Passing &i copies i’s address, not its value, into p. Dereferencing p consequently changes the caller’s i. Passing j copies 10 into the separate local variable m, so changing m cannot change j. The final addition is 20+10, not 20+15 or the original 5+10.

Original paper, page 28 · Answer key

1 mark · NAT
Report issue

Functions & Parameter Passing

Question 34, GATE 2015: Find the printed integer. C code: void f1(int a,int b){int c;c=a;a=b;b=c;} void f2(int *a,int *b){int c;c=*a;*a=*b;*b=c;} int main(){int a=4,b=5,c=6;f1(a,b);f2(&b,&c);printf("%d",c-a-b);}. Assume required declarations.
Tap to enlarge
Answer & solution

Official answer: -5

Our answer: −5

At f1 entry the parameters copy the caller's 4 and 5; swapping these copies leaves main's a,b,c at 4,5,6. In f2, the pointer parameters are also copied, but dereferencing them reaches main's b and c. Saving *a before overwriting prevents losing the original b. After the swap main has 4,6,5; subtraction is left-associative, so (5−4)−6=−5.

Solution: The value-parameter swap f1 changes only its local copies. The pointer-based f2 swaps caller b and c, giving a=4, b=6, c=5. Therefore c−a−b=−5, with routine declarations assumed.

Original paper, page 13 · Answer key

2 marks · NAT
Report issue

Functions & Parameter Passing

Question 40, GATE 2014: Consider the printed function double f(double x){if(abs(x*x-3)<0.01) return x; else return f(x/2+1.5/x);}. Give a value q, to two decimal places, such that f(q) returns q. Preserve the printed abs rather than silently replacing it with fabs.
Tap to enlarge
Answer & solution

Official answer: 1.72 to 1.74

Our answer: 1.73 in the intended real-absolute-value model

1. Set up the reasoning. For immediate return under the intended mathematical absolute-value interpretation, test whether the chosen two-decimal q satisfies abs(q²-3)<0.01. The recursive expression is a Newton update for a square root, but no iteration is needed if the starting value already passes.

2. Derive the result. For mathematical absolute value, immediate return requires|q²−3|<.01. q=1.73 gives q²=2.9929 and error.0071<.01, so it returns immediately (as does−1.73). The positive value is within the official range. Literal C abs takes an int: with declarations in scope, the double difference is truncated before testing, so every q with|q²−3|<1 returns immediately, including 1.50. The source should use fabs for floating absolute value if that is intended; the negative solution is also valid without a positivity restriction. Official1.72 to 1.74 is retained, with these distinct semantics explicit.

3. Check the interpretation. The distinction between mathematical absolute value and C's integer abs is substantive. The latter converts the double argument to int before testing, broadening immediate-return values. Also the source does not require q positive, so -1.73 is mathematically valid despite the positive official interval; retain the existing discrepancy.

Original paper, page 40 · Answer key

2 marks · MCQ
Report issue

Misspelled keyword that is a legal identifier

Functions & Parameter Passing

Question 61, GATE 2005: Line 3 of historical C code is fro(I=0, I<N, I++); inside int main(){int I, N; ...}. During object-module creation is the response A no compilation error; B lexical error only; C syntactic error only; D both? Preserve the printed identifier fro and comma-separated expressions, not a repaired for loop.
Question 61, GATE 2005: Line 3 of historical C code is fro(I=0, I<N, I++); inside int main(){int I, N; ...}. During object-module creation is the response A no compilation error; B lexical error only; C syntactic error only; D both? Preserve the printed identifier fro and comma-separated expressions, not a repaired for loop.
Tap to enlarge
Your answer
Answer & solution

Answer: A under C90; modern C needs a diagnostic (no official key was published)

The printed line is fro(I=0, I<N, I++); with commas, not semicolons. Lexically fro is a valid identifier, and syntactically this is a function call with three arguments, not a for statement. Under the historical C90 implicit-function-declaration rule, an object module can be produced, so A is the intended compilation-stage answer; a missing definition may fail at link time. In C99 and later the undeclared function requires a diagnostic, but this is not a lexical or grammar error of the kind offered. The argument side effects also give undefined runtime behavior under C90 because I is modified more than once without an intervening sequence point; no runtime result is asserted. The paper preserve fro and commas.

The lexer accepts fro as an ordinary identifier. The grammar accepts the following parentheses as a function call whose three arguments are I=0, I<N, I++. Thus misspelling for does not by itself create a lexical or grammar error. Older C allowed an implicit function declaration; an undefined fro may be discovered later by the linker. Modern C requires a diagnostic for the undeclared function, and runtime side effects are separately problematic. The intended old compilation-stage answer must not be presented as portable modern C.

Original paper, page 7

1 mark · MCQ
Report issue

Pass-by-value swap

Functions & Parameter Passing

Question 2, GATE 2004: Consider this C function:
void swap(int a, int b) {
  int temp; temp = a; a = b; b = temp;
}
To exchange caller variables x and y, which statement is correct?
Options:
(A) Call swap(x,y)
(B) Call swap(&x,&y)
(C) swap(x,y) cannot be used because it returns no value
(D) swap(x,y) cannot be used because its parameters are passed by value
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. The function receives local copies a and b. Swapping those copies does not modify the caller’s x,y, so D.

2. A void function can modify caller objects through pointers, but this function’s parameters are int, not int*, and calling it with addresses does not repair its body.

3. Teaching check: Trace x=3,y=8: entry makes local a=3,b=8; the body leaves a=8,b=3, but x=3,y=8. A correct pointer-based version would take int *a,int *b and exchange *a,*b. A return value is not essential for that alternative.

Original paper, page 1

1 mark · MCQ
Report issue

Activation records and actual calls

Functions & Parameter Passing

Question 1.17, GATE 2002: Select the correct statement about C.
(A) At most one activation record lies between the current record and main’s record.
(B) The number of activation records between the current record and main’s record depends on the actual sequence of function calls.
(C) Visibility of global variables depends on the actual sequence of function calls.
(D) Before a recursive call, the recursive function’s activation record must be saved on another stack.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step 1: Trace a concrete call chain.

Step 2: If main calls f and f calls g, the active stack contains main,f,g. If g recursively calls itself, another g activation is needed while the earlier one remains active. Thus the number of intervening records changes with actual calls. Lexical visibility is determined by declarations and scopes, not by this stack depth.

Conclusion: The runtime activation chain follows the actual sequence of function calls. Its depth between the current function and main therefore depends on that sequence, B. C uses lexical rather than dynamic scope for global names, and recursive calls can use successive records on the same runtime stack.

Original paper, page 2

Descriptive
Report issue

Trace call by value

Functions & Parameter Passing

Question 15(i), GATE 1999: For this program, determine printed a under call by value:
procedure P(x,y,z);
begin y := y+1; z := x+x end;
begin
  a := 2; b := 3;
  P(a+b,a,a);
  print(a)
end.
The initialization a:=2 is supplied by GFG; GateForum omits it.
Question 15(i), GATE 1999: For this program, determine printed a under call by value:
procedure P(x,y,z);
begin y := y+1; z := x+x end;
begin
  a := 2; b := 3;
  P(a+b,a,a);
  print(a)
end.
The initialization a:=2 is supplied by GFG; GateForum omits it.
Question 15(i), GATE 1999: For this program, determine printed a under call by value:
procedure P(x,y,z);
begin y := y+1; z := x+x end;
begin
  a := 2; b := 3;
  P(a+b,a,a);
  print(a)
end.
The initialization a:=2 is supplied by GFG; GateForum omits it.
Tap to enlarge
Answer & solution

Answer: 2 (no official key was published)

At entry the actual expression a+b is evaluated to 5. Call by value creates three independent local cells x=5, y=2 and z=2. The first assignment changes only local y from 2 to 3. The second computes local z=x+x=10. Neither assignment writes caller a; the local cells disappear when P returns. Hence caller a remains 2 and print(a) outputs 2. The two occurrences of actual a do not create aliases in call by value.

Original paper, page 8

Descriptive
Report issue

Trace reference passing with an expression

Functions & Parameter Passing

Question 15(ii), GATE 1999: Determine printed a under call by reference for:
procedure P(x,y,z);
begin y := y+1; z := x+x end;
begin
  a := 2; b := 3;
  P(a+b,a,a);
  print(a)
end.
Use GFG’s visible a:=2 initialization, absent in GateForum, and discuss passing the expression a+b by reference.
Question 15(ii), GATE 1999: Determine printed a under call by reference for:
procedure P(x,y,z);
begin y := y+1; z := x+x end;
begin
  a := 2; b := 3;
  P(a+b,a,a);
  print(a)
end.
Use GFG’s visible a:=2 initialization, absent in GateForum, and discuss passing the expression a+b by reference.
Question 15(ii), GATE 1999: Determine printed a under call by reference for:
procedure P(x,y,z);
begin y := y+1; z := x+x end;
begin
  a := 2; b := 3;
  P(a+b,a,a);
  print(a)
end.
Use GFG’s visible a:=2 initialization, absent in GateForum, and discuss passing the expression a+b by reference.
Tap to enlarge
Answer & solution

Answer: 10 under expression-temporary convention; strict variable-reference semantics reject the call. (no official key was published)

There is a language-semantic qualification: an ordinary assignable reference parameter cannot bind to the non-variable expression a+b. In a strict Pascal-style var-parameter language this call is invalid, so no output is defined. The customary exercise convention evaluates a+b once into an anonymous temporary t=5 and binds x to t, while both y and z refer directly to caller a. Then y:=y+1 changes a from 2 to 3. Next z:=x+x reads t twice, obtains 10, and writes a=10. Thus the intended trace under that convention prints 10. It does not print 12: reevaluating a+b after a changes would be call-by-name behavior, not a reference to the original temporary. The convention and the secondary initialization must both be stated rather than silently assumed.

Original paper, page 8

Report issue

Expose copy-restore aliasing

Functions & Parameter Passing

Question 15(iii), GATE 1999: Determine printed a under call by copy-restore for:
procedure P(x,y,z);
begin y := y+1; z := x+x end;
begin
  a := 2; b := 3;
  P(a+b,a,a);
  print(a)
end.
GFG supplies a:=2, which is missing in GateForum. No copy-back order is specified.
Question 15(iii), GATE 1999: Determine printed a under call by copy-restore for:
procedure P(x,y,z);
begin y := y+1; z := x+x end;
begin
  a := 2; b := 3;
  P(a+b,a,a);
  print(a)
end.
GFG supplies a:=2, which is missing in GateForum. No copy-back order is specified.
Question 15(iii), GATE 1999: Determine printed a under call by copy-restore for:
procedure P(x,y,z);
begin y := y+1; z := x+x end;
begin
  a := 2; b := 3;
  P(a+b,a,a);
  print(a)
end.
GFG supplies a:=2, which is missing in GateForum. No copy-back order is specified.
Tap to enlarge
Answer & solution

Answer: 10 if y restores before z; 3 if z restores before y; no unique output without a convention. (no official key was published)

Copy-in gives independent local values x=5, y=2, z=2. The body ends with x=5, y=3, z=10. Both y and z have the same copy-back destination, caller a, so restoration order matters. Restoring y then z sets a first to 3 and then to 10, printing 10. Restoring z then y sets a first to 10 and finally to 3, printing 3. Thus the given program has no uniquely determined copy-restore result unless the language specifies the order. A left-to-right formal-parameter restoration convention yields 10. As in reference passing, the non-variable actual a+b requires an expression temporary or a rule permitting input-only expressions; strict copy-in/copy-out parameters requiring assignable actuals reject the call. Under the temporary convention restoring unchanged x does not affect a. This is aliasing at copy-back, not aliasing between the local parameter cells.

Original paper, page 8

Report issue

Count value-parameter procedure calls

Functions & Parameter Passing

Question 10(a)(ii), GATE 1997: Consider this pseudo-Pascal program, where z is global:
program what;
var z:integer;
procedure recur(x);
begin
  if x<=40 then begin
    x:=x+z;
    recur(x);
    z:=x+10
  end
end;
begin
  z:=10;
  recur(z);
  writeln(z)
end.
If x is passed by value, how many times is recur called, including the call from the main program?
Question 10(a)(ii), GATE 1997: Consider this pseudo-Pascal program, where z is global:
program what;
var z:integer;
procedure recur(x);
begin
  if x<=40 then begin
    x:=x+z;
    recur(x);
    z:=x+10
  end
end;
begin
  z:=10;
  recur(z);
  writeln(z)
end.
If x is passed by value, how many times is recur called, including the call from the main program?
Question 10(a)(ii), GATE 1997: Consider this pseudo-Pascal program, where z is global:
program what;
var z:integer;
procedure recur(x);
begin
  if x<=40 then begin
    x:=x+z;
    recur(x);
    z:=x+10
  end
end;
begin
  z:=10;
  recur(z);
  writeln(z)
end.
If x is passed by value, how many times is recur called, including the call from the main program?
Tap to enlarge
Answer & solution

Answer: 5 total calls, of which 4 are recursive calls. (no official key was published)

Step through it (13 steps)

At every recursive descent, the global z is still 10, since z is assigned only after recur(x) returns. Each active call therefore adds 10 to its own copy of x and invokes the next call. The entry arguments are

Main → recur(10) → recur(20) → recur(30) → recur(40) → recur(50).

The first four entries satisfy x≤40 and execute the recursive-call statement. The last entry, with x=50, fails the test and returns without another call. It is nevertheless an invocation and must be included when counting how many times recur is called. Thus there are five calls in total, four of them made recursively by the procedure itself.

A count of four confuses the number of executed recursive-call statements, or the number of nonbase activations, with the total requested calls. A count based on doubling x would instead be using reference semantics, in which x aliases z; that is not this subpart. The literal ≤ is also essential: a call with argument 40 recurses once more. Original10(a)(ii) is a separate normalized leaf, but neither source prints its own mark allocation, so its marks remain null.

Original paper, page 7

2 marks · Descriptive
Report issue

Trace reference-parameter recursion

Functions & Parameter Passing

Question 10(b), GATE 1997: Consider this pseudo-Pascal program, where z is global:
program what;
var z:integer;
procedure recur(x);
begin
  if x<=40 then begin
    x:=x+z;
    recur(x);
    z:=x+10
  end
end;
begin
  z:=10;
  recur(z);
  writeln(z)
end.
If x is passed by reference, what is printed?
Question 10(b), GATE 1997: Consider this pseudo-Pascal program, where z is global:
program what;
var z:integer;
procedure recur(x);
begin
  if x<=40 then begin
    x:=x+z;
    recur(x);
    z:=x+10
  end
end;
begin
  z:=10;
  recur(z);
  writeln(z)
end.
If x is passed by reference, what is printed?
Tap to enlarge
Answer & solution

Answer: 110. (no official key was published)

Step through it (11 steps)

The main program calls recur(z), so its reference parameter x is another name for the global variable z. Passing that reference onward as recur(x) keeps the same alias in every activation. There are not separate saved integer values of x in the suspended calls.

Initially x=z=10. The first body executes x:=x+z, doubling the shared variable to 20. The next call doubles it to 40. Because the guard is x≤40, the call seeing 40 also executes the body and doubles it to 80. The next call sees 80 and returns immediately. Thus four activations exist, three with a pending final assignment.

On unwinding, every z:=x+10 reads the current shared value, because x aliases z. The three pending assignments make the values 80→90→100→110. The program consequently prints 110.

It would be incorrect to use the descent values 10,20,40 as saved local copies during unwinding: that is pass-by-value behavior. It would also be incorrect to stop at 80, since the three assignments following recursive calls still have to execute. The example shows why mutation of a reference argument can interact unexpectedly with a global variable naming the same storage location.

Original paper, page 7

Descriptive
Report issue

Reference parameter aliasing

Functions & Parameter Passing

Question 19(i), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by reference.
Question 19(i), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by reference.
Question 19(i), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by reference.
Tap to enlarge
Answer & solution

Answer: point 1: 5 5; point 2: 10 10; point 3: 10. (no official key was published)

A reference parameter is another name for the caller's variable, not a private copy of its value. Here the actual parameter is the global b, so a and b designate the same storage throughout test1. Initially that cell contains 3. Executing a:=5 updates the cell itself, which means both a and b now read as 5. Consequently the first output is point 1: 5 5.

Next test2 executes b:=10. It refers to the global b by the displayed lexical scope; this is the same cell already aliased by a. Therefore both reads at the second output produce 10, giving point 2: 10 10. Returning from test1 requires no copy-back for a reference parameter. The global value remains 10, and the final output is point 3: 10.

The important reasoning is storage identity: a:=5 changes b immediately, and test2's assignment to b changes what a observes immediately. The procedure nesting does not create a second b or hide the global declaration. Spaces and line breaks may depend on Pascal's output formatting; the value sequences, in order, are (5,5), (10,10), and (10).

Original paper, page 9

Descriptive
Report issue

Value-result parameter copy-back

Functions & Parameter Passing

Question 19(ii), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by value-result (copy-in/copy-out).
Question 19(ii), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by value-result (copy-in/copy-out).
Question 19(ii), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by value-result (copy-in/copy-out).
Tap to enlarge
Answer & solution

Answer: point 1: 5 3; point 2: 5 10; point 3: 5. (no official key was published)

Value-result creates a separate local parameter cell. At entry to test1, the current actual value b=3 is copied into a. The assignment a:=5 changes only that private cell; the global b remains 3. Thus the first output is point 1: 5 3.

The call to test2 then assigns 10 to the global b. It does not refer to the private parameter a of its caller, so a stays 5. The second output is consequently point 2: 5 10. This is the observable difference from call-by-reference: a is not an alias for b during the call.

At normal return from test1, the result phase copies the final local a back to the original actual parameter location b. That overwrites test2's value 10 with 5. The main program therefore prints point 3: 5. The chronological storage states are (a,b)=(3,3) at entry, (5,3) after assigning a, (5,10) after test2, and b=5 after copy-out. No conflict among multiple value-result actuals arises because the procedure has just one parameter. If it were ordinary call-by-value instead, there would be no final copy-back and point 3 would show 10; the word “result” is essential.

Original paper, page 9