Awesome q2a theme
0 votes
16 views

https://gateoverflow.in/960/gate2003-73

In this question it is said that Call-by-Need works the same way as Call-by-Name with the difference that the evaluation of the parameter reference is done the first time it is encountered in the called function.


global int i = 1, j = 2;
P(x){
print(x*10+1)
i = 2
print(x)
}

main(){
P(i+j)
}

In this above code Call-by-Name will replace the parameter x everywhere and evaluate every time separately as shown below,

global int i = 1, j = 2;
P(x){
print(i+j*10+1) // x will be replaced by i+j
i = 2
print(i+j) // x will be replaced by i+j
}

main(){
P(i+j)
}

Output: 22 4


My doubt is how will the operations be done in Call-by-Need for the same code. Will there be replacement and then evaluation or when we encounter x it will be calculated and saved as 3 and rest of the time x = 3 will only be used producing the output 31 3?

in Programming by (333 points)
edited by | 16 views

Please log in or register to answer this question.

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
Welcome to GATE CSE Doubts, where you can ask questions and receive answers from other members of the community.
8,437 questions
2,714 answers
13,238 comments
95,460 users