The superscript "(d)" stands for "difficult". Exercises similar to those marked with "(d)" might appear in candidacy exams, but not in the standard exams of CSE 428.
The superscript "(s)" stands for "solution provided". You can find the solution of these exercises here.
z : integer; procedure p (x:integer) < body >;Write a < body > for p so that the following piece of code
z := 1; p(z); write(z)will have all different outcomes in case of pass by value, pass by reference, and pass by value-result.
procedure p(value-result x, y : integer); < body >If we have a language that does not allow call by value-result, but only call by reference, how can we define the procedure p so that it has the same effect as the above one?
Hint: To see the difference between call by value-result and call by reference, consider, for example, the case in which the body consists of the commands
x:=x+1; y:=y+1and the call associates x and y to the same actual parameter.
Another case in which we observe a difference is when one of the actual parameters (say, z) occurs in the body as a non-local (or global) parameter, and the body contains instructions which modify z.
procedure p; x: integer; procedure q; begin x := x+1 end; procedure r; x: integer; begin x := 1; q; write(x) end; begin x:= 2; r end;What is the output produced by calling p in
#include < stdio.h > int x=0; void p(int,int); void main(){ int x = 1; p(x,x); } void p(int y, int z){ x = x+1; y = y+1; z = z+1; printf("%d\n",x+y+z); }
var p,q : ^integer; begin new(p); q := p; p^ := 1; q^ := p^ + 1; write(p^); dispose(p); foo; write(q^) endwhere foo is a procedure call (whose body does not contain occurrences of either p or q).
void p() {int x; q = &x; *q = 1;}where q is a global variable declared as pointer to int and initialized by q = new int;. What output can we expect to be produced by the following piece of code:
*q = 0; p(); r(); printf("%d\n",*q);where r is a procedure which does not mention q.