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.

Slides:



Advertisements
Similar presentations
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Advertisements

Programming Languages and Paradigms
COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
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.
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.
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.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Advanced Programming Parameter Passing Giuseppe Attardi Università di Pisa.
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.
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.
C++ Pointer and Functions
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.
Honors Compilers An Introduction to Algol-68S Jan 24 th 2002.
Overview Parameter passing modes.
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,
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
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
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Compiler Construction
Chapter 7 Runtime Environments. Relationships between names and data objects As execution proceeds, the same name can denote different data objects Procedures,
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.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
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.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
1 Subroutines Structure of Programming Language. 2 Procedures and Functions There are two categories of subprograms Function: subroutine that returns.
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.
ISBN Chapter 9 Subprograms Sections
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
Parameter Passing Mechanisms CS308 Compiler Theory.
CSE 3302 Programming Languages
Names, Scope, and Bindings Programming Languages and Paradigms.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
CS314 – Section 5 Recitation 9
Functions.
Parameter passing Module 12.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Principles of programming languages 4: Parameter passing, Scope rules
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSE 3302 Programming Languages
UNIT V Run Time Environments.
Parameter transmission
Parameter transmission
Parameter transmission
Presentation transcript:

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. A[I+J] has both. A[I+J] = X; // OK X = A[I+J]; // OK f(I) or I+J has only an rvalue. X=f(I); // OK f(I) = X // Not OK.

Variables: lvalues and rvalues In the assignment statement “X=Y”, variables X and Y are being used in different ways. Y is being used for the value it holds. E.g. if Y is currently set to 3, this statement has the same effect as “X=3”. This is the rvalue of Y. X is being used for its place in memory. This is the lvalue of X.

Expressions with lvalues and rvalues An expression has an lvalue/rvalue if it can be placed on the left/right side of an assignment. A[I+J] has both. A[I+J] = X; // OK X = A[I+J]; // OK f(I) or I+J has only an rvalue. X=f(I); // OK f(I) = X // Not OK.

Value vs. reference model of variables Value model: A variable contains a value. Reference model: A variable refers to an object with a variable. Variables have to be dereferenced to get their rvalue; however, the dereferencing is implicit in most languages, so the distinction is subtle.

Java uses both int I; // value model Integer J; // reference model. J = new(Integer(13)); // creates an object with value 13, J points // to the object.

Static variables Variable maintains the same value between successive calls to the function. Allocated with fixed address. int random() { static int r, first = TRUE; if (first) r = intTime(); // absolute time in int form first = FALSE; r = nextRandom(r); return r; }

Side effects A call to a function F has a side effect if either During the call, there is a change to the value of some object that existed before F. I/O occurs during the call. Otherwise the call is side-effect free. If F is side-effect free, then the only thing that matters about F is the value that it returns. (Note: If F creates a heap structure and returns a pointer to it, that is not considered a side effect.)

Expression side effects An expression has a side effect if it changes the value of some object. E.g. “x++” has a side effect. “x+1” does not. Assignment statements always have side effects. In pure functional programming, there are no side effects.

Function parameters int B(int I,J) // I and J are formal parameters { … } int A () { … B(Q+1, M[52]); // Q+1 and M[52] are … } // actual parameters.

Pass by value Formal parameters are local variables to callee. Values of the actual parameters are copied to the formal parameters when function is called. Conceptually simple Values of actual parameters are protected. Costly copying if parameters are large. Can only return 1 value.

Pass by copy-restore Formal parameters are local variables of callee. Values are copied from actual parameter to formal parameter when function called, and copied back when function returns. Can return multiple values. Actual parameters must have l-values. (If not, either forbidden or just skip restore step.)

Pass by reference Formal parameters are pointers to actual parameters. Every use of the formal parameter is implicitly dereferenced. Actual parameters must have l-values. (If not, either prohibit, or create temporary unnamed variable in caller). Aliasing problem (see below) Can’t use in remote procedure call.

Example PBV | PBCR PBRef I J I J I J int I = 1; void a(int J) { J = 2; J = J+I; } void main () { A(I); print(I); } 1 3 4

Aliases In previous example with Pass by Reference, I and J are aliases, two variables (or expressions) with the same lvalue. Aliases are generally created with pointers or with pass-by-reference. Greatly complicate analysis (compiler, human, or formal).

Achieving PBRef in PBV The effect of pass by reference can be achieved in languages with pass by value by using pointers. However, the dereferencing has to be explicit

Example: Pass by Ref in Pascal procedure swap(var A,B: integer) /* A and B are declared PBRef */ integer T; begin T=A; A=B; B=T end procedure foo() integer I,J; begin I = 2; J=3; swap(I,J); end

Example: Swapping in C void swap(int *A, *B) { int T; T = *A; *A = *B; *B = T; } void foo() { int I,J; I = 2; J = 3; swap(&I, &J); }

Named parameters and defaults Ada: drawLetter(Font => Helvetica, Size=>12, Color=>Blue, X=> 5.0, Y=>2.0) drawLetter(X,Y: Float; Font : font := Arial; Color : color := Black; Size : Integer := 10)

Variable passing in various languages C: Only pass by value. Arrays are pointers. Pascal: User specifies pass by value or pass by reference. Ada. Specify mode of parameter: in: Pass by value. out: Value not passed but returned in out: Value passed and returned. May be either PBVR or PBRef. Error if it makes a difference.

Macros in C Substitute specified text. With or without arguments. #define MaxLength 10 #define Max(A,B) (((A) < (B)) ? (B) | (A)) #define begin { #define end ;}

Problems with C macros Need extra level of parentheses in max to avoid precedence issues. Multiple evaluation: max(i++,j++) expands to (((i++) < (j++)) ? (i++) | (j++)) Either i or j is incremented twice.

More problems with C macros Variable name conflicts #define swap(A,B) { int I; I=A; A=B; B=I; } { int I=2, J=3; swap(I,J); } expands to { int I=2, J=3; { int I; I=I; I=B; B=I; }}

Macros in LISP A macro in many versions of LISP (not Scheme) can call any LISP function to generate the substituted text. Form of a LISP program is just a list structure of symbols, which is also the major data structure of LISP. Therefore it’s easy to write a preprocessor of arbitrary kind.

Inline expansion of function Body of callee is substituted into text of caller. Differs from macro in that: Callee is defined as a function in usual form. Compiler guarantees that the semantics of the substituted text is the same as if the actual function were called. Saves the overhead of function calling sequence. Generally user suggestion, not a requirement.

Lazy evaluation Lazy evaluation: Actual parameters are not evaluated until the first time they are used. (The point is that they may never be used.)

Call by name Actual parameters are evaluated, in caller environment every time they are used (Algol 60). We will discuss implementation of lazy evaluation and call by name when we discuss closures.

Strange effects of call by name function swap(int X,Y) { I = 3; A[3]=4; { int T; swap(I,A[I]); X  I, Y  A[I] T = X; T=3. X = Y; X=I=4. Y = T; Y=A[4]=3. }