Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456 notes11
DAT2 - SPO - Spring 2002
Lecture 11
Subjects:
Procedures and functions 1.
Readings:
Sehti, chap. 5, until p. 171
Notes:
Procedures and functions
"Yeah, but we do know what that is!"
Well, of course you do, but anyhow, there might be some details you would find interesting. For instance, there is a variety of methods to pass parameter to procedures or functions: call-by-value, call-by-reference, call-by-value-result and call-by-name.
Another interesting topic is dynamic scope. The opposite, static scope, is used in most languages nowadays, but actually its much easier to implement dynamic scope. But the problem is, that it is much harder for humans to understand what programs under static scope are doing. And likewise it is much harder for a compiler to figure out what programs are trying to do, and hence it is much more difficult to use static type checking.
Towards the end we will discuss nested scopes. This concept is useful in understanding inner classes in Java (and C++) and of course languages, which use nested scopes (Pascal, SML, Haskell and others).
Exercises:
In "The JavaTM Programming Language - Second Edition" by Ken Arnold and James Gosling, page 38-41, it is proposed that Java always uses call-by-value. How do parameters actually work in Java, and is this proposition justified?
Execute (by hand) the following program fragment. The fragment is written in a variant of Pascal, which has had added a call-by-name parameter-mechanism (parameters marked name where standard Pascal's call-by-reference parameters are marked var):
program
var x: Integer;
procedure foo(name y: Integer);
var i: Integer
begin
for i:=1 to 3 do writeln(y);
end;
function inc(var z: Integer): Integer;
begin
z := z+1;
inc := z;
end
begin
x := 0;
foo(inc(x));
end.
Use this example to characterise call-by-name parameters. What effects can be produced with call-by-name parameters, that cannot directly be obtained with call-by-value or call-by-reference? Simulate the example in C (hint: let y be a function pointer), try the experiment in Java (hint: let y be an object with a getValue method).
Dynamic scope may be described as an upward search through activation records on the stack. When one in the case of static scope wants to access a variable declared in a surrounding scope it is also necessary to locate an activation record further up on the stack. Examine these ways to access activation records in order to find similarities and differences.