Parameter Binding. Value: The formal parameter represents a local variable initialized to the value of the corresponding actual parameter.

Similar documents
UNIT V Sub u P b ro r g o r g a r m a s

G Programming Languages - Fall 2012

Implementing Coroutines with call/cc. Producer/Consumer using Coroutines

Reading Assignment. Lazy Evaluation

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages. ameter Passing Reference Parameter. Call-by-Value Parameters. 28 : Control Structures Parameters

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Parameters. Chapter Eighteen Modern Programming Languages, 2nd ed. 1

References and pointers

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

CSc 520 Principles of Programming Languages

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Assigning to a Variable

Topic 8: Lazy Evaluation

Subprograms. Akim D le Étienne Renault Roland Levillain EPITA École Pour l Informatique et les Techniques Avancées

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

CSE 307: Principles of Programming Languages

Statement Basics. Assignment Statements

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Types, Expressions, and States

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

a data type is Types

Abstraction. Markus Roggenbach. 4. März 2004

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

Principles of Programming Languages

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Procedures, Parameters, Values and Variables. Steven R. Bagley

Review of the C Programming Language

QUIZ. What is wrong with this code that uses default arguments?

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language

Binding and Variables

Java Review. Fundamentals of Computer Science

Chapter 5. Names, Bindings, and Scopes

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Fun facts about recursion

PROCEDURES, METHODS AND FUNCTIONS

About this exam review

known as non-void functions/methods in C/C++/Java called from within an expression.

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

C Language, Token, Keywords, Constant, variable

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Functional Programming. Pure Functional Programming

Programming Languages: Lecture 11

CSCI-1200 Data Structures Fall 2017 Lecture 13 Problem Solving Techniques

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Pointers, References and Arrays

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Chapter 9 :: Subroutines and Control Abstraction

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Pass by Value. Pass by Value. Our programs are littered with function calls like f (x, 5).

Implementing Subprograms

CS102: Variables and Expressions

Chapter 9. Subprograms

Binary Search. Roland Backhouse February 5th, 2001

Expressions and Casting

Properties of an identifier (and the object it represents) may be set at

Review of the C Programming Language for Principles of Operating Systems

Control Abstraction. Hwansoo Han

Intermediate Code Generation Part II

Programming Languages

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

COMP 250 Winter 2011 Reading: Java background January 5, 2011

CS201 Some Important Definitions

Chapter 10. Implementing Subprograms ISBN

Chap. 8 :: Subroutines and Control Abstraction

11/29/17. Outline. Subprograms. Subroutine. Subroutine. Parameters. Characteristics of Subroutines/ Subprograms

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

CS558 Programming Languages

Qualifying Exam in Programming Languages and Compilers

Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

9. Subprograms. 9.2 Fundamentals of Subprograms

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

18-642: Code Style for Compilers

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Run-time Environments -Part 1

Computer System and programming in C

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Semantic Analysis and Type Checking

Organization of Programming Languages CS 3200/5200N. Lecture 09

Chapter 10. Implementing Subprograms

Questions, Projects, and Labs

CARNEGIE MELLON UNIVERSITY DEPT. OF COMPUTER SCIENCE DATABASE APPLICATIONS

Java Basic Programming Constructs

Goals of this Lecture

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

CIS133J. Working with Numbers in Java

Lecture Notes for Chapter 2: Getting Started

CS31 Discussion 1E Spring 17 : week 08

Transcription:

Parameter Binding Almost all programming languages have some notion of binding an actual parameter (provided at the point of call) to a formal parameter (used in the body of a subprogram). There are many different, and inequivalent, methods of parameter binding. Exactly which is used depends upon the programming language in question. Parameter Binding Modes include: Value: The formal parameter represents a local variable initialized to the value of the corresponding actual parameter. 74

Result: The formal parameter represents a local variable. Its final value, at the point of return, is copied into the corresponding actual parameter. Value/Result: A combination of the value and results modes. The formal parameter is a local variable initialized to the value of the corresponding actual parameter. The formal s final value, at the point of return, is copied into the corresponding actual parameter. Reference: The formal parameter is a pointer to the corresponding actual parameter. All references to the formal parameter indirectly access the corresponding actual parameter through the pointer. 75

Name: The formal parameter represents a block of code (sometimes called a thunk) that is evaluated to obtain the value or address of the corresponding actual parameter. Each reference to the formal parameter causes the thunk to be reevaluated. Readonly (sometimes called Const): Only reads of the formal parameter are allowed. Either a copy of the actual parameter s value, or its address, may be used. 76

What Parameter Modes do Programming Languages Use? C: Value mode except for arrays which pass a pointer to the start of the array. C++: Allows reference as well as value modes. E.g., int f(int a, int& b) C#: Allows result (out) as well as reference and value modes. E.g., int g(int a, out int b) Java: Scalar types (int, float, char, etc.) are passed by value; objects are passed by reference (references to objects are passed by value). Fortran: Reference (even for constants!) Ada: Value/result, reference, and readonly are used. 77

Example void p(value int a, reference int b, name int c) { a=1; b=2; print(c) } int i=3, j=3, k[10][10]; p(i,j,k[i][j]); What element of k is printed? The assignment to a does not affect i, since a is a value parameter. The assignment to b does affect j, since b is a reference parameter. c is a name parameter, so it is evaluated whenever it is used. In the print statement k[i][j] is printed. At that point i=3 and j=2, so k[3][2] is printed. 78

Why are there so Many Different Parameter Modes? Parameter modes reflect different views on how parameters are to be accessed, as well as different degrees of efficiency in accessing and using parameters. Call by value protects the actual parameter value. No matter what the subprogram does, the parameter can t be changed. Call by reference allows immediate updates to the actual parameter. Call by readonly protects the actual parameter and emphasizes the constant nature of the formal parameter. 79

Call by value/result allows actual parameters to change, but treats a call as a single step (assign parameter values, execute the subprogram s body, update parameter values). Call by name delays evaluation of an actual parameter until it is actually needed (which may be never). 80

Call by Name Call by name is a special kind of parameter passing mode. It allows some calls to complete that otherwise would fail. Consider f(i,j/0) Normally, when j/0 is evaluated, a divide fault terminates execution. If j/0 is passed by name, the division is delayed until the parameter is needed, which may be never. Call by name also allows programmers to create some interesting solutions to hard programming problems. Consider the conditional expression found in C, C++, and Java: (cond? value1 : value2) 81

What if we want to implement this as a function call: condexpr(cond,value1,value2) { if (cond) return value1; else return value2; } With most parameter passing modes this implementation won t work! (Why?) But if value1 and value2 are passed by name, the implementation is correct. 82

Call by Name and Lazy Evaluation Call by name has much of the flavor of lazy evaluation. With lazy evaluation, you don t compute a value but rather a suspension a function that will provide a value when called. This can be useful when we need to control how much of a computation is actually performed. Consider an infinite list of integers. Mathematically it is represented as 1, 2, 3,... How do we compute a data structure that represents an infinite list? 83

The obvious computation inflist(int start) { return list(start, inflist(start+1)); } doesn t work. (Why?) A less obvious implementation, using suspensions, does work: inflist(int start) { return list(start, function() { return inflist(start+1); }); } Now, whenever we are given an infinite list, we get two things: the first integer in the list and a suspension function. When called, this function will give you the rest of the infinite list (again, one more 84

value and another suspension function). The whole list is there, but only as much as you care to access is actually computed. 85

Eager Parameter Evaluation Sometimes we want parameters evaluated eagerly as soon as they are known. Consider a sorting routine that breaks an array in half, sorts each half, and then merges together the two sorted halves (this is a merge sort). In outline form it is: sort(inputarray) {... merge(sort(lefthalf(inputarray)), sort(righthalf(inputarray)));} This definition lends itself nicely to parallel evaluation: The two halves of an input array can be sorted in parallel. Each of these two halves can again be split in two, allowing parallel sorting of four quarter-sized arrays, 86

then leading to 8 sorts of 1/8 sized arrays, etc. But, to make this all work, the two parameters to merge must be evaluated eagerly, rather than in sequence. 87