【正文】
s by reference passes the location of the variable, so that the parameter bees an alias for the argument. ? Any changes made to parameter occur to the argument as well. ? This is the only parameter passing in FORTRAN77. void inc2(int amp。 x) { ++x。 ++x。} /* correct */ inc2(y)。 Compiler Theory Fall 2021 Jianhui Yue Pass by Reference (cont) ? The piler must – Compute the address of the argument – Store it in the local activation record – Turn local accesses to a reference parameter into indirect accesses ? No copy of the passed value is made. – This is significant when the parameter is a large structure. Compiler Theory Fall 2021 Jianhui Yue Pass by ValueResult ? Similar to pass by reference. ? No actual alias is established – The value of the argument is copied and used in the procedure – On exit the final value of the parameter is copied back out to the location of the argument. ? This method is known as copyin, copyout or copyrestore. void p(int x, int y) { ++x。 ++y。} main () { int a=1。 p(a,a)。 return 0。} a has value of 3 after p is called if pass by reference is used. a has value of 2 after p is called if pass by valueresult is used. Compiler Theory Fall 2021 Jianhui Yue Pass by Name ? The idea is that the argument is not evaluated until its actual use in the called program. ? The name of the argument replaces the name of the parameter it corresponds to. int i。 int a[10] void p(int x) { ++i。 ++x。} main() { i=1。 a[1]=1。 a[2]=2 p(a[i])。 return 0。 } a[2] is set to 3 a[1] is unchanged Compiler Theory Fall 2021 Jianhui Yue Pass by Name (cont) ? Pass by name was offered in Algol60, but bee unpopular. 1. It can give surprising and counterintuitive results in the presence of the side effects. 2. It is difficult to implement – Each argument must be turned into the procedure 3. It is inefficient ? A variation on this mechanism called lazy evaluation bee popular in purely functional languages (Miranda, Haskell) Compiler Theory Fall 2021 Jianhui Yue Homework ? ,