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

Similar documents
Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

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

9. Subprograms. 9.2 Fundamentals of Subprograms

Chapter 9 :: Subroutines and Control Abstraction

Programming Languages

Chap. 8 :: Subroutines and Control Abstraction

Control Abstraction. Hwansoo Han

Chapter 10. Implementing Subprograms ISBN

Implementing Subprograms

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

Programming Languages: Lecture 12

Chapter 9 Subprograms

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

G Programming Languages - Fall 2012

Principles of Programming Languages

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

IA010: Principles of Programming Languages

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

Implementing Subprograms

CS 345. Functions. Vitaly Shmatikov. slide 1

Programming Languages: Lecture 11

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

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

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Binding and Variables

Chapter 9. Subprograms

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

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

22c:111 Programming Language Concepts. Fall Functions

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

Chapter 9. Subprograms

Chapter 9 Subprograms

CS558 Programming Languages

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

Chapter 5 Names, Binding, Type Checking and Scopes

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

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

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

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

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

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

CSc 520 Principles of Programming Languages

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

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

Informatica 3 Syntax and Semantics

CSCI312 Principles of Programming Languages!

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 10. Implementing Subprograms

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

G Programming Languages - Fall 2012

Chapter 5. Names, Bindings, and Scopes

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

10. Abstract Data Types

NOTE: Answer ANY FOUR of the following 6 sections:

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

CSc 520 Principles of Programming Languages

Programmiersprachen (Programming Languages)

Names, Scopes, and Bindings II. Hwansoo Han

CS 314 Principles of Programming Languages

Names, Bindings, Scopes

Programming Languages, Summary CSC419; Odelia Schwartz

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

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Types. What is a type?

Introduction to Scientific Computing Languages

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Introduction to Scientific Computing Languages

Software II: Principles of Programming Languages. Why Expressions?

Final-Term Papers Solved MCQS with Reference

Concepts Introduced in Chapter 7

CS558 Programming Languages

6. Names, Scopes, and Bindings

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

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

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

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

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

References and pointers

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Lecture 16: Static Semantics Overview 1

Code Generation & Parameter Passing

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

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

Procedure and Object- Oriented Abstraction

Chapter 7. Expressions and Assignment Statements ISBN

CS558 Programming Languages

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Chapter 7. Expressions and Assignment Statements

Statement Basics. Assignment Statements

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

LECTURE 18. Control Flow

Chapter 5 Names, Bindings, Type Checking, and Scopes

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

CSE 3302 Programming Languages Lecture 5: Control

Imperative Programming

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

CS 314 Principles of Programming Languages. Lecture 13

Run-time Environments -Part 1

Object Oriented Paradigm Languages

Transcription:

Control Abstraction Chapter 8 (81 84) Control Abstraction: Subroutines and parameters Programmer defined control structures Subprograms Procedures Functions Coroutines Exception handlers Processes Subprograms Issues related to subprograms How is control transferred to & from the subprogram? Caller explicitly names the subprogram unit Subprogram implicitly transfers control back to caller Caller is suspended during execution of the subprogram main call sub1 call sub1 sub1 end sub1 Subprograms Issues related to subprograms How is data transferred in & out of the subprogram? Parameters Global variables/shared data Function return values Terminology Formal parameter Used in subprogram definition Actual parameter (argument) Used in subprogram call Correspondence of: Formal Actual Positional Keyword Terminology Subprogram definition describes the interface to the subprogram the actions of the subprogram Subprogram header contains its name list of parameters return type Subprogram call is an explicit request for the subprogram to be executed Call By Value A copy of the actual parameter is assigned to the formal parameter at entry Formal parameter behaves as a local variable Algol-68, Pascal, PL/1, Lisp, C++, Java (primitives) Ada IN parameters are a variation which uses call by constant value Statements that change formal parameter are not allowed -1-

Call By Result Formal parameter behaves like a local variable during subprogram execution Upon return, the value of the formal parameter is copied into the actual parameter Ada OUT parameters Call By Value-Result Combines the semantics of Value and Result parameter passage Ada IN OUT parameters IN -2- IN- OUT OUT Comparison of in, out, in-out Call By Reference Formal parameter is an alias for the actual parameter (stores the address of the argument) Access to the formal parameter involves dereferencing FORTRAN, Pascal, PL/1, Algol-68, C (arrays), C++ Ada compilers may use pass by reference for OUT parameters that are structures (arrays, etc) Java object parameters are references The reference cannot be changed, but the contents of the object being referenced can (Call by Sharing) Call By Name Behaves as if the name of the actual parameter is substituted in place of the formal parameter This is an example of very late binding between formal and actual parameters Very flexible, but not efficient or readable Algol-68 Example Notes All arguments, except those passed by value, must be bound to memory cells Generally arguments are bound to formal parameters at subprogram entry The amount of type checking, matching of parameters, etc varies with different languages Call by reference is not necessarily more efficient than call by value-result Subprogram void sub (int x, int y) { x++; y++; print(i, x, y); Main program int main ( ) { int i = 0; a[ ] = {5, 10; sub (i, a[i]); print(i, a[0], a[1]); i = 1; sub(i, i); print(i); Trace using call by value, value result, reference, and name

Parameter Passing C/C++ Parametes Parameters passed by value in C Call by reference can be simulated with pointers void proc(int* x, int y){*x = *x+y proc(&a,b); Directly passed by reference in C++ void proc(int& x, int y) {x = x + y proc(a,b); Figure 83 Parameter passing modes Copyright 2009 Elsevier Default Parameters Ada procedure Proc(x : integer; y := 0 : integer; z := 5 : integer ) Proc(10, 20, 30); Proc(10, 20); -- z gets 5 by default Proc(10); -- y gets 0 and z gets 5 C++ uses a similar approach So does Ruby, but without types declared Java Varargs Java 15+ supports variable length parameter lists: class VarGreeter { public static void printgreeting(string names) { for (String n : names) { Systemoutprintln("Hello " + n + " "); public static void main(string[] args) { printgreeting("phil", "Brian", "Ashley", "Mark"); Ruby also supports this: def proc(*arg) Perl Parameters Passed implicitly as a list (array) sub p { $x = shift; $y = shift; print "$x $y\n"; p(99, 88); Subprograms as Parameters Powerful feature supported by some languages Can be as simple as passing a pointer to the beginning of the subprogram But this will not allow checking parameters of the passed subprogram Ruby closures and blocks are examples of passing subprograms -3-

Subprograms as Parameters The MODULA-2 solution (* Declare a type for procedures with one integer parameter passed by reference *) TYPE ProcType = PROCEDURE( VAR INTEGER); PROCEDURE P( X : INTEGER; P1 : ProcType); Call Semantics (prologue): 1 Save the execution status of the caller 2 Carry out the parameter-passing process 3 Pass the return address to the callee 4 Transfer control to the callee P(10, ReadInt); P(5, INC); ReadInt and INC are both procedures with one INTEGER parameter passed by reference Return Semantics (epilogue): 1 If call-by-value-result parameters are used, move the current values of those parameters to their corresponding actual parameters 2 If it is a function, move the functional value to a place the caller can get it 3 Restore the execution status of the caller 4 Transfer control back to the caller Required Storage Status information of the caller, parameters, return address, and functional value (if it is a function) Implementing Subprograms with Stack-Dynamic Local Variables The activation record format is static, but its size may be dynamic The dynamic link points to the top of an instance of the activation record of the caller An activation record instance is dynamically created when a subprogram is called Stack For a C Program void A(int x) { int y; C(y); void B(float r) { int s, t; A(s); void C(int q) { void main() { float p; B(p); -4-

Generics Provide parameterized types Source code of subprogram is reused Compiler creates copies of subprograms for each type used (static binding) Ada GENERIC, C++ template Java guarantees that all instances share same run time code Everything is Object, compiler inserts type casts Ada Generics generic type T is private; type VECTOR is array (integer range <>) of T; procedure GENERIC_SORT(A: in out VECTOR); procedure GENERIC_SORT(A: in out VECTOR) is TEMP : T; begin for INDEX_1 in A'FIRSTPRED(A'LAST) loop for INDEX_2 in SUCC(INDEX_1)A'LAST loop if A(INDEX_1) > A(INDEX_2) then TEMP := A(INDEX_1); A(INDEX_1) := A(INDEX_2); A(INDEX_2) := TEMP; end if; end loop; end loop end GENERIC_SORT; type INT_ARRAY is array (150) of INTEGER; procedure INTEGER_SORT is new GENERIC_SORT(T => INTEGER, VECTOR => INT_ARRAY); Parameters are types C++ Generics (Templates) template <class T> void sort(t list[], int len) { T temp; for (int i = 0; i < len - 1; i++) for (int j = i+1; j < len; j++) { if (list[i] > list[j]) { temp = list [i]; list[i] = list[j]; list[j] = temp; float flt_list[100]; sort(flt_list, 100); Java Generics public static <T extends Comparable<T>> void sort(t[] list) { T temp; for (int i= 0; i < listlength - 1; i++) for (int j = i+1; j < listlength; j++) { if (list[i]compareto(list[j]) > 0) { temp = list[i]; list[i] = list[j]; list[j] = temp; Integer [] a = {3, 6, 8, 2, 5; sort(a); Java Generics A common use of generics in Java (and other languages) is with Collections: ArrayList<Integer> list = new ArrayList<Integer>(); listadd(0, 42); int val = listget(0); for (Integer i : list) { Autoboxing/ unboxing of primitive types -5-