Parameter Passing Methods



Similar documents
Parameter Passing. Standard mechanisms. Call by value-result Call by name, result

Parameter passing in LISP

History OOP languages Year Language 1967 Simula Smalltalk

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Chapter 5 Names, Bindings, Type Checking, and Scopes

Programming Language Features (cont.) CMSC 330: Organization of Programming Languages. Parameter Passing in OCaml. Call-by-Value

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Parameter Passing in Pascal

Class 16: Function Parameters and Polymorphism

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

CSCI 3136 Principles of Programming Languages

Moving from C++ to VBA

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Compiler Construction

Stack Allocation. Run-Time Data Structures. Static Structures

Lecture 1: Introduction

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CSC Software II: Principles of Programming Languages

Functional Programming

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

CSE 130 Programming Language Principles & Paradigms

CS2210: Compiler Construction. Runtime Environment

Chapter 9. Subprograms

J a v a Quiz (Unit 3, Test 0 Practice)

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

The C Programming Language course syllabus associate level

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Java Interview Questions and Answers

Machine-Code Generation for Functions

User Guide Thank you for purchasing the DX90

OpenACC 2.0 and the PGI Accelerator Compilers

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

13 Classes & Objects with Constructors/Destructors

Programming Languages

Unit Testing & JUnit

AP Computer Science Java Subset

Testing, Debugging, and Verification

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Lecture 9. Semantic Analysis Scoping and Symbol Table

Programmierpraktikum

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Fundamentals of Programming Languages

COMPUTER SCIENCE 1999 (Delhi Board)

Engineering Problem Solving

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

01 Introduction. The timeline

Chapter 13 Storage classes

Django Assess Managed Nicely Documentation

Static Taint-Analysis on Binary Executables

Statements and Control Flow

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

ELEG3924 Microprocessor Ch.7 Programming In C

CS 106 Introduction to Computer Science I

MATLAB Tutorial. Chapter 6. Writing and calling functions

Instructor Özgür ZEYDAN BEU Dept. of Enve. Eng. CIV 112 Computer Programming Lecture Notes (1)

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

C++ INTERVIEW QUESTIONS

JavaScript. JavaScript: fundamentals, concepts, object model. Document Object Model. The Web Page. The window object (1/2) The document object

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Basic Object-Oriented Programming in Java

Time Limit: X Flags: -std=gnu99 -w -O2 -fomitframe-pointer. Time Limit: X. Flags: -std=c++0x -w -O2 -fomit-frame-pointer - lm

CSE 307: Principles of Programming Languages

Java CPD (I) Frans Coenen Department of Computer Science

Computational Mathematics with Python

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

4 Sums of Random Variables

Explain the relationship between a class and an object. Which is general and which is specific?

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Programming Language Theory

Geometric Transformations

03 - Lexical Analysis

>

McGraw-Hill The McGraw-Hill Companies, Inc.,

Problems and Measures Regarding Waste 1 Management and 3R Era of public health improvement Situation subsequent to the Meiji Restoration

14 Financial. Functions. Financial Functions Contents

Computational Mathematics with Python

Section IV.1: Recursive Algorithms and Recursion Trees

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Chapter 6: Programming Languages

Section 6.1 Joint Distribution Functions

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

An Incomplete C++ Primer. University of Wyoming MA 5310

Transcription:

Parameter Passing Methods Procedural abstraction Parameter passing methods pass by value pass by result pass by value-result pass by reference aliasing pass by name Procedures/functions as arguments 1

Procedures Modularize program structure Argument: information passed from caller to callee (actual parameter) Parameter: local variable whose value (sometimes) is received from caller (formal parameter) Procedure declaration name, formal parameters, procedure body with local declarations and statement list, optional result type void translatex(point *p, int dx) 2

Parameter Association Positional association Arguments associated with formals one-by-one E.g., C, Pascal, Scheme, Java Keyword association E.g., Ada uses a mixture procedure plot (x,y: in real; penup: in boolean). plot (0.0, 0.0, penup=> true).plot (penup=>true, x=>0.0, y=>0.0) Similarly for common lisp 3

Parameter Passing Modes pass by value C, Pascal, Ada, Scheme, Algol68 pass by result Ada pass by value-result (copy-in, copy-out) Fortran, sometimes Ada pass by reference Fortran, Pascal var params, sometimes Cobol pass by name (outmoded) Algol60 4

Pass by Value { c: array [1..10] of integer; m,n : integer; procedure r (k,j : integer) begin k := k+1; j := j+2; end r; m := 5; n := 3; r(m,n); write m,n; Output: 5 3 By Value: k j 5 3 6 5 5

Pass by Value Advantages Argument protected from changes in callee Disadvantages Copying of values takes execution time and space, especially for aggregate values 6

Pass by Result { c: array [1..10] of integer; m,n : integer; procedure r (k,j : integer) begin k := k+1; j := j+2; end r; m := 5; n := 3; r(m,n); write m,n; Error in procedure r: can t use parameters which are uninitialized! 7

Pass by Result Assume we have procedure p(k, j : int) with k and j as result parameters. what is the interpretation of p(m,m)? Assume parameter k has value 2 and j has value 3 at end of p. What value is m on return? 8

Pass by Value-Result { c: array [1..10] of integer; m,n : integer; procedure r (k,j : integer) begin k := k+1; j := j+2; end r; m := 5; n := 3; r(m,n); write m,n; Output: 6 5 By Value-Result k j 5 3 6 5 9

Pass by Value-Result { c: array [1..10] of integer; m,n : integer; procedure r (k,j : integer) k j begin 2 2 k := k+1; 3 4 j := j+2; What element of c end r; has its value changed? /* set c[m] = m */ c[2]? c[3]? m := 2; r(m, c[m]); write c[1], c[2],, c[10]; 10

Pass by Reference { c: array [1..10] of integer; m,n : integer; procedure r (k,j : integer) begin k := k+1; k j j := j+2; --> m -->n end r; m n 5 3 m := 5; n := 3; r(m,n); write m,n; 6 5 Value update happens in storage of the caller while callee is executing 11

Comparisons Value-result Has all advantages and disadvantages of value and result together Reference Advantage: is more efficient than copying Disadvantage: can redefine constants r(0, X) will redefine the constant zero in old Fortran 66 compilers Leads to aliasing: when there are two or more different names for the same storage location Side effects not visible from code itself 12

Aliasing: by Reference { y: integer; procedure p(x: integer) { x := x + 1; x := x + y; y := 2; p(y); write y; y 2 3 6 output: 6 x -->y during the call, x and y are the same location! 13

No Aliasing: Value-Result { y: integer; procedure p(x: integer) { x := x + 1; x := x + y; y := 2; p(y); write y; y 2 5 output: 5 x 2 3 5 14

Another Aliasing Example { j, k, m :integer; procedure q( a, b: integer) { b := 3; m := m *a;... s1: q(m, k); s2: q(j, j); global-formal aliases: <m,a> <k,b> associations during call S1; formal-formal aliases: <a,b> during call S2; 15

Pass by Reference Disadvantage: if an error occurs, harder to trace values since some side-effected values are in environment of the caller What happens when someone uses an expression argument for a by reference parameter? (2*x)?? 16

Pass by Name { c: array [1..10] of integer; m,n : integer; procedure r (k,j : integer) begin k := k+1; j := j+2; end r; /* set c[n] to n */ m := 2; r(m,c[m]); write m,n; m:= m+1 m c[ ] 2 1 2 3 4 5 6 7 8 9 10 3 c[m] := c[m] + 2 1 2 5 4 5 6 7 8 9 10 17

Pass by Name Algol60 device Deferred calculation of the argument until needed; like textual substitution with name clashes resolved THUNK - evaluates argument in caller s environment and returns address of location containing the result Characteristics Inefficient Same as pass by reference for scalars 18

Procedures as Parameters To type check the call, need the full function signature of the function argument <function name> : <vector of parameter types> Æ<return type> e.g., translatex:(point *, int) Æ void procedure q( x: integer; function s (y,z: integer):integer) s takes 2 integer arguments and returns an integer! 19

Example { m, k : integer; procedure q(x : integer; function s(y,z: integer): integer) { k, l : integer; s(); /*call to function parameter s */ /* end of q*/ integer function f(w,v: integer) { w := k*v; /* which k is this? k or k?*/ q(m, f); 20