Parameters. However, one important class of variables is still missing Þ parameters. Terminology: Example: Java, C, C++ Chap 18.

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

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

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

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

Chapter 9 Subprograms

a data type is Types

Programming Languages: Lecture 11

Memory and C++ Pointers

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

CSE 307: Principles of Programming Languages

CS 314 Principles of Programming Languages. Lecture 13

Programming Languages: Lecture 12

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

10. Abstract Data Types

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

CMSC 132: Object-Oriented Programming II

CS 314 Principles of Programming Languages

POINTER & REFERENCE VARIABLES

Chapter 2. Procedural Programming

CSCI312 Principles of Programming Languages!


References and pointers

Exam 1 - (20 points)

Lecture 3. Variables. Variables

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

CS558 Programming Languages

1st Semester MTCE 601A COMPUTER SYSTEM SOFTWARE

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Principles of Programming Languages

Pointers, Dynamic Data, and Reference Types

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

Chapter 10. Implementing Subprograms ISBN

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Chapter 5 Names, Bindings, Type Checking, and Scopes

Exercise 7 References, Arrays, Vectors

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

names names identifiers variables subroutines constants

Lab5. Wooseok Kim

CMSC 132: Object-Oriented Programming II

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

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

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

Chapter 5 Names, Binding, Type Checking and Scopes

Computational Expression

NOTE: Answer ANY FOUR of the following 6 sections:


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

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

G Programming Languages - Fall 2012

Pointers. Cedric Saule

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

MIDTERM EXAMINATION - CS130 - Spring 2005

COMP 2355 Introduction to Systems Programming

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Programming Languages and Techniques (CIS120)

What have we learned about when we learned about function parameters? 1-1

CS162: Introduction to Computer Science II

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Implementing Subprograms

C Introduction. Comparison w/ Java, Memory Model, and Pointers

COS 140: Foundations of Computer Science

LECTURE 17. Expressions and Assignment

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Chapter-8 DATA TYPES. Introduction. Variable:

Run-time Environments - 2

Lecture 2: C Programm

EI326 ENGINEERING PRACTICE & TECHNICAL INNOVATION (III-G) Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

CS111: PROGRAMMING LANGUAGE II

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

Contents. 8-1 Copyright (c) N. Afshartous

Nested Loops. A loop can be nested inside another loop.

Implementing Subprograms

Chapter 5. Names, Bindings, and Scopes

Principles of Programming Languages

Homework #3 CS2255 Fall 2012

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Object Oriented Design

Types. What is a type?

New Concepts. Lab 7 Using Arrays, an Introduction

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

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

CSE 3302 Programming Languages Lecture 5: Control

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

Chapter 9. Subprograms

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Programming Methodology

Lecture 8 Tao Wang 1

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Data Representation and Storage

Chapter 10. Implementing Subprograms

CA31-1K DIS. Pointers. TA: You Lu

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Chap. 8 :: Subroutines and Control Abstraction

Transcription:

Parameters We have discussed different classes of variables so far: lobal/static variables - Function local or automatic variables -Dynamic, heap allocated variables Chap 18 However, one important class of variables is still missing Þ parameters Terminology: Example: Java, C, C++ Function Definition Function Call int plus (int a, int b) { return a + b; int x = plus(1,2); Formal Parameters Function Body Actual Parameters Observation: in function definitions formal parameters act as placeholders for the values of actual parameters.

Two Fundamental Questions l How is the correspondence between actual and formal parameters established? l How is the value of an actual parameter transmitted to a formal parameter?

Correspondence Most programming languages use positional parameters; the first actual parameter is assigned to the first formal parameter, the second actual parameter is assigned to the second formal parameters, etc. int x = plus(1,2); 1 int plus (int a, int b) { return a + b; 1 2 2

Correspondence Some languages such as Ada provide keyword parameters. Example: Ada FUNCTION Divide(Dividend:Float, Divisor:Float) RETURN Float IS BEGIN RETURN Dividend/Divisor; END... Foo = Divide(Divisor => 2.0, Dividend => 4.0);... 2nd formal parameter becomes 2.0 1st formal Parameter Becomes 4.0

Parameter Value Transmission l We look at three different techniques l By value l By value-result l By reference

I. By Value For by-value parameter passing, the formal parameter is just like a local variable in the of the called method, with one important difference: it is initialized using the value of the corresponding actual parameter, before the called method begins executing. l Also called copy-in l Simplest method l Widely used l The only method in Java

By Value - Example int plus(int a, int b) { a += b; return a; void f() { int x = 3; int y = 4; int z = plus(x, y); current a: 3 x: 3 b: 4 y: 4 When plus is starting result:? z:?

II. By Value-Result For passing parameters by value-result, the formal parameter is just like a local variable in the activation record of the called method. It is initialized using the value of the corresponding actual parameter, before the called method begins executing. Then, after the called method finishes executing, the final value of the formal parameter is assigned to the actual parameter. l Also called copy-in/copy-out l Actual must have an lvalue Lvalues are values that have addresses, meaning they are variables or dereferenced references to a certain place.

By Value-Result - Example void plus(int a, by-value-result int b) { b += a; void f() { int x = 3; plus(4, x); current When plus is starting a: 4 b: 3 x: 3

By Value-Result - Example void plus(int a, by-value-result int b) { b += a; void f() { int x = 3; plus(4, x); current When plus is ready to return a: 4 b: 7 x: 3

By Value-Result - Example void plus(int a, by-value-result int b) { b += a; void f() { int x = 3; plus(4, x); current a: 4 b: 7 x: 7 When plus has returned

III. By Reference For passing parameters by reference, the lvalue of the actual parameter is computed before the called method executes. Inside the called method, that lvalue is used as the lvalue of the corresponding formal parameter. In effect, the formal parameter is an alias for the actual parameter another name for the same memory location. l One of the earliest methods: Fortran l Most efficient for large objects l Still frequently used; C++ allows you define calls by reference

By Reference - Example void plus(int a, by-reference int b) { b += a; void f() { int x = 3; plus(4, x); current When plus is starting a: 4 b: x: 3

By Reference - Example void plus(int a, by-reference int b) { b += a; void f() { int x = 3; plus(4, x); current When plus has made the assignment a: 4 b: x: 7

Homework l HW#9 see website