PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ09B - Parameter transmission Programming Language Design.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
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 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.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
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.
PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05B - Type equality Programming Language Design and.
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
PZ11A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ11A - Exception handling Programming Language Design.
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,
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.
Subprograms subroutines, procedures, functions, methods.
PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06C - Polymorphism Programming Language Design and.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
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
CSE 3302 Programming Languages
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
1 Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Chapter 9 Subprograms.
PZ11A Programming Language design and Implementation -4th Edition
Functions.
Principles of programming languages 4: Parameter passing, Scope rules
Chapter 9 :: Subroutines and Control Abstraction
PZ06C - Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3 PZ06C.
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSC 533: Programming Languages Spring 2015
PZ09A - Activation records
CSE 3302 Programming Languages
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Polymorphism Programming Language Design and Implementation
Parameter transmission
Polymorphism Programming Language Design and Implementation
PZ09B - Parameter transmission
Parameter transmission
Parameter transmission
Polymorphism Programming Language Design and Implementation
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSC 533: Programming Languages Spring 2018
Parameter transmission
CSC 533: Programming Languages Spring 2019
PZ05B - Type equality Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 6.4 PZ05B.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Parameter transmission
Polymorphism Programming Language Design and Implementation
Presentation transcript:

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 9.3

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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 value Call by reference Call by name Call by result (or value-result)

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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.

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Call by Value The formal parameter acts as a local variable that is initialized with the value of the actual parameter. This method is most common and considered most safe (no side effects). C has call by value. int total(int val) { int sum = 0; while (val > 0) { sum = sum + val; val--; } return sum; } \\\\ int x,y=10; x = total(y);// value of y is not changed

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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.

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Call by Reference The problem with call by value is that the subprogram can affect only one value for the calling program. Also, if an array is passed as a parameter (because we want to operate on some of the values) the entire array is copied to the subprogram parameter area at run time (time consuming if done extensively and uses space on the stack). Therefore, in many languages, call by reference is provided as an option. In call by reference, the address of the actual parameter is passed to the subprogram.

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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.

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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) C++: default is call by value except for arrays which are call by reference

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Call by Value-Result 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.

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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;

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Call by Value-Result Call by value-result and call by reference seem to have the same result (except concurrency and aliasing). However, call by value-result has several advantages over call by reference. Within the subprogram, all references are to local variables, which results in faster execution. If there are concurrent threads, call by value-result will prevent other threads from seeing intermediate results. In Java, parameters are always call by value, objects are accessed by reference, so the effect of passing an object is call by reference.

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Call by Name Some languages use call by name which substitutes the text of the actual parameter for each occurrence of the formal parameter. In call by reference, the address is calculated once at the point of the call and in call by value, the value is determined once at the point of the subprogram call. In call by name, the actual parameter is unevaluated and is recalculated each time the formal parameter is used during the execution of the subprogram. Most of the time, this has the same effect as call by reference. The exception is when subscripting and aliasing are used. Fun1(A, B+3, Fun2(A))

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 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)

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Examples of Call by Name 1. P(x,y) {x = x + 1; y=x;} Seems simple enough … X=1; Y = 2;P(X,Y); write(X)  means X = X+1;Y=Xwrite(X)  prints 2 2. int A[10]; for(I=0; I<10; I++) {A[I]=I;}; I=J=1; P(J,A[I])  J = J+1; A[I]=J  A[1], J set to 2 3. But: What is: P(I,A[I])? P(A[I],I)  I=I+1; A[I] = I  I=1+1; A[I] = I  I=2; A[2] = 2; 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.

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Call by Name Because of the extra calculations that must be done at run time, this method is not used very much, however, it is used in the C,C++ preprocessor for macro expansion. Call by name (macro style) 1.Actual parameters are textually substituted for the formals. 2.The resulting procedure is textually substituted for the call. #define MAX(a,b) ( (a) > (b) ? (a) : (b) ) // j = MAX(x,y); // int, float, etc.

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Call by Name #define MAX(a,b) ( (a) > (b) ? (a) : (b) ) // Be careful to parenthesize all parameters in the definition. j = MAX(x = x + 5, y+5); Beware of strange side effects: int a = 1; b = 0; MAX(a++, b);// a is incremented twice MAX(a++, b+10);// a is incremented once

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Example of parameter passing Main{ int A,B,C,D,M[3]={0,0,0}; A = 1; B = 5; C = 8; D = 9; P(A, B, C, D, M[A]); write(A, B, C, D,M[0],M[1],M[2]); P(U, V, W, X) {V = U+A; W = A+B; A = A+1; X = A+2;Y = 5; write(U, V, W, X, Y)} Fill in table assume parameters are of the given type: U V W X Y A B C D M[0] M[1] M[2] Call by name Call by reference Call by value Call by result When do call by name and call by reference differ? When L-value can change between parameter references. E.g., P(I, A[I])