CS 536 Parameter Passing 1. Roadmap Last Time – Storing variables Locals, non-locals, globals This Time – Propagating values from one function to another.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
Subprogram ธนวัฒน์ แซ่ เอียบ. Subprograms as computational abstractions.
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.
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.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
SPL/2010 Pointers and Parameter Passing in C++ 1.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter EighteenModern Programming Languages1 Parameters.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
CS 536 Spring Run-time organization Lecture 19.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
Run-Time Storage Organization
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 536 Spring Code Generation II Lecture 21.
Run-time Environment and Program Organization
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.
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
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,
Runtime Environments Compiler Construction Chapter 7.
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.
Basic Semantics Associating meaning with language entities.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
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.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
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.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
Parameter Passing Mechanisms CS308 Compiler Theory.
CSE 3302 Programming Languages
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Chapter 9 Chapter 9 Subprograms. The Structure of Run-Time Memory.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Run-time organization
CSE 3302 Programming Languages
CSC 253 Lecture 8.
Chapter 9 :: Subroutines and Control Abstraction
Parameter Passing (Ch 18)
CSC 253 Lecture 8.
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Stack Memory 2 (also called Call Stack)
Subroutines – parameter passing
CSE 3302 Programming Languages
Presentation transcript:

CS 536 Parameter Passing 1

Roadmap Last Time – Storing variables Locals, non-locals, globals This Time – Propagating values from one function to another 2

Outline Parameter Passing – Different styles – What they mean – How they look on the stack 3

Vocabulary 4 Define a couple of terms that are helpful to talk about parameters – We’ve already obliquely talked about some of these

L- and R- Values L-Value – A value with a place of storage R-Value – A value that may not have storage 5 a = 1; a = b; b++;

Memory references Pointer – A variable whose value is a memory address Aliasing – When two or more variables hold same address 6

Parameter Passing In definition: void v(int a, int b, bool c) { … } – Terms Formals / formal parameters / parameters In call: v(a+b,8,true); – Terms Actuals / actual parameters / arguments 7

Types of Parameter Passing We’ll talk about 4 different varieties – Some of these are more used than others – Each has it’s own advantages / uses 8

Pass by Value On function call – Values of actuals are copied into the formals – C and java always pass by value void fun(int a){ int a = 1; } void main(){ int i = 0; fun(i); print(i); } 9

Pass by Reference On function call – The address of the actuals are implicitly copied 10 void fun(int a){ int a = 1; } void main(){ int i = 0; fun(i); print(i); }

Language Examples Pass by value – C and Java Pass by reference – Allowed in C++ and Pascal 11

Wait, Java is Pass by Value? All non-primitive L-values are pointers void fun(int a, Point p){ int a = 0; p.x = 5; } void main(){ int i = 0; Point k = new Point(1, 2); fun(i,k); } 12

Java – pass by value public static void main( String[] args ){ Dog aDog = new Dog("Max"); foo(aDog); if (aDog.getName().equals("Max")) { System.out.println( "Java passes by value." ); } else if (aDog.getName().equals("Fifi")) { System.out.println( "Java passes by reference." ); } public static void foo(Dog d) { d.getName().equals("Max"); d = new Dog("Fifi"); d.getName().equals("Fifi");} 13

Pass by Value-Result When function is called – Value of actual is passed When function returns – Final values are copied back to the actuals Used by Fortran IV, Ada – As the language examples show, not very modern 14

Pass-by-value-result int x = 1; // a global variable void f(int & a) { a = 2; // when f is called from main, a and x are aliases x = 0; } main() { f(x); cout << x; } 15

Pass-by-value-result void f(int &a, &b) { a = 2; b = 4; } main() { int x; f(x, x); cout << x; } 16

Pass by Name Conceptually works as follows: – When a function is called Body of the callee is rewritten with the text of the argument – Like macros in C / C++ 17

Call-by-need / lazy evaluation example 18

Implementing Parameter Passing Let’s talk about how this actually is going to work in memory 19

Let’s draw out the memory int g; void f (int x, int y, int z){ x = 3 ; y = 4; z = y; } void main(){ int a = 1, b = 2, c = 3; f(a,b,c); f(a+b,7,8); } 20 Consider pass-by-value and pass-by reference

Bad use of R-Values Can prevent programs that are valid in pass by value from working in pass by reference – Literals (for example) do not have locations in memory We will rely on the type checker to catch these errors. 21

Let’s draw out the memory again int g; void f (int x, int y, int z){ x = 3 ; y = 4; z = y; } void main(){ int a = 1, b = 2, c = 3; f(a,b,g); f(a+b,7,8); } 22 Consider pass by value-result and pass by name

Object Handling void alter(Point pt, Position pos){ pos = pt.p; pos.x++; pos.y++; } void main(){ Position loc; Point dot; // … initialize loc with x=1,y=2 // … initialize dot with loc alter(dot, loc); } 23 class Point{ Position p; } class Position{ int x, y; } In java, loc and dot are pointers to objects (on the heap) In C++, loc and dot are objects with no indirection (on the stack)

Efficiency Considerations Pass by Value – Copy values into AR (slow) – Access storage directly in function (fast) Pass by Address – Copy address into AR (fast) – Access storage via indirection (slow) Pass by Value-result – Strictly slower than pass by value – Also need to know where to copy locations back 24