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

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

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

Programming Languages: Lecture 11

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

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

CS 345. Functions. Vitaly Shmatikov. slide 1

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

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

Chapter 9 Subprograms

IA010: Principles of Programming Languages

CSc 520 Principles of Programming Languages

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

Run-time Environments -Part 1

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

CSc 520 Principles of Programming Languages

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

G Programming Languages - Fall 2012

9. Subprograms. 9.2 Fundamentals of Subprograms

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Implementing Subprograms

Principles of Programming Languages

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

Programming Languages: Lecture 12

Chapter 10. Implementing Subprograms ISBN

References and pointers

Weeks 6&7: Procedures and Parameter Passing

Chapter 5. Names, Bindings, and Scopes

NOTE: Answer ANY FOUR of the following 6 sections:

Chapter 9 Subprograms

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

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

Binding and Variables

Principles of Programming Languages

Chapter 5 Names, Binding, Type Checking and Scopes

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

Chapter 10. Implementing Subprograms

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

CSCI312 Principles of Programming Languages!

Names, Bindings, Scopes

CS 314 Principles of Programming Languages

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

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

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

Chapter 9. Subprograms

Chapter 5 Names, Bindings, Type Checking, and Scopes

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

CS558 Programming Languages

CS 314 Principles of Programming Languages. Lecture 13

Lecture 2: C Programm

Chapter 9. Subprograms

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Arrays and Pointers (part 1)

Informatica 3 Syntax and Semantics

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Statement Basics. Assignment Statements

Arrays and Pointers (part 1)

C Programming for Engineers Functions

Subroutines and Control Abstraction. CSE 307 Principles of Programming Languages Stony Brook University

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

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

PROCEDURES, METHODS AND FUNCTIONS

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Implementing Subprograms

Parameter passing. Parameter: A variable in a procedure that represents some other data from the procedure that invoked the given procedure.

Topic IV. Block-structured procedural languages Algol and Pascal. References:

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

procedure definition (or declaration - some make a distinction, we generally won t), e.g., void foo(int x) {... }

Programmiersprachen (Programming Languages)

CS201- Introduction to Programming Current Quizzes

Type Bindings. Static Type Binding

Memory Management and Run-Time Systems

Computer Science & Engineering 150A Problem Solving Using Computers

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

22c:111 Programming Language Concepts. Fall Types I

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

Expressions and Assignment

CSE 3302 Programming Languages Lecture 5: Control

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Concepts Introduced in Chapter 7

Subprogram Concept. COMP3220 Principle of Programming Languages. Zhitao Gong Spring

Evolution of Programming Languages

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

SYSC 2006 C Winter 2012

Pointers as Arguments

Functions in C C Programming and Software Tools


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

Abstraction. Markus Roggenbach. 4. März 2004

New Concepts. Lab 7 Using Arrays, an Introduction

Programming Languages

Computers in Engineering COMP 208. Subprograms. Subroutines. Subroutines Michael A. Hawker

Introduction to Scientific Computing and Problem Solving

Review of the C Programming Language

Lecture 3: C Programm

Programming Languages Third Edition. Chapter 7 Basic Semantics

Transcription:

UNIT V SubPrograms

Outline Subprograms Parameter Passing Parameter correspondence Main Issues when designing subroutine in programming languages Parameter passing techniques

Characteristics of Subprogram Each program has a single entry point The calling program unit is suspended during execution of the called subprogram, which implies that there is only on subprogram in execution Control always return to the caller when subprogram execution terminates The two fundamental types of subprograms are: procedure functions

Basic Definitions Structure or definition of a subprogram construct is: special-word name (list of parameter) Fortran: subroutine Addr (parameters) Ada: procedure Addr (parameters) C: void Addr (parameters)

Parameters Formal parameters -> parts of the specification Actual parameters ex) formal parameter void swap (first, second) { int temp; temp = first; first = second; second = temp; } end swap ---------- ---------- actual parameter swap (x, y);

Parameters The parameters in the subprogram header are called formal parameters. Parameters include as arguments when the subroutine is called are named actual parameters note: a subroutine that return a value is called a function A subroutine that does not return a value is usually called a procedure or method Most programming languages require subroutine to be declared before they are used (forward)

Parameter Correspondence Establishing the correspondence between actual parameters and formal parameters 1. Positional Correspondence The correspondence is established by pairing actual and formal parameters based on the positions of the actual and formal parameter list.

Parameter Correspondence 2. Correspondence by explicit name In Ada the formal parameter is paired with each actual parameter they may be named explicitly in the calling statement. example: sub (y => B, x=> 32); formal actual

Parameter Correspondence 3. Mixed keyword (Explicit name) and positional Most languages that support keyword parameters allow both: Ada, Fortran, Dylan, Python The first parameter in a list can be positional, and the remainder can be keyword parameters. ex) my_sub (Length, sub=>b, List =>my_array);

Parameter Correspondence 4. Optimal parameters Formal parameter list includes default values to be used if the corresponding actual parameter is missing. This gives a very short way of writing certain kinds of overloaded function definitions

Example in C++ int f( int a =1, int b=2, int c=3) {body} int f() { f(1, 2, 3); } int f( int a) { f(a, 2, 3); } int f( int a, int b) { f(a, b, 3); } int f( int a, int b, int c) { f(a, b, c); }

Unlimited Parameter Lists Some languages allow actual parameter lists of unbounded length: C, C++, and scripting languages like JavaScript, Python, and Perl Library routines must be used to access the excess actual parameters A hole in static type systems, since the types of the excess parameters cannot be checked at compile time int printf (char *format,...) { body }

Example in Ada function Compute_Pay (Income : Float; Exemptions : Integer := 1; Tax_Rate : Float) return Float; Pay := Compute_Pay (2000.0, Tax_Rate => 0.15);

Main Issues when designing subroutine in programming languages What parameter passing methods are used? Are the types of the actual parameters checked against the types of the formal parameters? Check for error condition Are local variables statically or dynamically allocated? Can subroutines definitions appear on other subroutines definitions?

Main Issues when designing subroutine in programming languages (cont) Can subroutines can be passed as parameters? Can subroutines be nested? If passed as parameters and/or nested, what is the referencing environment of a passed parameter? Can a subroutine be generic?

Parameter Passing Technique 1 Pass-by-Value The value of the actual parameter is associated with the formal parameter It is initialized using the value of the corresponding actual parameter, before the called method begins executing Simplest method Widely used The only method in real Java Changing to a formal parameter do not affect the actual parameter

Changes Visible to the Caller When parameters are passed by value, changes to a formal do not affect the actual But it is still possible for the called method to make changes that are visible to the caller The value of the paramter could be a pointer( in Java, a reference) Then the actual cannot be changed, but the object referred to by the actual can be

Example for Pass by Value (1) int plus(int a, int b) { a += b; return a; a : 3 } void f() { int x = 3; int y = 4; int z = plus(x, y); When plus Is starting Current Activation record b : 4 return address Previous Activation record Result x : 3 y : 4 z :? return address Previous Activation record }

Example for Pass by Value (2) name formal parameter type result function square(x : integer) : integer begin square := x * x end square(2) => 4

Example for Pass by Value (3) procedure swap(a, b); integer a, b, temp; begin temp temp := a; a := b; b := temp; end; swap(x, y); x effect y

Parameter Passing Technique 2 Pass-by-Result The formal parameter is just like a local variable in the activation record of the called procedure( it is uninitialized) After the called method finish execution, the final value of the formal parameter is assigned to the corresponding actual parameter It is also called copy-out It was introduced in ALGOL 68 Sometimes used in Ada

Example for Pass by Result void plus (int a, int b, by-result int c){ c = a + b; } current AR void f(){ int x = 3; int y = 4; int z; plus(x, y, z); } a : 3 x : 3 b : 4 y : 4 c :? z :? RA RA Previous Previous AR AR 1 When plus is starting

Example cont. current AR a : 3 when plus is 2 3 ready to return current x : 3 AR When plus has returned x : 3 b : 4 y : 4 y : 4 c : 7 z :? z : 7 RA RA RA Previous AR Previous AR Previous AR

Parameter Passing Technique 3 Pass-by-Value Result The formal parameter is just like a local variable in the activation record of the called procedure After the called method finish execution, the final value of the formal parameter is assigned to the corresponding actual parameter It is also called copy-in/copy-out It is initialized using the value of the corresponding actual parameter, before the called method begins execution

Example for Pass by Value Result void plus(int a, by-value-result int b){ b += a; } current 1 When plus is starting AR a : 4 x : 3 void f(){ int x = 3; plus(2, x); } b : 3 RA PAR RA PAR

Example cont. current When plus is 2 ready to return 3 When plus has returned AR a = 4 x = 3 x = 7 b = 7 RA PAR RA PAR RA PAR

Parameter Passing Technique 4 Pass-by-Reference The address of the actual parameter is passed to the called method. This address is used as the formal parameter. The formal parameter is an alias for the formal parameter One of the earliest methods: Fortran Most efficient for large object Still frequently used

Picture x reference value copy-out copy-in b reference value x reference value b reference

Example for Pass by Reference (1) void plus (int a, by-reference int b) { b += a; } void f() { int x = 3; plus(4, x); } CAR a = 4 b RA PAR CAR a = 4 b = 7 RA PAR x = 3 RA PAR x = 7 RA PAR 1 When plus Is starting 2 When plus is Ready to return

Implementing Reference void plus( int a, by-reference int b) { b += a; } previous example void f() { int x = 3; plus( 4, x ); } void plus (int a, int *b) { *b += a; } void f() { int x = 3; plus(4, &x); } C implementation By-reference = address by value

Aliasing When two expressions have the same lvalue, they are aliases of each other There are obvious cases: ConsCell x = new ConsCell (0, null); ConsCell y = x; A[ i ] = A[ j ] + A[ k ]; Passing by reference leads to less obvious cases...

Example for Pass by Reference (2) void sigsum( by-reference int n, by-reference int ans) { ans = 0; int I = I; while ( i <= n) ans +=i ++; } Int f() { int g() { int x, y; int x; x = 10; x = 10; sigsum (x, y); sigsum (x, x); return y; return x; } }

Example cont void sigsum(by-reference int n, by-reference int ans) { ans = 0; int i = 1; while (i <= n) ans += i++; } int g() { int x; x = 10; sigsum(x, x); return x; } CAR n : ans : i :? RA PAR When sigsum Is starting X : 10 RA PAR Result :?

Parameter Passing Technique 5 Pass-by-Name Each actual parameter is evaluated in the caller s context, on every use of the corresponding formal parameter Introduce in ALGOL 60 Now unpopular The actual parameters is treated like a Little anonymous function (Thunk) Whenever the called method needs the value of the formal, it calls the Thunk to get it.

Example for Pass by Name (1) procedure swap(a, b); integer a, b, temp; begin temp := a; a := b; b := temp; end A 0 1 2 3 4 5 3 11 10 12.... A[3] = 10 I = 2

Example 1 cont. swap(i, A[3]){ temp := 2 I := A[3] = 10 A[10] := temp } swap(i, A[I]){ temp := 2 I := A[2] = 10 A[10] := temp }

Example for Pass by Name (2) void f( by-name int a, by-name int b){ b = 5; b = a; Thunk i + 1 Thunk } i int g(){ } int i = 3; f ( i + 1, i ); return; CAR when f() is starting a: b: RA PAR i = 3 RA PAR result?

Jensen s Device Real procedure sum(j, lo, hi, Exp) Value lo, hi; Real Exp; Integers j, lo, hi; Begin Real RTN; RTN := 0; FOR I = lo step 1 UNTIL hi do RTN := RTN + Exp; SUM : = RTM End x[ i ] * i

Jensen s Device Example y = 10 i = 1 2 3x - 5x + 2 y = sum(x, I, 10, 3 * x * x - 5 * x + 2) Each time through the loop, evaluation of the expression is actually the evaluation of 2 3x - 5x + 2 Procedure swap (a, b); Integer a, b, temp; Begin temp := a; a := b; b := temp; End

Jensen s Device Example cont. i = 2 x[i] = 5 Effect of calling swap(i, x[i]) Before the call: i = 2, x[2] = 5 What do we expect: i = 5, x[2] = 2 After the call: i = 5, x[2] = 5, x[5] = 2 side effect temp 2 i A[i] = 2 A[i] = A[5] = 2

Thunks Function XjThunk() : Real address VAR Expi : Real Begin Expi := x[ i ] * i XjThunk := address(expi) End

Specification Issues Are these just implementation techniques, or part of the language specification? Depends on the language: Without side-effects, parameter-passing technique may be undetectable by the programmer Even with side effects, some languages specify the parameter passing technique only partially

Without Side Effects Big question are parameters always evaluated, or only if they are really needed? Cost model may also be used by the programmer Is re-evaluation of a formal expensive? Does parameter-passing take time proportional to the size of the object?

With Side Effects A program can detect which parameterpassing technique is being used by the language system But it may be an implementation detail that programs are not supposed to depend on: it may not be part of the specification of the language Case in point: Ada