C SC 520 Principles of Programming Languages 1 Principles of Programming Languages Lecture 06 Parameters.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Parameter Passing. Variables: lvalues and rvalues In the assignment statement “X=Y”, variables X and Y are being used in different ways. Y is being used.
Parameter Passing CMSC 331 Extracted with only very minor changes from
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
Subprograms A subprogram allows process abstraction (as opposed to data abstraction). Characteristics –single entry point –caller suspended until control.
Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.
CPSC 388 – Compiler Design and Construction Parameter Passing.
1 Parameter Passing (Section 8.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Advanced Programming Parameter Passing Giuseppe Attardi Università di Pisa.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Procedures and Control Flow CS351 – Programming Paradigms.
5-1 © 2004, D.A. Watt, University of Glasgow 5 Procedural Abstraction  Proper procedures and function procedures.  Parameters and arguments.  Implementation.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Parameters Chapter EighteenModern Programming Languages, 2nd ed.1.
Chapter EighteenModern Programming Languages1 Parameters.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
PSUCS322 HM 1 Languages and Compiler Design II Parameter Passing Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Parameter Passing. Expressions with lvalues and rvalues An expression has an lvalue/rvalue if it can be placed on the left/right side of an assignment.
Overview Parameter passing modes.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
Chapter 8 :: Subroutines and Control Abstraction
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CS 2104 Prog. Lang. Concepts Subprograms
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Runtime Environments Compiler Construction Chapter 7.
Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
Subprograms subroutines, procedures, functions, methods.
Programming Languages and Paradigms Imperative Programming.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.
1 Subroutines Structure of Programming Language. 2 Procedures and Functions There are two categories of subprograms Function: subroutine that returns.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
CS 536 Parameter Passing 1. Roadmap Last Time – Storing variables Locals, non-locals, globals This Time – Propagating values from one function to another.
CSI 3125, Subprograms, page 1 Subprograms General concepts Parameter passing Functions Subprograms as parameters.
CSC 8505 Compiler Construction Runtime Environments.
Chapter EighteenModern Programming Languages1 Parameters.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
CSE 3302 Programming Languages
C SC 520 Principles of Programming Languages 1 Principles of Programming Languages Lecture 10 Variables and Assignment.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Dr. Philip Cannata 1 Functions and Recursion Programming Languages.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
CS314 – Section 5 Recitation 9
Functions.
Principles of programming languages 4: Parameter passing, Scope rules
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Lecture 18 Arrays and Pointer Arithmetic
CSE 3302 Programming Languages
Assignments and Procs w/Params
Parameter transmission
PZ09B - Parameter transmission
Parameter transmission
Parameter transmission
Parameter transmission
Subprograms General concepts Parameter passing Functions
Parameter transmission
Presentation transcript:

C SC 520 Principles of Programming Languages 1 Principles of Programming Languages Lecture 06 Parameters

C SC 520 Principles of Programming Languages 2 Parameter Passing Modes Definitional Modes (call time binding) Call as constantCAC Call by referenceCBR Copying Modes (call time copying) Call by valueCBV Call by copyCBC  Copy-in/copy-out Call by value-resultCBVR Delay Modes (reference time copying) Call by textCBT Call by nameCBN Call by need  R-valueCBNeed-R  L-ValueCBNeed-L

C SC 520 Principles of Programming Languages 3 Example Program { local i,j local a[1..12] proc P( X, Y ) { local j j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y } a[1]=1; a[2]=2; … ; a[12]=12; i = 1 j = 3 P( i, a[i*j] ) print i print a[9] }

C SC 520 Principles of Programming Languages 4 Program Execution Sequence i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2// this j is local i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9]

C SC 520 Principles of Programming Languages 5 Call by text (macrosubstitution) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] rewrite proc body X -> i Y -> a[i*j] j = 2 // j == 2 i=i+1 // i == 2 print i2 i = i+2 //i == 4 print a[i*j] // a[4*2] 8 i--; print a[i*j]6 print i3 print a[9]9

C SC 520 Principles of Programming Languages 6 Call by name (copy rule) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] Rename locals j->j’ & rewrite X -> i Y -> a[i*j] j = 2 //j’ == 2 j == 3 i=i+1 //i == 2 print i2 i = i+2 //i == 4 print a[i*j]// a[4*3] 12 i--; print a[i*j]9 ! print i3 print a[9]9

C SC 520 Principles of Programming Languages 7 Algol 60 “Copy Rule” Name replacement (call by name) Any formal parameter not quoted in the value list is replaced, throughout the procedure body, by the corresponding actual parameter... Possible conflicts between identifiers inserted through this process and other identifiers already present within the procedure body will be avoided by suitable systematic changes of the formal or local identifiers involved... Finally the procedure body, modified as above, is inserted in place of the procedure statement [the call] and executed... –Report on the Algorithmic Language ALGOL 60, CACM, May 1960

C SC 520 Principles of Programming Languages 8 Algol 60 “Copy Rule” (cont.) integer procedure f(x); integer x; begin integer y; y := 1; f := x + y; end; begin integer y, w; y := 2; w := 10; w := f(y + w + 1); end; begin integer y,w; y :=2; w := 10; begin integer z; z := 1; w := (y+w+1)+z; end; Copy rule

C SC 520 Principles of Programming Languages 9 Algol 60 “Copy Rule” (cont.): Thunks thunk: a 0-arg. Procedure encapsulating actual argument plus environment of caller, called at site of each formal. Used to implement CBN and delayed evaluation in general integer procedure f(x); integer x; begin integer y; y := 1; f := x + y; end; begin integer y, w; y := 2; w := 10; w := f(y + w + 1); end; int function f(int x); { int y; y = 1; f = *x() + y; } { int y,w; int& function x() {int t=y+w+1;return &t;} y = 2; w = 10; w = f(y+w+1); }

C SC 520 Principles of Programming Languages 10 Call by name—implemented by thunk i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] thunk: 0-ary function encapsulating argument text int& X(){return &i} int& Y(){return &a[i*j]} j = 2 // j’ == 2 j == 3 i=i+1 // i == 2 print *X() 2 X()=*X()+2 //i == 4 print *Y() // a[i*j] == a[4*3] 12 i--; print *Y()9 ! print i3 print a[9]9

C SC 520 Principles of Programming Languages 11 Call by reference i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] lval (X) == lval (i) lval (Y) == lval (a[1*3]) j = 2 //j’ == 2 j == 3 i=i+1 //i == 2 print i2 i = i+2 //i == 4 print a[1*3] 3 i--; print a[1*3] 3 print i3 print a[9]9 aliased

C SC 520 Principles of Programming Languages 12 Call by value (copy-in) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] X = i //X == 1 Y = a[i*j])//Y == a[1*3] j = 2 //j’ == 2 j == 3 i=i+1 //i == 2 print X1 X = X+2 //i == 2 X == 3 print Y 3 i--; print Y3 print i1 print a[9]9

C SC 520 Principles of Programming Languages 13 Call by value–result (Algol W) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] X = i Y = a[i*j])//Y == a[1*3] j = 2 // j’ == 2 j == 3 i=i+1 // i == 2 print X1 X = X+2 //i == 2 X == 3 print Y 3 i--; print Y3 Use names of actuals for copy-out i = X //i == 3 a[i*j] = Y //a[3*3] == 3 print i3 print a[9]3

C SC 520 Principles of Programming Languages 14 Call by copy (copy-in/copy-out) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] px= lvalue (i) X=*px py= lvalue (a[1*3]) Y=*py j = 2 // j’ == 2 j == 3 i=i+1 // i == 2 print X 1 X = X+2 //i == 2 X == 3 print Y 3 i--; print Y3 Use original lvals for copy-out *px = X //i == 3 *py = Y //a[3] == 3 print i3 print a[9]9

C SC 520 Principles of Programming Languages 15 Call by need, r-value (“normal”, lazy) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] Use thunk once on 1 st ref to assign local; then use local int& X(){return &i} int& Y(){return &a[i*j]} j = 2 // j’ == 2 j == 3 i=i+1 // i == 2 print X = *X() 2 //X == 2 X = X+2 //i == 2 X == 4 print Y = *Y() // Y == a[i*j] == a[2*3] 6 i--; print Y6 print i1 print a[9]9

C SC 520 Principles of Programming Languages 16 Call by need, l-value i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] At 1 st ref, alias actual lval to formal int& X(){return &i} int& Y(){return &a[i*j]} j = 2 // j’ == 2 j == 3 i=i+1 // i == 2 lval (X) == X() // X aliases i print X //X == 2 2 X = X+2 //i == 4 X == 4 lval (Y) == Y() //Y aliases a[4*3] print Y // Y == a[12]} 12 i--; print Y12 print i3 print a[9]9

C SC 520 Principles of Programming Languages 17 Call by Name vs. Call by Need Assume X is the formal and e the corresponding actual expression CBN Delays evaluation of arguments past call until a reference to the formal Re-evaluates argument e on each reference to X in environment of caller No local variable X is allocated Implemented by call to thunk CB Need Delays evaluation of arguments past call until a reference to the formal Evaluates e on 1 st reference in environment of caller & loads local variable X ; no re-evaluation: subsequent references use local X Implemented by call to “memoized thunk”

C SC 520 Principles of Programming Languages 18 Call by constant i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y i--; print Y print i print a[9] const X = i const Y = a[1*3] j = 2 // j’ == 2 j == 3 i=i+1 // i == 2 print X1 X = X+2 error print Y (3) i--; print Y (3) print i (1) print a[9] (9)

C SC 520 Principles of Programming Languages 19 Modes Differ Definitional Modes (call time binding)example Call as constantCAC1 error Call by referenceCBR Copying Modes (call time passing) Call by valueCBV Call by copyCBC  Copy-in/copy-out Call by value-resultCBVR Delay Modes (reference time passing) Call by textCBT Call by nameCBN Call by need  R-valueCBNeed-R  L-ValueCBNeed-L

C SC 520 Principles of Programming Languages 20 Modes in Programming Languages Call as constantAlgol 68, C/C++ const Call by referencePascal var, Fortran, Cobol, PL/I, Ada arrays, C arrays Call by valueAlgol 60, Algol 68 (has ref x ), Simula, C, C++ (has & x), Pascal, PL/I, APL, LISP, Snobol4, Ada (scalar in variables) Call by copyAda (scalar out or in-out ) Call by value-resultAlgol W Call by textLISP Macros Call by nameAlgol 60, Simula Call by need, R-valueHaskell, Scheme (delay x) &(force x) Call by need, L-valueimperative + lazy eval?

C SC 520 Principles of Programming Languages 21 Ada Parameters Attempt at orthogonality of use vs implementation copy ref copy ref in outin-out in outin-out iplementationm implementation use scalar parameters composite Parameters (arrays &c) copy-incopy-outcopy-in-copy-out CBC required CBC/CBR option

C SC 520 Principles of Programming Languages 22 Arrays: by ref or by copy? a[] void function P(int x[]) { … x[i1] … x[i2]... x[i n ] … } n references to elements of formal x[] at RT ( n known at RT) Call by reference: copy a pointer to a[] into AR for P(a[]) Each reference x[i] becomes indirect address through pointer to original argument a[] in caller; loads and stores reflected immediately in original array Call by copy: copy argument a[] into AR for P(a[]) & overwrite original a[] from copy on return Each reference x[i] directly addresses copy ; stores and loads made to copy s elements

C SC 520 Principles of Programming Languages 23 by ref or copy? (cont.) Q calls P(a[]) by copy: # of memory references at RT: C = 4s + n Q P =x[i]... x[j]= Code of P(int x[]) a[] s n refs call return

C SC 520 Principles of Programming Languages 24 by ref or copy? (cont.) Q calls P(a[]) by reference: # of memory references at RT: Assume a[0] is at known fp +offset at CT R = 1 + 2n Q P =x[i]... x[j]= a[] s n Code of P(int x[])

C SC 520 Principles of Programming Languages 25 by ref or copy? (cont.) R > C  1 + 2n > 4s + n  n > 4s - 1  n  4s Reference density: n / s = ave. # references per element Copying less expensive than reference: R > C  n / s  4 n references s size R > C s = n/4 R  C

C SC 520 Principles of Programming Languages 26 More on Call By Name Most curious mode ever invented. Pros and Cons. CBN’s main characteristics: Defers evaluation of actual argument expressions Re-evaluates the argument expression at each use of formal  So arg value can actually change from one ref to another Can simulate lazy evaluation example: short circuit booleans boolean procedure cand(p,q); boolean p, q; if p then cand := q else cand := false; end Cannot be written as a procedure in an “eager” programming language (like Scheme)

C SC 520 Principles of Programming Languages 27 CBN (cont.) “Jensen’s Device” –weird sort of polymorphism Example: real procedure sigma(x, j, n); value n; real x; integer j, n; begin real s; s := 0; for j := 1 step 1 until n do s := s + x; sigma := s end sigma( a(i), i, 3)  a(1) + a(2) + a(3) sigma( a(k)*b(k), k, 2)  a(1)*b(1) + a(2)*b(2)

C SC 520 Principles of Programming Languages 28 CBN (cont.) Example: begin comment zero vector procedure zero(elt, j, lo, hi); value lo, hi; integer elt, j, lo, hi; begin for j := lo step 1 until hi do elt := 0; end integer i; integer array a[1:10], b[1:5, 1:5]; zero(a[i], i, 1, 10); zero(b[i, i], i, 1, 5); end

C SC 520 Principles of Programming Languages 29 CBN (cont.) Limitations of CBN: the obvious “swap” algorithm fails begin procedure swap(a, b); integer a, b; begin integer t; t := b; b := a; a := t; end; integer array a[1:10]; integer i; i := 1; a[i] := 2; // i=1 a[1]=2 swap(a[i], i); //(a[1],i)=(2,1) end swap(a[i],i) //i==2 & //a[1]==2 t:= i // t==1 i:=a[i] // i==2 a[i]:=t // a[2]==1 //(a[1],i)==(2,2) Reordering code doesn’t help; swap(i, a[i]) fails

C SC 520 Principles of Programming Languages 30 CBN (cont.) Proposed “solution”: assume in t:=s that the target l-value is computed before the source r-value procedure swap(a, b); integer a, b; begin integer procedure testset(t,s); integer t,s; begin testset := t;//ret. old r-val of target t := s;//assign source r-val to target end; a := testset(b, a); end swap(a[i], i) a[i]:=testset(i,a[i]) ts:= i; i:=a[i]; a[i]:=ts

C SC 520 Principles of Programming Languages 31 CBN (cont.) Problem: proposed “solution” has a bug. Difficulty is the re-evaluation of actual on each use of formal Can arrange an actual that yields a distinct l-value on each use begin integer array a[1:10]; integer j; integer procedure i; comment return next integer; begin j:= j + 1; i := j end; j := 0; swap(a[i], a[i]) end a[i] := testset(a[i], a[i]) ts :=a[i]; a[i]:=a[i]; a[i]:=ts; ts :=a[1]; a[2]:=a[3]; a[4]:=ts;