Discussion:
Assignment to parameters
(too old to reply)
t***@engmail.uwaterloo.ca
2004-07-14 00:39:11 UTC
Permalink
Should our compiler reject procedures that try to assign values to their
parameters? For example, should we reject a file in which we see the following
procedure:

void myProc(int r, int a)
begin
r <- 5*a;
end;

If not, then how should calls to procedures such as this behave?

----------------------------------------
This mail sent through www.mywaterloo.ca
Danny Su
2004-07-14 00:38:18 UTC
Permalink
Parameters are passed by value, so any changes are only seen locally
within the procedure. So you should accept r <- 5 * a;
If you're implementing arrays, then it's passed by reference.

The result of such statement is that r now contains 5 * a for rest of the
procedure.

r <- 5 * a;
outn(r); # This should output whatever 5 * a is.
Troy Mark Gonsalves
2004-07-20 17:48:07 UTC
Permalink
You won't generate a compile error.

This explanation is probably going to confuse people more than help
them...just focus on the fact that if you're calling procedures properly
(copying arguments to the stack) then everything should work the way it's
supposed to....On with the explanation:

Procedures can use their parameters just like local variables, but the
effects of any changes are not visible outside of the call...That may
sound hard to implement, but as long as you're passing arguments using the
stack then you get this for free...any changes to the parameter are
ignored by the caller, since the caller just increments the stack pointer
when the procedure returns (i.e. the caller doesn't look at the
new values of the arguments it sent).
Post by t***@engmail.uwaterloo.ca
Should our compiler reject procedures that try to assign values to their
parameters? For example, should we reject a file in which we see the following
void myProc(int r, int a)
begin
r <- 5*a;
end;
If not, then how should calls to procedures such as this behave?
----------------------------------------
This mail sent through www.mywaterloo.ca
Loading...