Chapter EighteenModern Programming Languages1 Parameters.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

The Assembly Language Level
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.
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.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Procedures and Control Flow CS351 – Programming Paradigms.
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.
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
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.
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.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
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.
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,
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.
Basic Semantics Associating meaning with language entities.
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.
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.
CSC 8505 Compiler Construction Runtime Environments.
Chapter EighteenModern Programming Languages1 Parameters.
Parameter Passing Mechanisms CS308 Compiler Theory.
CSE 3302 Programming Languages
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.
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.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
CS314 – Section 5 Recitation 9
Functions.
Algol 60 John Cowan N Languages in N Months Meetup June 7, 2016
Principles of programming languages 4: Parameter passing, Scope rules
Chapter 9 :: Subroutines and Control Abstraction
Parameter Passing (Ch 18)
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Topic 3-a Calling Convention 1/10/2019.
CSE 3302 Programming Languages
Parameter transmission
PZ09B - Parameter transmission
Parameter transmission
Procedure Linkages Standard procedure linkage Procedure has
Corresponds with Chapter 5
Parameter transmission
C Parameter Passing.
Presentation transcript:

Chapter EighteenModern Programming Languages1 Parameters

Chapter EighteenModern Programming Languages2 Outline 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

Chapter EighteenModern Programming Languages3 By Value n Simplest method n Widely used n The only method in real Java 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 Languages4 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

Chapter EighteenModern Programming Languages5 Changes Visible To The Caller n When parameters are passed by value, changes to a (formal) parameter do not affect the actual parameter (“argument”) n 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”) n Then the actual argument cannot be changed, but the object referred to by the actual argument can be

Chapter EighteenModern Programming Languages6 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

Chapter EighteenModern Programming Languages7 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

Chapter EighteenModern Programming Languages8 By Result n Also called copy-out – Acts like a return value in a variable n Actual must have an lvalue n Introduced in Algol 68; sometimes used for Ada; OUT parameters in C#, D 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 Languages9 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

Chapter EighteenModern Programming Languages10 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

Chapter EighteenModern Programming Languages11 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

Chapter EighteenModern Programming Languages12 By Value-Result n Also called copy-in/copy-out n Actual must have an lvalue 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 Languages13 void plus(int a, by-value-result int b) { b += a; } void f() { int x = 3; plus(4, x); } When plus is starting

Chapter EighteenModern Programming Languages14 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

Chapter EighteenModern Programming Languages15 void plus(int a, by-value-result int b) { b += a; } void f() { int x = 3; plus(4, x); } When plus has returned

Chapter EighteenModern Programming Languages16 By Reference n One of the earliest methods: Fortran n Most efficient for large objects n Still frequently used 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 Languages17 void plus(int a, by-reference int b) { b += a; } void f() { int x = 3; plus(4, x); } When plus is starting

Chapter EighteenModern Programming Languages18 void plus(int a, by-reference int b) { b += a; } void f() { int x = 3; plus(4, x); } When plus has made the assignment

Chapter EighteenModern Programming Languages19 Implementing Reference 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

Chapter EighteenModern Programming Languages20 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… ConsCell x = new ConsCell(0,null); ConsCell y = x; A[i]=A[j]+A[k]; // i, j, or k could be equal

Chapter EighteenModern Programming Languages21 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 EighteenModern Programming Languages22 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

Chapter EighteenModern Programming Languages23 By Macro Expansion n Like C macros n Natural implementation: textual substitution before compiling 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).

Chapter EighteenModern Programming Languages24 Macro Expansions In C n An extra step in the classical sequence n Macro expansion before compilation #define MIN(X,Y) ((X)<(Y)?(X):(Y)) a = MIN(b,c); a = ((b)<(c)?(b):(c)) source file: expanded source:

Chapter EighteenModern Programming Languages25 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 Languages26 Repeated Evaluation n Each actual parameter is re-evaluated every time it is used #define MIN(X,Y) ((X)<(Y)?(X):(Y)) a = MIN(b++,c++); a = ((b++)<(c++)?(b++):(c++)) source file: expanded source:

Chapter EighteenModern Programming Languages27 Capture Example #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:

Chapter EighteenModern Programming Languages28 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 Languages29 By Name n Like macro expansion without capture n Algol 60 and others n Now unpopular For passing parameters by name, each actual parameter is evaluated in the caller’s context, on every use of the corresponding formal parameter.

Chapter EighteenModern Programming Languages30 Implementing By-Name n The actual parameter is treated like a little anonymous function (a “thunk”) – It could be an expression 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 Languages31 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

Chapter EighteenModern Programming Languages32 Comparison n Like macro expansion, by-name parameters are re-evaluated every time they are used – Makes a big difference with expressions – Passing a literal by name = by value – Passing a variable by name = by reference n Unlike macro expansion, there is no possibility of capture n See byname.cpp, arraybyname.cpp

Chapter EighteenModern Programming Languages33 By Need n Used in lazy functional languages (Haskell) n Avoids wasteful recomputations of by-name 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 Languages34 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

Chapter EighteenModern Programming Languages35 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.