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?

edited | 16 views