Parameter passing. Parameter: A variable in a procedure that represents some other data from the procedure that invoked the given procedure.

Similar documents
CS 345. Functions. Vitaly Shmatikov. slide 1

Principles of Programming Languages

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

Weeks 6&7: Procedures and Parameter Passing

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

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

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CS558 Programming Languages

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

CSc 520 Principles of Programming Languages

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

CSc 520 Principles of Programming Languages. ameter Passing Reference Parameter. Call-by-Value Parameters. 28 : Control Structures Parameters

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

CSc 520 Principles of Programming Languages

CS 314 Principles of Programming Languages

CSE 307: Principles of Programming Languages

Chapter 9 :: Subroutines and Control Abstraction

Informatica 3 Syntax and Semantics

Programming Languages

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Chap. 8 :: Subroutines and Control Abstraction

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Today's Topics. Last Time Modelling Scopes and Visibility The Run Stack, the Dynamic Pointer Stack, the Display (LL,ON) addressing

Programmiersprachen (Programming Languages)

Topic IV. Block-structured procedural languages Algol and Pascal. References:

CS 314 Principles of Programming Languages. Lecture 13

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

G Programming Languages - Fall 2012

CSE 3302 Notes 7: Control Abstraction

CMSC 330: Organization of Programming Languages

The Procedure Abstraction Part I: Basics

Run-time Environments -Part 1

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

References and pointers

Principles of Programming Languages

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

Memory Management and Run-Time Systems

COMPILER DESIGN LECTURE NOTES

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

SYSC 2006 C Winter 2012

CS 415 Midterm Exam Spring SOLUTION

9. Subprograms. 9.2 Fundamentals of Subprograms

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

Concepts Introduced in Chapter 7

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CS 314 Principles of Programming Languages

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

Binding and Variables

Informatica 3 Marcello Restelli

Parameter Passing Styles

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

A Type System for Object Initialization In the Java TM Bytecode Language

CS 242. Fundamentals. Reading: See last slide

Chapter 9 Subprograms

Statement Basics. Assignment Statements

Control Abstraction. Hwansoo Han

Procedures and Run-Time Storage

Thunks. A Way of Compiling Procedure Statements with Some Comments on Procedure Declarations* P. Z. Ingerman

Chapter 9 Subroutines and Control Abstraction. June 22, 2016

Topic 3-a. Calling Convention 2/29/2008 1

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Computers in Engineering COMP 208. Subprograms. Subroutines. Subroutines Michael A. Hawker

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

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

Arrays and Pointers (part 1)

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

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

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

Computers in Engineering. Subroutines Michael A. Hawker

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

LECTURE 17. Expressions and Assignment

Procedures and Run-Time Storage

Pointers. Pointers. Pointers (cont) CS 217

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

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

22c:111 Programming Language Concepts. Fall Functions

Special Topics: Programming Languages

Run-Time Environments

Lecture 2:- Functions. Introduction

Today USING POINTERS. Functions: parameters and arguments. Todaywewilllookattopicsrelatingtotheuseofpointers

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

UNIT-IV: MACRO PROCESSOR

Introduction. Chapter 4 Macro Processors -- Basic Macro Processor Functions. Macro expansion. Basic macro processor functions

Functional Programming. Big Picture. Design of Programming Languages

CSE 307: Principles of Programming Languages

Principles of Programming Languages

LECTURE 18. Control Flow

Intermediate Code Generation

A macro- generator for ALGOL

Intermediate Code Generation Part II

Arrays and Pointers (part 1)

CS558 Programming Languages. Winter 2013 Lecture 4

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

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

Programming Languages: Lecture 11

Functions. Arash Rafiey. September 26, 2017

(Not Quite) Minijava

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

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Transcription:

Parameter passing Parameter: A variable in a procedure that represents some other data from the procedure that invoked the given procedure. Parameter transmission: How that information is passed to the procedure. The parameter is also called the formal argument.the data from the invoking procedure is called the actual argument or sometimes just the argument. Usual syntax: Actual arguments: call P(A, B+2, 27+3) Parameters: Procedure P(X, Y, Z) What is connection between the parameters and the arguments? Call by name Call by reference Call by value Call by result (or value-result) 1

Language dependent Difference languages have different mechanisms: ALGOL - name, value Pascal - value, reference C - value (BUT pointers give us reference Constant tension between desire for efficiency and semantic correctness in defining parameter transmission. 2

Call by name Substitute argument for parameter at each occurrence of parameter: Invocation: P(A, B+2, 27+3) Definition: procedure P(X,Y,Z) {int I; I=7; X = I + (7/Y)*Z;} Meaning: P(X,Y,Z) {int I; I=7; A=I+(7/(B+2))*(27+3);} This is a true macro expansion. Simple semantics, BUT: 1. Implementation. How to do it? 2. Aliases. What if statement of P were: I = A? 3. Expressions versus statements: If we had D=P(1,2,3) and a return(42) in P, what does semantics mean? 4. Error conditions: P(A+B, B+2, 27+3) 3

Implementation of call by name A thunk is the code which computes the L-value and R- value of an argument. For each argument, pass code address that computes both L-values and R-values of arguments. P(A, B+2, 27+3) generates: jump to subroutine P address of thunk to return L-value(A) address of thunk to return R-value(A) address of thunk to return L-value(B+2) address of thunk to return R-value(B+2) address of thunk to return L-value(27+3) address of thunk to return R-value(27+3) To assign to X, call thunk 1, To access X, call thunk 2 To assign to Y, call thunk 3, To access Y, call thunk 4 To assign to Z, call thunk 5, To access Z, call thunk 6 Issue: Assignment to (B+2): How? Call by name is conceptually convenient, but inefficient. 4

Examples of Call by Name 1. P(x) {x = x + x;} Seems simple enough Y = 2; P(Y); write(y) means Y = Y+Y write(y) prints 4 2. int A[10]; for(i=0; I<10; I++) {A[I]=I;}; I=1; P(A[I]) A[1] = A[1] + A[1] A[1] set to 2 3. But: F {I = I + 1; return I;} What is: P(A[F])? P(A[F]) A[F] = A[F]+A[F] A[I++] = A[I++]+A[I++] A[2] = A[3]+A[4] 4. Write a program to exchange values of X and Y: (swap(x,y)) Usual way: swap(x,y) {t=x; x=y; y=t;} Cannot do it with call by name. Cannot handle both of following: swap(i, A[I]) swap(a[i],i) One of these must fail. 5

Call by reference Pass the L-value of the argument for the parameter. Invocation: P(A, B+2, 27+3) Implementation: Temp1 = B+2 Temp2 = 27+3 jump to subroutine P L-value of A L-value of Temp1 L-value of Temp2 This is the most common parameter transmission mechanism. In the procedure activation record, parameter X is a local variable whose R-value is the L-value of the argument. 6

Call by value Pass the R-value of the argument for the parameter. Invocation: P(A, B+2, 27+3) Implementation: Temp1 = B+2 Temp2 = 27+3 jump to subroutine P R-value of A R-value of Temp1 R-value of Temp2 In procedure activation record, parameter X is a local variable whose R-value is the R-value of the argument. 7

Call by reference in C C only has call by value, BUT pointer variables allow for simulating call by reference: P(i, j) passes i and j by value. P(&i, &j) passes L-values of i and j. P(*x, *y) {*x = *y + 1;} arguments are addresses (pointers) Call by result (or value-result): Call by value, AND pass back the final value to argument upon return. Parameter is a local value in procedure. Similar to call by reference, except for aliasing. 8

In-out semantics Parameters in Ada are based upon use (semantics), not implementation: in - argument value will be used in procedure. out - parameter value will be used in calling program. in out - both uses of arguments and parameters P(X in integer; Y out integer; Z in out integer); begin... end; In Ada 83, language definition allowed some latitude in implementation as long as implementation consistent, ok. But this meant that the same program could give different answers from different standards conforming compilers In Ada 95, more restricted: in integer is value, out integer is value-result, composite (e.g., arrays) is reference. 9

Example of parameter passing Main {A = 2; B = 5; C = 8; D = 9; P(A, B, C, D); write(a, B, C, D); P(U, V, W, X) {V = U+A; W = A+B; A = A+1; X = A+2; write(u, V, W, X)} Fill in table assume parameters are of the given type: A B C D U V W X print P print main 2 5 8 9 Call by name 3 4 6 5 A B C D 3465 3465 Call by reference 3 4 6 5 A B C D 3465 3465 Call by value Call by result 3 5 8 9 2 4 7 5 2475 3589 3 5 8 9 2 4 7 5 2475 2475 When do call by name and call by reference differ? When L-value can change between parameter references. E.g., P(I, A[I]) 10