Parameter passing modes in programming. What are the values printed in each of t
ID: 3780326 • Letter: P
Question
Parameter passing modes in programming. What are the values printed in each of the four cases ?
What is the value printed by the following pseudo-code program for each of the four parameter passing modes call-by-value, call-by-reference, call-by-value result, and call-by-name? That is, for each of the four parameter passing modes assume that both the x and y parameters use that mode. Show how each program is evaluated in that mode and show the final value printed: a: integer//a global variable procedure p(in out x: integer, in y: integer) x: = x + 1 a: = y begin//main program a: = 2 p(a, a) write_integer(a) endExplanation / Answer
The value printed by call-by-value : 2
p(2,2) would produce x = 3, a=2, which results in 2.
The value printed by call-by-reference:3
p(a,a) would produce x = 3 ( where x is a) then (a is assigned with y(which holds address a) in the previous statemnt a is increemnt to 3. so the output would be 3.)
The value printed by call-by-value-result:2
This is special case of call-by-reference which would give to instances to p(a,a), here a,a are treated differently.
Hence the output would be 2.
The value printed by call-by-name:2
call-by-name is sometimes treated as call-by-value, p(x=a,y=a) is call-by-name,
the output is 2.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.