The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+12 votes

What is the result of the following program?

    program side-effect (input, output);
    var x, result: integer;
    function f (var x:integer:integer;
  1. $5$
  2. $25$
  3. $36$
  4. $42$
asked in Programming by Veteran (59.5k points)
edited by | 1.2k views

2 Answers

+8 votes
Best answer
Call by value: $36$,
Call by reference: undefined behaviour for C/C++ but $42$ for languages having $*$ as a sequence point.

$f(x) * f(x);$

If the value of $x$ is being modified inside the function (call by reference) we cannot be sure if this modified value or the old value will be passed as argument for the second call to $f()$. This is because left and right operand of any arithmetic expression in C/C++ can be evaluated in any order. For languages like Java, strict left-right order is maintained.
answered by Veteran (355k points)
edited by
Arjun sir,

1. If we pass by value then ,x is a local variable in function f.So even if it increments global remains same.I am not getting how you are getting 36 for call by value?Also, what if f=x statement means?
yes, global x remains same but the local x in f gets incremented. "f = x" means the function f is returning the value x.
No need to worry about such notations-- it will be pretty clearly stated if asked now.
ok.Thanks for explanation sir.
Sir,one more thing if you can help. For the call by reference case,the undefined ouput will be from 36 or 42 or it can be anyhting?
If it is call by reference then 49 will be ans
in case of call by value 36 will ans
how 49 srestha?
If it is call by reference


then f is also 7

then result f(x)*f(x)=7*7=49

Same location pointed all time na?
and x is 5 here
yes x=5

In time of 1st function call,

then increment x+1, it becomes 6 and assign inside x

f also points to value 6

Now again function called

x becomes 7

f also points to memory location of x

Now multiplication happened

So, f(x) is pointing to same memory location of x i.e.7

Now, 7*7=49

is it ok now?
okay i got your explanation for 47 , now i have doubt about evaluation of f(x)*f(x)

why can't  first f(x) evaluates first and gives 6 and then second f(x) gives 7 , eventually 42?


I tried to run this program in pascal but it is showing fatal error.

how will call by reference work here?

+4 votes

Answer is (d) :

Why? Since in question syntax is like pascal we will assume that this segment follows rules of Pascal programming language. Then , there is rule in pascal that if in declaration of formal parameters keyword 'var' is prefixed , then the procedure follows call by reference and modifies the actual values of actual params. So  the answer is 42.

answered by (55 points)
Whatever you're saying is correct. can't comment about 98 but now GATE don't ask individual language base questions...they will explicitly mention about parameter passing.

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

37,939 questions
45,453 answers
48,208 users