DOC PREVIEW
UA CSC 520 - Control Structures - Parameters

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 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 25 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 25 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 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Parameter Passing -- Value ParametersParameter Passing -- Reference ParametersCall-by-Value ParametersCall-by-Reference ParametersCall-by-Name ParametersCall-by-Name Parametersldots Call-by-Name Parametersldots Call-by-Name -- ImplementationCall-by-Name -- Jensen's DeviceLarge Value ParametersLarge Value Parametersldots Parameter PassingParameter Passingldots Parameter Passingldots Parameter Passingldots Parameter Passing in AdaParameter Passing in Adaldots Parameter Passing in Adaldots Parameter Passing in Adaldots Exam Problem 415.330/96 (A)Exam Problem 415.330/96 (B)HomeworkReadings and ReferencesSummary520 —Spring 2008 — 28CSc 520Principles of ProgrammingLanguages28 : Control Structures — ParametersChristian [email protected] of Computer ScienceUniversity of ArizonaCopyrightc 2008 Christian Collberg[1]520 —Spring 2008 — 28Parameter Passing – Value ParametersPROG M;PROC P(X:INT);BEGINX:=5END P;VAR S:INT;BEGINS:=6;P(S);END.X=5oP(6)S=6oMValue parameters are (usually) copied by the caller intothe callee’s activation record. Changes to a formalwon’t affect the actual.[2]520 —Spring 2008 — 28Parameter Passing – Reference ParametersPROG M;PROC P(VAR X:INT);BEGINX:=5END P;VAR S:INT;BEGINS:=6;P(S);END.X=5oP(6)S=5oMReference parameters are passed by passing theaddress (location, l-value) fo the parameter. Changes toa formal affects the actual also.[3]520 —Spring 2008 — 28Call-by-Value Parameters1. The caller computes the arguments’ r-value.2. The caller places the r-values in the callee’s activationrecord.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) END[4]520 —Spring 2008 — 28Call-by-Reference Parameters1. The caller computes the arguments’ l-value.2. Expression actuals (like a + b) are stored in a newlocation.3. The caller places the l-values in the callee’s activationrecord.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) END[5]520 —Spring 2008 — 28Call-by-Name Parameters(Un-)popularized by Algol 60.A name parameter is (re-)evaluatedevery time it is referenced,in the callers environment.Algorithm:1. The caller passes a thunk, a function which computesthe argument’s l-value and/or r-value, to the callee.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 link ispassed to the thunk.[6]520 —Spring 2008 — 28Call-by-Name Parameters...Algorithm:4. If the parameter is used as an l-value, the thunk shouldreturn an l-value, otherwise an r-value.5. If the parameter is used as an l-value, but the actualparameter has no l-value (it’s a constant), the thunkshould produce an error.Consequences:Every time a callee references a name parameter, itmay produce a different result.[7]520 —Spring 2008 — 28Call-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];ENDx := 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]520 —Spring 2008 — 28Call-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];END[9]520 —Spring 2008 — 28Call-by-Name – Jensen’s DevicePROC Sum (NAME Expr:REAL; NAME Idx:INTEGER;Max:INTEGER):INTEGER;VAR 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*)END[10]520 —Spring 2008 — 28Large Value ParametersLarge value parameters have to be treated specially, sothat a change to the formal won’t affect the actual.Example:TYPE T = ARRAY [1..1000] OF CHAR;PROCEDURE P (x : T);BEGINx[5] := "f";END P;VAR L : T;BEGINP(L);END.[11]520 —Spring 2008 — 28Large Value Parameters...Algorithm 1: Callee Copy Algorithm 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);END[12]520 —Spring 2008 — 28Parameter PassingIn Pascal, parameters are passed either by value or byreference (if the formal is preceeded by the keywordvar).In C, all parameters are passed by value. Pass byreference can be simulated by explicitly passing theaddress of a variable:swap(&x,&y).In FORTRAN, all parameters are passed by reference.A programmer can simulate pass-by-value by explicitlymaking a local copy of an argument.Unlike most languages, FORTRAN allows r-values tobe passed by reference:swap(3+4,7*x). Thecompiler creates a temporary variable to hold the value.[13]520 —Spring 2008 — 28Parameter Passing...In Java, object references are transfered usingpass-by-sharing. This means that the actual and formalwill refer to the same object. The compiler simplypasses the address of the object.In Java, primitive types are passed by value.[14]520 —Spring 2008 — 28Parameter Passing...In Pascal and Modula-2 a programmer would usecall-by-value toensure that the callee cannot modify the actualargument.In Pascal and Modula-2 a programmer would usecall-by-reference toensure that the callee can modify the actual argument,or tomake sure that a large parameter (which semanticallyshould be passed by value) is not copied. (This is donefor efficiency reasons).[15]520 —Spring 2008 — 28Parameter Passing...Modula-3 provides a READONLY parameter mode. AREADONLY formal parameter cannot be changed by thecallee. The formal1. cannot be on the left-hand-side of an assignmentstatement, and2. cannot be passed by reference to another routine.Small READONLY parameters are passed by value.Large READONLY parameters are passed by reference.[16]520 —Spring 2008 — 28Parameter Passing in AdaAda has three modes:1.in-parameters pass information from the caller to thecallee. The callee cannot write to them.2.out-parameters pass information to the callee from thecaller. The callee can read and write them. They


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?