1 Parameter Passing (Section 8.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.

Slides:



Advertisements
Similar presentations
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Advertisements

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.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
1 CHAPTER 9 SUBPROGRAM. 2 SUBPROGRAM Topics: b Definitions of subprogram b general subprogram characteristics b parameters b Functions and procedures.
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.
1 Subroutines and Control Abstraction. 2 Control Abstraction Abstraction Abstraction associate a name N to a program part P associate a name N to a program.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 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.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
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.
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
High-Level Programming Languages: C++
1 Subroutines and Control Abstraction Aaron Bloomfield CS 415 Fall 2005.
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,
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
1 Symbol Table and Subroutine Closures (Sections 3.3 & 3.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos.
Runtime Organization.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Subprograms subroutines, procedures, functions, methods.
Programming Languages and Paradigms Imperative Programming.
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.
CSI 3125, Subprograms, page 1 Subprograms General concepts Parameter passing Functions Subprograms as parameters.
Lecture 19: Control Abstraction (Section )
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
CSE 3302 Programming Languages
LECTURE 20 Parameter Passing. PARAMETER PASSING Parameterized subroutines accept arguments which control certain aspects of their behavior or act as data.
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 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
LECTURE 20 Parameter Passing. PARAMETER PASSING Parameterized subroutines accept arguments which control certain aspects of their behavior or act as data.
Parameter passing Module 12.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Principles of programming languages 4: Parameter passing, Scope rules
CS 3304 Comparative Languages
Chapter 9 :: Subroutines and Control Abstraction
CS 3304 Comparative Languages
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSC 533: Programming Languages Spring 2015
CSE 3302 Programming Languages
Chapter 9 Subprograms Fundamentals of Subprograms
COP4020 Programming Languages
Parameter transmission
PZ09B - Parameter transmission
COP4020 Programming Languages
Parameter transmission
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Parameter transmission
Presentation transcript:

1 Parameter Passing (Section 8.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott

2 Parameter Passing Notation for function calls:Notation for function calls: –prefix notation f(a, b) –Cambridge Polish notation(f a b) –infix notation »operators »can be requested explicitly in ML: infixr 8 tothe;(* exponentiation *) fun x tothe 0 = 1.0 | x tothe n = x * (x tothe (n-1)); | x tothe n = x * (x tothe (n-1)); Control abstraction - example:Control abstraction - example: if a > b then max := a else max := b;(* Pascal *) (if (> a b) (setf max a) (setf max b)); Lisp (a > b) ifTrue: [max b) ifTrue: [max <- a] ifFalse: [max <- b]."Smalltalk" –Lisp and Smalltalk - no syntactic distinction between control statements and function calls

3 Parameter Modes Issues:Issues: –implementation mechanism (what is it passed?) »value »reference »name »closure –legal operations (inside subroutine) »read »write –change performed on actual parameter? »yes »no –change visible immediately? »yes »no

4 Parameter Modes Main parameter-passing modes:Main parameter-passing modes: –call by value »the value of actual parameter is copied into formal parameter »the two are independent –call by reference »the address of actual parameter is passed »the formal parameter is an alias for the actual parameter SpeedSpeed SafetySafety –better to pass a large object by reference –if a subroutine is allowed to change the actual parameter - pass it by reference Semantic issue:Semantic issue: –argument passed by reference - is it because it's large, or because changes should be allowed? –what if we want to pass a large argument, but not to allow changes?

5 Parameter Modes C –everything is passed by value –arrays are pointers - what is passed by value is a pointer –to allow "call by reference" - must pass the address explicitly as a pointer: void swap (int * a, int * b) { int t = *a; *a = *b; *b = t; }... swap (&v1, &v2); –what if we want to change a pointer? int * p; void my_allocate (int ** x) { *x = new int;...} my_allocate (&p); int * p; void my_allocate (int * x) { x = new int;...} my_allocate (p); Wrong

6 Parameter Modes C –Permissible operations – –Change on actual parameter – –Alias – read and write no no –Speed - better to pass the address of a large object –How do we prohibit changes to the object? void f (const huge_record * r){... } r pointer to constant huge_record –How do we define a constant pointer to huge_record? huge_record * const r

7 Parameter Modes PascalPascal –programmer's choice - call by value or call by reference procedure ( var a : integer, b : integer);(* a passed by reference *)...(* b passed by value *) –if an array is passed without var - it will be passed by value!! –var should be used with: »arguments that need to be changed »large arguments –no mechanism to prohibit changes to an argument passed by reference

8 Parameter Modes Modula-3Modula-3 –call by value (small objects) or call by reference (large ones) –READONLY mode can be specified to prohibit changes FortranFortran –everything is passed by reference –does not require actual parameter to be a l-value –if it's a constant: »creates a temporary location to hold it »allowed to change the temporary Languages with reference model (Smalltalk, Lisp, Clu)Languages with reference model (Smalltalk, Lisp, Clu) –everything is a reference anyway –“call by sharing”

9 Parameter Modes AdaAda –three parameter modes: »in - read only »out - write only »in out - read and write –for scalar types - always pass values –call by value/result »if it's an out or in out parameter - copy formal into actual parameter upon return »change to actual parameter becomes visible only at return

10 Parameter Modes AdaAda –for composite types – pass either a value or a reference –program is "erroneous" if the results are different: –Passing values – –Passing addresses – print 4 print 5

11 Parameter Modes C++C++ –same modes as in C, plus references: int i; int &j = i; i = 2; j = 3; cout << i;// void swap (int & a, int & b) { int t = a; a = b; b = t; }... swap (v1, v2); –safety - use const to prohibit changes to actual parameter –references - can be used not only for parameters: prints 3 –implementation: i is an integer, j is a pointer to i –semantic: both are treated as integers (same operations apply to both)

12 Parameter Modes C++C++ –can also return references from functions: cout << a << b << c; // equivalent to: –if it returned a pointer: *(*(cout.operator<< (a)).operator<< (b)).operator<< (c); // or equivalently (no more elegant syntax): ((cout.operator<< (a)).operator<< (b)).operator<< (c); // the << operator returns a reference to the output stream *(*(cout << a) << b) << c;

13 Call by Name Call by name (Algol 60, Simula)Call by name (Algol 60, Simula) –parameters are re-evaluated in the caller's referencing environment every time they are used –similar to a macro (textual expansion) –pass a hidden routine (thunk) - re-evaluates the parameter –Example (Jensen's device): To evaluate the sum: Call: y := sum (3*x*x - 5*x + 2, x, 1, 10);

14 Summary