I am learning parameter passing.
I've been working on a question about parameter passing, but I don't understand one problem.
I searched the Internet, but there was no example of a parameter being expression.
If it's a expression, can I know how to evaluate pass-by-name?
The problem is as follows.
function func(int a, int b, int c)
begin
a := b + c;
b := c + 1;
print a, b, c;
end
function main
begin
int x := 10;
int y := 5;
ink z := 15;
func(x, y, y + z);
print x, y, z;
end
From Wikipedia: (note that "call by name" and "pass by name" mean the same thing.)
So for your function, we should make the substitutions
a → x,b → yandc → y + zaccordingly:I've put brackets around the places where
cis substituted, to make clear that when the expression is "copy/pasted" into the function, it doesn't change the precedence of the other operations; for example,3 * cwould be equivalent to3 * (x + y), not3 * x + y.The fact that
cis replaced by the expressiony + zrather than a simple variable, doesn't create any problems here, becausecnever appeared on the left side of an assignment statement.