DOC PREVIEW
UA CSC 520 - Control Structures — Parameters

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSc 520 — Principles of Programming Languages28 : Control Structures — ParametersChristian CollbergDepartment of Computer ScienceUniversity of [email protected] 2008 Christian CollbergMarch 31, 20081 Parameter Passing – Value Parameters1mmPROG M;PROC P(X:INT);BEGINX:=5END P;VAR S:INT;BEGINS:=6;P(S);END.X=5P(6)S=6M• Value parameters are (usually) copied by the caller into the callee’s activation record. Changes to aformal won’t affect the actual.2 Parameter Passing – Reference Parameters1mmPROG M;PROC P(VAR X:INT);BEGINX:=5END P;VAR S:INT;BEGINS:=6;P(S);END.X=5P(6)S=5M• Reference parameters are passed by passing the address (location, l-value) fo the parameter. Changesto a formal affects the actual also.13 Call-by-Value Parameters1. The caller computes the arguments’ r-value.2. The caller places the r-values in the callee’s activation record.• The caller’s actuals are never affected by the call.• Copies may have to be made of large structures.TYPE T = ARRAY 10000 OF CHAR;PROC P (a:INTEGER; b:T);BEGIN a:=10; b[5]:="4" END P;VAR r : INTEGER; X : T;BEGIN P(r, X) END4 Call-by-Reference Parameters1. The caller computes the arguments’ l-value.2. Expression actuals (like a + b) are stored in a new lo cation.3. The caller places the l-values in the callee’s activation record.• The caller’s actuals may be affected by the call.TYPE T = ARRAY 10000 OF CHAR;PROC P (VAR a:INT; VAR b:T);BEGIN a:=10; b[5]:="4" END P;VAR r : INTEGER; X : T;BEGIN P(5 + r, X) END5 Call-by-Name Parameters• (Un-)popularized by Algol 60.• A name parameter is (re-)evaluated– every time it is referenced,– in the callers environment.Algorithm:1. The caller passes a thunk, a function which computes the argument’s l-value and/or r-value, to thecallee.2. The caller also passes a static link to its environment.3. Every time the callee references the name parameter, the thunk is called to evaluate it. The static linkis passed to the thunk.26 Call-by-Name Parameters. . .Algorithm:4. If the parameter is used as an l-value, the thunk should return an l-value, otherwise an r-value.5. If the parameter is used as an l-value, but the actual parameter has no l-value (it’s a constant), thethunk should produce an error.Consequences:• Every time a callee references a name parameter, it may produce a different result.7 Call-by-Name Parameters. . .VAR i : INTEGER; VAR a : ARRAY 2 OF INTEGER;PROCEDURE P (NAME x:INTEGER);BEGINi := i + 1; x := x + 1;END;BEGINi := 1; a[1] := 1; a[2] := 2;P(a[i]);WRITE a[1], a[2];END• x := x + 1 becomes a[i] := a[i] + 1.• Since i is incremented before x, we get a[2] := a[2] + 1. ⇒ Print 1,3.8 Call-by-Name – ImplementationPROCEDURE P (thunk : PROC());BEGINi := i + 1; thunk()↑ := thunk()↑ + 1;END;PROCEDURE thunk1 () : ADDRESS;BEGIN RETURN ADDR(a[i]) END;BEGINi := 1; a[1] := 1; a[2] := 2;P(thunk1);WRITE a[1], a[2];END9 Call-by-Name – Jensen’s DevicePROC Sum (NAME Expr:REAL; NAME Idx:INTEGER;Max:INTEGER):INTEGER;3VAR Result : REAL := 0;BEGINFOR k := 1 TO Max DO;Idx := k; Result := Result + Expr;ENDFOR;RETURN Result;END;VAR i : INTEGER;BEGINWRITE Sum(i, i, 5); (*P5i=1i *)WRITE Sum(i*i, i, 10); (*P10i=1i2*)END10 Large Value Parameters• Large value parameters have to be treated specially, so that a change to the formal won’t affect theactual. Example:TYPE T = ARRAY [1..1000] OF CHAR;PROCEDURE P (x : T);BEGINx[5] := "f";END P;VAR L : T;BEGINP(L);END.11 Large Value Parameters. . .Algorithm 1: Callee CopyAlgorithm 2: Caller CopyPROCEDURE P (VAR x : T);VAR xT : T;BEGINcopy(xT,x,1000);xT[5]:="f";END P;VAR L : T;BEGINP(L);ENDPROCEDURE P (VAR x : T);BEGINx[5] := "f";END P;VAR L : T;VAR LT : T;BEGINcopy(LT, L, 1000);P(LT);END12 Parameter Passing• In Pascal, parameters are passed either by value or by reference (if the formal is preceeded by thekeyword var).• In C, all parameters are passed by value. Pass by reference can be simulated by explicitly passing theaddress of a variable: swap(&x,&y).4• In FORTRAN, all parameters are passed by reference. A programmer can simulate pass-by-value byexplicitly making a local copy of an argument.• Unlike most languages, FORTRAN allows r-values to be passed by reference: swap(3+4,7*x). Thecompiler creates a temporary variable to hold the value.13 Parameter Passing. . .• In Java, object references are transfered using pass-by-sharing. This means that the actual andformal will refer to the same object. The compiler simply passes the address of the object.• In Java, primitive types are passed by value.14 Parameter Passing. . .In Pascal and Modula-2 a programmer would use call-by-value to• ensure that the callee cannot modify the actual argument.In Pascal and Modula-2 a programmer would use call-by-reference to• ensure that the callee can modify the actual argument, or to• make sure that a large parameter (which semantically should be passed by value) is not copied. (Thisis done for efficiency reasons).15 Parameter Passing. . .Modula-3 provides a READONLYparameter mode. A READONLY formal parameter cannot be changed bythe callee. The formal1. cannot be on the left-hand-side of an assignment statement, and2. cannot be passed by reference to another routine.• Small READONLY parameters are passed by value.• Large READONLY parameters are passed by reference.16 Parameter Passing in AdaAda has three modes:1. in-parameters pass information from the caller to the callee. The callee cannot write to them.2. out-parameters pass information to the callee from the caller. The callee can read and write them.They start out being uninitialized.3. in out-parameters pass information from the caller to the callee and back.517 Parameter Passing in Ada. . .For scalars and pointers, all modes should be implemented by copying values. Thus1. in-parameters are passed-by-value.2. out-parameters are passed-by-result (the formal is copied into the actual when the procedure re-turns).3. in out-parameters are passed-by-value/result (On entry, the actual is copied into the formal. Onreturn, the formal is copied back into the actual).18 Parameter Passing in Ada. . .For constructed types (records, arrays) an implementation is allowed to pass either values or addresses.• If an in out parameter is passed by address an assignment to the formal changes the actual immedi-ately.• If an in outparameter is passed by value an assignment to the formal will not affect the actual untilthe procedure returns


View Full Document

UA CSC 520 - Control Structures — Parameters

Documents in this Course
Handout

Handout

13 pages

Semantics

Semantics

15 pages

Haskell

Haskell

15 pages

Recursion

Recursion

18 pages

Semantics

Semantics

12 pages

Scheme

Scheme

32 pages

Syllabus

Syllabus

40 pages

Haskell

Haskell

17 pages

Scheme

Scheme

27 pages

Scheme

Scheme

9 pages

TypeS

TypeS

13 pages

Scheme

Scheme

27 pages

Syllabus

Syllabus

10 pages

Types

Types

16 pages

FORTRAN

FORTRAN

10 pages

Load more
Download Control Structures — Parameters
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Control Structures — Parameters and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Control Structures — Parameters 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?