CS 342 Lecture -*- Outline -*- * Parameter Passing mechanisms of Algol 60 ** Pass by value (call by value) copy value, result is not copied back ------------ integer procedure sqrt(n); value n; integer n; ------------ *** advantages/disadvantages -used for input, but can be assigned (so must make copy) -inefficient to make copy of actual for arrays *** variation: call by constant (not in Algol 60) copy value, result is not copied back, but not allowed to assign to formal! -can pass reference for arrays (have to be careful of aliases) ** Pass by name (call by name) pass actual argument expression (name), remember to evaluate in environment of (orig.) caller, if actual is a by-name parameter, pass that parameter *** example (draw contour diagram) ------------- begin procedure inc(n); integer n; begin integer temp; temp := n + 1; n := temp end; procedure inc2(n); integer n; begin inc(n); inc(n); end; begin integer i, temp; integer array A[1:100]; temp := 1; for i := 1 step 1 until 100 do A[i] := i; i := 1; inc(i); comment stmt 1.4; inc(A[i]); comment stmt 1.5; inc2(temp); comment stmt 1.6; inc2(A[temp]); comment stmt 1.7; end end ------------- *** copy rule (from Algol report section 4.7.3) copy the text of the procedure (in a block), replacing formals with actuals (in parentheses), changing internal names to avoid conflicts with actuals (avoid capture) changing intervening declarations of nonlocals to the proc to allow access -substituting actual parameter expression in procedure body doesn't always work, since scope won't always be right, even with renaming. (e.g. procedure parameters) (show contours for this) *** variation: call by text (not in Algol 60) pass text of actual expression, evaluate in called program's environment. (work above example with dynamic scoping). *** Call by name can implement nonstrict procedures the actual does not have to be defined if not used! ------------------------ comment ** strictness; begin integer procedure IfExp(b,e1,e2); value b; boolean b; integer e1,e2; begin if b then IfExp := e1 else IfExp := e2 end; integer procedure loop; begin loop := loop end; PrintInt(IfExp(true, 2, loop)); PrintInt(IfExp(false, loop, 3)) end ------------------------ (compare call by value, reference, etc.) **** Strictness: a function is strict if it uses all its arguments. informally: if it uses all its arguments -call by name allows one to write nonstrict functions **** Jensen's device altering an index makes an array element name range over all elements of an array. ------------- comment ** Jensen's device; begin real procedure Sum(k,ak,n); value n; integer k; real ak; begin real S; S := 0; for k := 1 step 1 until n do S := S + ak; Sum := S end; real array one[1:100], two[1:100,1:50]; integer i,j; PrintReal(Sum(j, a[j], 100)); PrintReal(Sum(i, Sum(j,two[i,j],50), 100)) end ------------- (explain how this works) *** implementation of call by name -cannot use copy rule (why not?) Thunk: routine used to compute address of actual when passed by name. a thunk is a kind of closure: pair of code and environment