Parameters Chapter EighteenModern Programming Languages, 2nd ed.1.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
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
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.
Parameters Chapter EighteenModern Programming Languages, 2nd ed.1 Spring 2012.
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.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Procedures and Control Flow CS351 – Programming Paradigms.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
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.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
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.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
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.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
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
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CS 2104 Prog. Lang. Concepts Subprograms
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
ISBN Chapter 7 Expressions and Assignment Statements.
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
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.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
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 Subprograms In Text: Chapter 8. 2 Chapter 8: Subprograms Outline Definitions Referencing environments Parameter passing modes and mechanisms Independent.
Chapter 9 Subprograms. 2 Fundamentals of Subprograms Each subprogram has a single entry point The calling program is suspended during execution of the.
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.
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
CSI 3125, Subprograms, page 1 Subprograms General concepts Parameter passing Functions Subprograms as parameters.
Chapter EighteenModern Programming Languages1 Parameters.
Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
ISBN Chapter 9 Subprograms. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CSE 3302 Programming Languages
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
Procedure Activations Programming Language. Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Chapter 9 Subprograms. Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Chapter 9 Subprograms.
Functions.
Chapter 6: Data Types Lectures # 10.
Principles of programming languages 4: Parameter passing, Scope rules
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Chapter 9 :: Subroutines and Control Abstraction
Parameter Passing (Ch 18)
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Subprograms In Text: Chapter 9.
CSE 3302 Programming Languages
Presentation transcript:

Parameters Chapter EighteenModern Programming Languages, 2nd ed.1

Parameter Passing n How are parameters passed? n Looks simple enough… n We will see seven techniques Chapter EighteenModern Programming Languages, 2nd ed.2 int plus(int a, int b) { return a+b; } int x = plus(1,2); formal parameters method body method call actual parameters

Outline n 18.2 Parameter correspondence n Implementation techniques – 18.3 By value – 18.4 By result – 18.5 By value-result – 18.6 By reference – 18.7 By macro expansion – 18.8 By name – 18.9 By need n Specification issues Chapter EighteenModern Programming Languages, 2nd ed.3

Parameter Correspondence n A preliminary question: how does the language match up parameters? n That is, which formal parameters go with which actual parameters? n Most common case: positional parameters – Correspondence determined by positions – nth formal parameter matched with nth actual Chapter EighteenModern Programming Languages, 2nd ed.4

Keyword Parameters n Correspondence can be determined by matching parameter names Ada: DIVIDE(DIVIDEND => X, DIVISOR => Y); Matches actual parameter X to formal parameter DIVIDEND, and Y to DIVISOR n Parameter order is irrelevant here Chapter EighteenModern Programming Languages, 2nd ed.5

Mixed Keyword And Positional n Most languages that support keyword parameters allow both: Ada, Fortran, Dylan, Python n The first parameters in a list can be positional, and the remainder can be keyword parameters Chapter EighteenModern Programming Languages, 2nd ed.6

Optional Parameters n Optional, with default values: formal parameter list includes default values to be used if the corresponding actual is missing n This gives a very short way of writing certain kinds of overloaded function definitions Chapter EighteenModern Programming Languages, 2nd ed.7

Example: C++ Chapter EighteenModern Programming Languages, 2nd ed.8 int f(int a=1, int b=2, int c=3) { body } int f() {f(1,2,3);} int f(int a) {f(a,2,3);} int f(int a, int b) {f(a,b,3);} int f(int a, int b, int c) { body }

Unlimited Parameter Lists n Some languages allow actual parameter lists of unbounded length: C, C++, and scripting languages like JavaScript, Python, and Perl n Library routines must be used to access the excess actual parameters n A hole in static type systems, since the types of the excess parameters cannot be checked at compile time Chapter EighteenModern Programming Languages, 2nd ed.9 int printf(char *format,...) { body }

Outline n 18.2 Parameter correspondence n Implementation techniques – 18.3 By value – 18.4 By result – 18.5 By value-result – 18.6 By reference – 18.7 By macro expansion – 18.8 By name – 18.9 By need n Specification issues Chapter EighteenModern Programming Languages, 2nd ed.10

By Value n Simplest method n Widely used n The only method in real Java Chapter EighteenModern Programming Languages, 2nd ed.11 For by-value parameter passing, the formal parameter is just like a local variable in the activation record of the called method, with one important difference: it is initialized using the value of the corresponding actual parameter, before the called method begins executing.

Chapter EighteenModern Programming Languages, 2nd ed.12 int plus(int a, int b) { a += b; return a; } void f() { int x = 3; int y = 4; int z = plus(x, y); } When plus is starting previous activation record return address x: 3 previous activation record result: ? return address a: 3 current activation record b: 4y: 4 z: ?

Changes Visible To The Caller n When parameters are passed by value, changes to a formal do not affect the actual n But it is still possible for the called method to make changes that are visible to the caller n The value of the parameter could be a pointer (in Java, a reference) n Then the actual cannot be changed, but the object referred to by the actual can be Chapter EighteenModern Programming Languages, 2nd ed.13

Chapter EighteenModern Programming Languages, 2nd ed.14 void f() { ConsCell x = new ConsCell(0,null); alter(3,x); } void alter(int newHead, ConsCell c) { c.setHead(newHead); c = null; } When alter is starting previous activation record return address x: previous activation record return address newHead: 3 current activation record c: head: 0 tail: null

Chapter EighteenModern Programming Languages, 2nd ed.15 void f() { ConsCell x = new ConsCell(0,null); alter(3,x); } void alter(int newHead, ConsCell c) { c.setHead(newHead); c = null; } When alter is finishing previous activation record return address x: previous activation record return address newHead: 3 current activation record c: null head: 3 tail: null

By Result n Also called copy-out n Actual must have an lvalue n Introduced in Algol 68; sometimes used for Ada Chapter EighteenModern Programming Languages, 2nd ed.16 For by-result parameter passing, the formal parameter is just like a local variable in the activation record of the called method—it is uninitialized. After the called method finished executing, the final value of the formal parameter is assigned to the corresponding actual parameter.

Chapter EighteenModern Programming Languages, 2nd ed.17 void plus(int a, int b, by-result int c) { c = a+b; } void f() { int x = 3; int y = 4; int z; plus(x, y, z); } When plus is starting previous activation record return address x: 3 previous activation record return address a: 3 current activation record b: 4y: 4 z: ? c: ?

Chapter EighteenModern Programming Languages, 2nd ed.18 void plus(int a, int b, by-result int c) { c = a+b; } void f() { int x = 3; int y = 4; int z; plus(x, y, z); } When plus is ready to return previous activation record return address x: 3 previous activation record return address a: 3 current activation record b: 4y: 4 z: ? c: 7

Chapter EighteenModern Programming Languages, 2nd ed.19 void plus(int a, int b, by-result int c) { c = a+b; } void f() { int x = 3; int y = 4; int z; plus(x, y, z); } When plus has returned previous activation record return address x: 3 previous activation record return address a: 3 current activation record b: 4y: 4 z: 7c: 7

By Value-Result n Also called copy-in/copy-out n Actual must have an lvalue Chapter EighteenModern Programming Languages, 2nd ed.20 For passing parameters by value-result, the formal parameter is just like a local variable in the activation record of the called method. It is initialized using the value of the corresponding actual parameter, before the called method begins executing. Then, after the called method finishes executing, the final value of the formal parameter is assigned to the actual parameter.

Chapter EighteenModern Programming Languages, 2nd ed.21 void plus(int a, by-value-result int b) { b += a; } void f() { int x = 3; plus(4, x); } When plus is starting previous activation record return address x: 3 previous activation record return address a: 4 current activation record b: 3

Chapter EighteenModern Programming Languages, 2nd ed.22 void plus(int a, by-value-result int b) { b += a; } void f() { int x = 3; plus(4, x); } When plus is ready to return previous activation record return address x: 3 previous activation record return address a: 4 current activation record b: 7

Chapter EighteenModern Programming Languages, 2nd ed.23 void plus(int a, by-value-result int b) { b += a; } void f() { int x = 3; plus(4, x); } When plus has returned previous activation record return address x: 7 previous activation record return address a: 4 current activation record b: 7

By Reference n One of the earliest methods: Fortran n Most efficient for large objects n Still frequently used Chapter EighteenModern Programming Languages, 2nd ed.24 For passing parameters by reference, the lvalue of the actual parameter is computed before the called method executes. Inside the called method, that lvalue is used as the lvalue of the corresponding formal parameter. In effect, the formal parameter is an alias for the actual parameter—another name for the same memory location.

Chapter EighteenModern Programming Languages, 2nd ed.25 void plus(int a, by-reference int b) { b += a; } void f() { int x = 3; plus(4, x); } When plus is starting previous activation record return address x: 3 previous activation record return address a: 4 current activation record b:

Chapter EighteenModern Programming Languages, 2nd ed.26 void plus(int a, by-reference int b) { b += a; } void f() { int x = 3; plus(4, x); } When plus has made the assignment previous activation record return address x: 7 previous activation record return address a: 4 current activation record b:

Implementing Reference Chapter EighteenModern Programming Languages, 2nd ed.27 void plus(int a, int *b) { *b += a; } void f() { int x = 3; plus(4, &x); } void plus(int a, by-reference int b) { b += a; } void f() { int x = 3; plus(4, x); } C implementation By-reference = address by value Previous example

Aliasing n When two expressions have the same lvalue, they are aliases of each other n There are obvious cases: n Passing by reference leads to less obvious cases… Chapter EighteenModern Programming Languages, 2nd ed.28 ConsCell x = new ConsCell(0,null); ConsCell y = x; A[i]=A[j]+A[k];

Example Chapter EighteenModern Programming Languages, 2nd ed.29 void sigsum(by-reference int n, by-reference int ans) { ans = 0; int i = 1; while (i <= n) ans += i++; } int f() { int x,y; x = 10; sigsum(x,y); return y; } int g() { int x; x = 10; sigsum(x,x); return x; }

Chapter EighteenModern Programming Languages, 2nd ed.30 void sigsum(by-reference int n, by-reference int ans) { ans = 0; int i = 1; while (i <= n) ans += i++; } int g() { int x; x = 10; sigsum(x,x); return x; } When sigsum is starting previous activation record return address x: 10 previous activation record return address n: current activation record ans: i: ? result: ?

By Macro Expansion n Like C macros n Natural implementation: textual substitution before compiling Chapter EighteenModern Programming Languages, 2nd ed.31 For passing parameters by macro expansion, the body of the macro is evaluated in the caller’s context. Each actual parameter is evaluated on every use of the corresponding formal parameter, in the context of that occurrence of that formal parameter (which is itself in the caller’s context).

Macro Expansions In C n An extra step in the classical sequence n Macro expansion before compilation Chapter EighteenModern Programming Languages, 2nd ed.32 #define MIN(X,Y) ((X)<(Y)?(X):(Y)) a = MIN(b,c); a = ((b)<(c)?(b):(c)) source file: expanded source: editorpre-processorcompiler source file expanded source assembly- language file

Preprocessing n Replace each use of the macro with a copy of the macro body, with actuals substituted for formals n An old technique, used in assemblers before the days of high-level languages n It has some odd effects… Chapter EighteenModern Programming Languages, 2nd ed.33

Repeated Evaluation n Each actual parameter is re-evaluated every time it is used Chapter EighteenModern Programming Languages, 2nd ed.34 #define MIN(X,Y) ((X)<(Y)?(X):(Y)) a = MIN(b++,c++); a = ((b++)<(c++)?(b++):(c++)) source file: expanded source:

Capture Example Chapter EighteenModern Programming Languages, 2nd ed.35 #define intswap(X,Y) {int temp=X; X=Y; Y=temp;} int main() { int temp=1, b=2; intswap(temp,b); printf("%d, %d\n", temp, b); } int main() { int temp=1, b=2; {int temp= temp ; temp = b ; b =temp;} ; printf("%d, %d\n", temp, b); } source file: expanded source:

Capture n In a program fragment, any occurrence of a variable that is not statically bound is free n When a fragment is moved to a different context, its free variables can become bound n This phenomenon is called capture: – Free variables in the actuals can be captured by definitions in the macro body – Also, free variables in the macro body can be captured by definitions in the caller Chapter EighteenModern Programming Languages, 2nd ed.36

By Name n Like macro expansion without capture n Algol 60 and others n Now unpopular Chapter EighteenModern Programming Languages, 2nd ed.37 For passing parameters by name, each actual parameter is evaluated in the caller’s context, on every use of the corresponding formal parameter.

Implementing By-Name n The actual parameter is treated like a little anonymous function n Whenever the called method needs the value of the formal (either rvalue or lvalue) it calls the function to get it n The function must be passed with its nesting link, so it can be evaluated in the caller’s context Chapter EighteenModern Programming Languages, 2nd ed.38

Chapter EighteenModern Programming Languages, 2nd ed.39 void f(by-name int a, by-name int b) { b=5; b=a; } int g() { int i = 3; f(i+1,i); return i; } When f is starting i previous activation record return address i: 3 previous activation record return address current activation record a: b: result: ? i+1

Comparison n Like macro expansion, by-name parameters are re-evaluated every time they are used n (Can be useful, but more often this is merely wasteful) n Unlike macro expansion, there is no possibility of capture Chapter EighteenModern Programming Languages, 2nd ed.40

By Need n Used in lazy functional languages (Haskell) n Avoids wasteful recomputations of by-name Chapter EighteenModern Programming Languages, 2nd ed.41 For passing parameters by need, each actual parameter is evaluated in the caller’s context, on the first use of the corresponding formal parameter. The value of the actual parameter is then cached, so that subsequent uses of the corresponding formal parameter do not cause reevaluation.

Chapter EighteenModern Programming Languages, 2nd ed.42 void f(by-need int a, by-need int b) { b=a; b=a; } void g() { int i = 3; f(i+1,i); return i; } When f is starting i previous activation record return address i: 3 previous activation record return address current activation record a: b: result: ? i+1

Laziness Chapter EighteenModern Programming Languages, 2nd ed.43 boolean andand(by-need boolean a, by-need boolean b) { if (!a) return false; else return b; } boolean g() { while (true) { } return true; } void f() { andand(false,g()); } Here, andand is short-circuiting, like ML’s andalso and Java’s && operators. The method f will terminate. Same behavior for by-name and macro expansion.

Outline n 18.2 Parameter correspondence n Implementation techniques – 18.3 By value – 18.4 By result – 18.5 By value-result – 18.6 By reference – 18.7 By macro expansion – 18.8 By name – 18.9 By need n Specification issues Chapter EighteenModern Programming Languages, 2nd ed.44

Specification Issues n Are these just implementation techniques, or part of the language specification? n Depends on the language: – Without side-effects, parameter-passing technique may be undetectable by the programmer – Even with side effects, some languages specify the parameter passing technique only partially Chapter EighteenModern Programming Languages, 2nd ed.45

Without Side Effects n Big question: are parameters always evaluated (eager evaluation), or only if they are really needed (lazy evaluation)? n Cost model may also be used by the programmer (more in Chapter 21): – Is re-evaluation of a formal expensive? – Does parameter-passing take time proportional to the size of the object? Chapter EighteenModern Programming Languages, 2nd ed.46

With Side Effects n A program can detect which parameter- passing technique is being used by the language system n But it may be an implementation detail that programs are not supposed to depend on—it may not be part of the specification of the language n Case in point: Ada Chapter EighteenModern Programming Languages, 2nd ed.47

Ada Modes n Three parameter-passing modes: – in : these can be read in the called method, but not assigned—like constants – out : these must be assigned and cannot be read – in out : may be read and/or assigned n Ada specification intentionally leaves some flexibility for implementations Chapter EighteenModern Programming Languages, 2nd ed.48

Ada Implementations n Copying is specified for scalar values: – in = value, out = result, in out = value/result n Aggregates like arrays and records may be passed by reference instead n Any program that can detect the difference (like some of our earlier examples) is not a legal Ada program Chapter EighteenModern Programming Languages, 2nd ed.49

Conclusion n Today: – How to match formals with actuals – Seven different parameter-passing techniques – Ideas about where to draw the line between language definition and implementation detail n These are not the only schemes that have been tried, just some of the most common n The CS corollary of Murphy’s Law: Chapter EighteenModern Programming Languages, 2nd ed.50 Inside every little problem there is a big problem waiting to get out