Parameters. Chapter Eighteen Modern Programming Languages, 2nd ed. 1

Similar documents
UNIT V Sub u P b ro r g o r g a r m a s

Parameters. However, one important class of variables is still missing Þ parameters. Terminology: Example: Java, C, C++ Chap 18.

CS 345. Functions. Vitaly Shmatikov. slide 1

Chapter 9 Subprograms

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

CS 314 Principles of Programming Languages. Lecture 13

Review of the C Programming Language

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CS558 Programming Languages

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Chapter 5. Names, Bindings, and Scopes

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

Organization of Programming Languages CS 3200/5200N. Lecture 09

Weeks 6&7: Procedures and Parameter Passing

Scope. Chapter Ten Modern Programming Languages 1

Parameter Binding. Value: The formal parameter represents a local variable initialized to the value of the corresponding actual parameter.

Chapter 9 :: Subroutines and Control Abstraction

procedure definition (or declaration - some make a distinction, we generally won t), e.g., void foo(int x) {... }

Principles of Programming Languages

CS 314 Principles of Programming Languages

Software II: Principles of Programming Languages. Why Expressions?

22c:111 Programming Language Concepts. Fall Types I

Chapter 7. Expressions and Assignment Statements ISBN

Chapter 7. Expressions and Assignment Statements

11/29/17. Outline. Subprograms. Subroutine. Subroutine. Parameters. Characteristics of Subroutines/ Subprograms

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Module 27 Switch-case statements and Run-time storage management

Control Abstraction. Hwansoo Han

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Cost Models. Chapter Twenty-One Modern Programming Languages, 2nd ed. 1

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

Control Flow. Stephen A. Edwards. Fall Columbia University

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

int foo() { x += 5; return x; } int a = foo() + x + foo(); Side-effects GCC sets a=25. int x = 0; int foo() { x += 5; return x; }

A First Look At Java. Didactic Module 13 Programming Languages - EEL670 1

Types and Type Inference

Programming Languages

Programming Languages: Lecture 11

Chap. 8 :: Subroutines and Control Abstraction

Statement Basics. Assignment Statements

Pass by Value. Pass by Value. Our programs are littered with function calls like f (x, 5).

CS 314 Principles of Programming Languages

CSE 3302 Programming Languages Lecture 5: Control

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Subroutines and Control Abstraction. CSE 307 Principles of Programming Languages Stony Brook University

Principles of Programming Languages

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Chapter 9. Subprograms

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Subprograms. Akim D le Étienne Renault Roland Levillain EPITA École Pour l Informatique et les Techniques Avancées

Review of the C Programming Language for Principles of Operating Systems

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

Chapter 9. Subprograms

Chapter 5 Names, Binding, Type Checking and Scopes

9. Subprograms. 9.2 Fundamentals of Subprograms

Chapter 7 Expressions and Assignment statements

MIDTERM EXAMINATION - CS130 - Spring 2005

Programmiersprachen (Programming Languages)

Expressions and Assignment

NOTE: Answer ANY FOUR of the following 6 sections:

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Flow Control. CSC215 Lecture

MIDTERM EXAMINATION - CS130 - Spring 2003

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

CS 360 Programming Languages Interpreters

Expressions & Assignment Statements

Programming Languages, Summary CSC419; Odelia Schwartz

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

The SPL Programming Language Reference Manual

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

Compiler Design. Code Shaping. Hwansoo Han

Chapter 6 Control Flow. June 9, 2015

known as non-void functions/methods in C/C++/Java called from within an expression.

G Programming Languages - Fall 2012

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

CS 415 Midterm Exam Spring 2002

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Expressions and Assignment Statements

The Environment Model

TYPES, VALUES AND DECLARATIONS

COS 140: Foundations of Computer Science

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Run-Time Data Structures

The Environment Model. Nate Foster Spring 2018

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Outline. Introduction to Programming (in C++) Introduction. First program in C++ Programming examples

Code Generation & Parameter Passing

Short Notes of CS201

Transcription:

Parameters Chapter Eighteen Modern Programming Languages, 2nd ed. 1

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

Outline 18.2 Parameter correspondence 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 18.10 Specification issues Chapter Eighteen Modern Programming Languages, 2nd ed. 3

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

Keyword Parameters 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 Parameter order is irrelevant here Chapter Eighteen Modern Programming Languages, 2nd ed. 5

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

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

Example: C++ 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 Chapter Eighteen Modern Programming Languages, 2nd ed. 8

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

Outline 18.2 Parameter correspondence 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 18.10 Specification issues Chapter Eighteen Modern Programming Languages, 2nd ed. 10

By Value For by-value parameter passing, the formal parameter is just like a local variable in the 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. Simplest method Widely used The only method in real Java Chapter Eighteen Modern Programming Languages, 2nd ed. 11

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

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

void f() { ConsCell x = new ConsCell(0,null); alter(3,x); void alter(int newhead, ConsCell c) { c.sethead(newhead); c = null; current head: 0 tail: null When alter is starting newhead: 3 c: x: Chapter Eighteen Modern 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; current head: 3 tail: null When alter is finishing newhead: 3 c: null x: Chapter Eighteen Modern Programming Languages, 2nd ed. 15

By Result For by-result parameter passing, the formal parameter is just like a local variable in the 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. Also called copy-out Actual must have an lvalue Introduced in Algol 68; sometimes used for Ada Chapter Eighteen Modern Programming Languages, 2nd ed. 16

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); current a: 3 x: 3 b: 4 y: 4 When plus is starting c:? z:? Chapter Eighteen Modern 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); current a: 3 x: 3 b: 4 y: 4 When plus is ready to return c: 7 z:? Chapter Eighteen Modern 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); a: 3 current x: 3 b: 4 y: 4 When plus has returned c: 7 z: 7 Chapter Eighteen Modern Programming Languages, 2nd ed. 19

By Value-Result 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. Also called copy-in/copy-out Actual must have an lvalue Chapter Eighteen Modern Programming Languages, 2nd ed. 20

void plus(int a, by-value-result int b) { b += a; void f() { int x = 3; plus(4, x); current a: 4 x: 3 b: 3 When plus is starting Chapter Eighteen Modern Programming Languages, 2nd ed. 21

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

void plus(int a, by-value-result int b) { b += a; void f() { int x = 3; plus(4, x); a: 4 current x: 7 b: 7 When plus has returned Chapter Eighteen Modern Programming Languages, 2nd ed. 23

By Reference 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. One of the earliest methods: Fortran Most efficient for large objects Still frequently used Chapter Eighteen Modern Programming Languages, 2nd ed. 24

void plus(int a, by-reference int b) { b += a; void f() { int x = 3; plus(4, x); current a: 4 b: x: 3 When plus is starting Chapter Eighteen Modern Programming Languages, 2nd ed. 25

void plus(int a, by-reference int b) { b += a; void f() { int x = 3; plus(4, x); current a: 4 b: x: 7 When plus has made the assignment Chapter Eighteen Modern Programming Languages, 2nd ed. 26

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

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

Example 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 Eighteen Modern 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 g() { int x; x = 10; sigsum(x,x); return x; When sigsum is starting current n: ans: i:? x: 10 result:? Chapter Eighteen Modern Programming Languages, 2nd ed. 30

By Macro Expansion 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). Like C macros Natural implementation: textual substitution before compiling Chapter Eighteen Modern Programming Languages, 2nd ed. 31

Macro Expansions In C source expanded editor file pre-processor source compiler assemblylanguage file An extra step in the classical sequence Macro expansion before compilation source file: expanded source: #define MIN(X,Y) ((X)<(Y)?(X):(Y)) a = MIN(b,c); a = ((b)<(c)?(b):(c)) Chapter Eighteen Modern Programming Languages, 2nd ed. 32

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

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

Capture Example source file: #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); expanded source: int main() { int temp=1, b=2; {int temp= temp ; temp = b ; b =temp; ; printf("%d, %d\n", temp, b); Chapter Eighteen Modern Programming Languages, 2nd ed. 35

Capture In a program fragment, any occurrence of a variable that is not statically bound is free When a fragment is moved to a different context, its free variables can become bound 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 Eighteen Modern Programming Languages, 2nd ed. 36

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

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

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

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

By Need 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. Used in lazy functional languages (Haskell) Avoids wasteful recomputations of by-name Chapter Eighteen Modern Programming Languages, 2nd ed. 41

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

Laziness 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. Chapter Eighteen Modern Programming Languages, 2nd ed. 43

Outline 18.2 Parameter correspondence 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 18.10 Specification issues Chapter Eighteen Modern Programming Languages, 2nd ed. 44

Specification Issues Are these just implementation techniques, or part of the language specification? 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 Eighteen Modern Programming Languages, 2nd ed. 45

Without Side Effects Big question: are parameters always evaluated (eager evaluation), or only if they are really needed (lazy evaluation)? 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 Eighteen Modern Programming Languages, 2nd ed. 46

With Side Effects A program can detect which parameterpassing technique is being used by the language system 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 Case in point: Ada Chapter Eighteen Modern Programming Languages, 2nd ed. 47

Ada Modes 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 Ada specification intentionally leaves some flexibility for implementations Chapter Eighteen Modern Programming Languages, 2nd ed. 48

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

Conclusion 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 These are not the only schemes that have been tried, just some of the most common The CS corollary of Murphy s Law: Inside every little problem there is a big problem waiting to get out Chapter Eighteen Modern Programming Languages, 2nd ed. 50