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

Similar documents
Chapter 9 Subprograms

Chapter 9. Subprograms

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

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

Programming Languages: Lecture 11

Chapter 9. Subprograms

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

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

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

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Statement Basics. Assignment Statements

IA010: Principles of Programming Languages

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

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

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

Chapter 9 Subprograms

9. Subprograms. 9.2 Fundamentals of Subprograms

Weeks 6&7: Procedures and Parameter Passing

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

G Programming Languages - Fall 2012

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

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

Programming Languages, Summary CSC419; Odelia Schwartz

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

CMSC 330: Organization of Programming Languages

CS558 Programming Languages

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

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

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

CS 315 Programming Languages Subprograms

Chapter 5 Names, Bindings, Type Checking, and Scopes

Principles of Programming Languages

Chapter 5 Names, Binding, Type Checking and Scopes

Procedure and Object- Oriented Abstraction

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Subprograms: Arguments

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Examples for Programming Language Features

Python in 10 (50) minutes

Lecture 4 Memory Management

Implementing Subprograms

C The new standard

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

low and not larger than high. 18 points

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

Pierce Ch. 3, 8, 11, 15. Type Systems

CS558 Programming Languages

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

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

Chapter 10. Implementing Subprograms ISBN

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

CS 345. Functions. Vitaly Shmatikov. slide 1

CIS192 Python Programming

Types. What is a type?

Chapter 5. Names, Bindings, and Scopes

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

Lecture 3. Variables. Variables

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

CS558 Programming Languages. Winter 2013 Lecture 4

CSC 2400: Computer Systems. Using the Stack for Function Calls

Type Bindings. Static Type Binding

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

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

Administration CS 412/413. Advanced Language Support. First-class vs. Second-class. First-class functions. Function Types

Topic 9: Type Checking

Topic 9: Type Checking

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

Programming Languages: Lecture 12

CIS192 Python Programming

CCReflect has a few interesting features that are quite desirable for DigiPen game projects:

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Chapter 10. Implementing Subprograms

Chapter 9 :: Subroutines and Control Abstraction

Lecture 2: C Programm

22c:111 Programming Language Concepts. Fall Types I

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Final-Term Papers Solved MCQS with Reference

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

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

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

CS558 Programming Languages

Programming Languages

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

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

The paramaterless ctor (aka default ctor)

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

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

Informatica 3 Syntax and Semantics

Chap. 8 :: Subroutines and Control Abstraction

CIS192: Python Programming

CSC 533: Organization of Programming Languages. Spring 2005

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

COEN244: Class & function templates

Object Model Comparisons

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

Variables and Bindings

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Transcription:

Subprogram Concept COMP3220 Principle of Programming Languages Zhitao Gong 2016 Spring 1 / 30

Outline Introduction Closure Parameter Passing Summary 2 / 30

Introduction Tow fundamental abstractions process abstraction and data abstraction. What is process abstraction? int swap(int& a, int& b) { int c = a; a = b; b = c; } Why process abstraction? Memory space efficiency, Less development and maintenance time, Increase readability, and etc. 3 / 30

General Characteristics Each subprogram has a single entry point. The caller is suspended during the execution of the callee. Control always returns to the caller when callee finishes. 4 / 30

General Characteristics Each subprogram has a single entry point. The caller is suspended during the execution of the callee. Control always returns to the caller when callee finishes. Really?? 4 / 30

General Characteristics Each subprogram has a single entry point. The caller is suspended during the execution of the callee. Control always returns to the caller when callee finishes. Really?? Not really!! Coroutine has multiple entry points. Multi-threaded functions run in parallel. Callee might abort. 4 / 30

Terminologies int fun_name(int a0, float b0, char c0); int main() { int a1; float b1; char c1; fun_name(a1, b1, c1); // why a1 corresponds to a0??? } a0, b0 and c0 are formal parameters, or parameters. a1, b1 and c1 are actual parameters, or arguments. The collection of number, order and types of formal parameters is called parameter profile. Parameter profile plus the return type is called protocol. 5 / 30

Positional Parameter Positional Parameter In nearly all PL, the correspondence between actual and formal parameters is done by position. int func(int v1, float v2, char v3) { } int main() { func(1, 2.3, c ); } This is convenient as long as parameter list is short. 6 / 30

Keyword Parameter Keyword Parameter The name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter in a call. def func(v1, v2, v3): pass func(v1 = 1, v2 = 2.3, v3 = c ) The Boost Parameter Library (BPL) supports keyword in C++. 7 / 30

Mixed Usage Some PL s support both positional and keyword parameters, e.g., Python, R, and etc. func <- function(v1, v2, v3) v1 + v2 + v3 func(1, v2 = 3, v3 = 4) The only restriction is that after a keyword parameter appears in the list, all remaining parameters must be keywords. Why? 8 / 30

Mixed Usage Some PL s support both positional and keyword parameters, e.g., Python, R, and etc. func <- function(v1, v2, v3) v1 + v2 + v3 func(1, v2 = 3, v3 = 4) The only restriction is that after a keyword parameter appears in the list, all remaining parameters must be keywords. Why? func(v3 = 4, v1 = 3, v2 = 3) func(v3 = 4, v3 = 3, v1 = 3, v2 = 3) # ERROR 8 / 30

Default Value Some PL s allow parameters to have default values, e.g., Python, C++, R and etc. def func(v1, v2, v3 = 0): print(v1 * 100 + v2 * 10 + v3) func(1, 2) func(1, 2, 3) Some allow mixing parameters with and without default values. func <- function(v1, v2 = 3, v3) v1 + v2 + v3 func(1, v3 = 4) func(v3 = 4, v1 = 3, v2 = 3) 9 / 30

Default Value Cont d Javascript, allows parameters to be undefined. function foo(v1, v2, v3) { v1 = typeof v1 === undefined? 0 : v1; v2 = typeof v2 === undefined? 0 : v2; v3 = typeof v3 === undefined? 0 : v3; console.log(v1 * 100 + v2 * 10 + v3); } foo(); foo(1); foo(1, 2); foo(1, 2, 3); 10 / 30

Variable Number of Parameters Variadic functions Functions which take a variable number of arguments, e.g., std::printf in C++. Usually the parameter is denoted by an ellipsis (...). #include <iostream> #include <cstdarg> int add_nums(int count,...) { int result = 0; va_list args; va_start(args, count); for (int i = 0; i < count; ++i) result += va_arg(args, int); va_end(args); return result; } int main() { std::cout << add_nums(4, 25, 25, 50, 50) << \n ; } 11 / 30

Packed Parameters In Python, we can pack and unpack positional and keyword arguments. def func(v1, v2): print(10 * v1 + v2) args = (1, 2) # a tuple func(*args) # -> func(1, 2) kwargs = { v1 : 1, v2 : 2} func(**kwargs) # a dict # -> func(v1=1, v2=2) 12 / 30

Design Issues 1. Are side effects allowed? 2. What types of values can be returned? 3. How many values can be returned? 13 / 30

Generic Subprograms Why? 14 / 30

Generic Subprograms Why? Programmers are lazy. Ad hoc polymorphism Overloaded subprograms need not behave similarly. The only bond is the name. Subtype polymorphism A variable of type T can access any object of type T or any type derived from T. Parametric polymorphism A subprogram that takes generic parameters that are used in type expressions that describe the types of the parameters of the subprogram. E.g., C++ template. More general polymorphism as provided by script languages. 14 / 30

Outline Introduction Closure Parameter Passing Summary 15 / 30

Definition Closure A subprogram and the referencing environment where it was defined. The referencing environment is needed if the subprogram is called from any arbitrary place in the program. Unlimited Extend Variable Lifetime is that of the whole program which usually means they must be heap-dynamic, rather than stack-dynamic. 16 / 30

Closure in C++ #include <functional> #include <cstdio> auto func(int b) { int a = 100; int bb = b; return f = [&a, b](int c) { a += 100; return a + b + c; }; } int main() { auto f = func(20); printf("%d\n", f(3)); printf("%d\n----------------\n", f(3)); auto f2 = func(40); printf("%d\n", f2(3)); printf("%d\n----------------\n", f2(3)); auto f3 = func(40); printf("%d\n", f3(3)); printf("%d\n", f3(3)); } 17 / 30

Closure in Javascript function genf (a) { var b = 30; } return function(c) { return a + b + c; }; var f = genf(100); console.log(f(1)); console.log(f(2)); E.g., Extensively used in D3. 18 / 30

Outline Introduction Closure Parameter Passing Summary 19 / 30

Semantic Models Formal parameters are characterized by one of three distinct semantics models: In Mode They can receive data from the actual parameter. Out Mode They can transmit data to the actual parameter. Inout Mode They can do both. 20 / 30

Data Transfer Either an actual value is copied back and forth or int foo(int v) { } int main() { int a = 3; foo(a); } or an access path is transmitted, e.g., pointers, references. int foo(int& v) { } int main() { int a = 3; foo(a); } 21 / 30

Pass-by-Value Implements in mode semantics. The value of the actual parameter is used to initialize the corresponding formal parameter which then acts as a local variable in the subprogram It may be implemented by copy or write-protected access path. 22 / 30

Pass-by-Result Implements out mode semantics. No value is transmitted to the subprogram The corresponding formal parameter acts as a local variable and Its value is transmitted back to the actual parameter 23 / 30

Pass-by-Result Cont d function foo(a, b) { a = 0; b = 1; } What if actual parameters are literals? foo(3, 3) What if actual parameters collide? var v = 0; foo(v, v); When to evaluate the address of the actual parameter? var v = [1, 2]; var p = 0; foo(v[p], p); 24 / 30

Pass-by-Copy AKA Pass-by-Value-Result. Implements in-out mode semantics. A combination of pass-by-value and pass-by-result. 25 / 30

Pass-by-Reference Access path is passed to formal parameters. Access to the formal parameters is slower. Aliasing problem. 26 / 30

Pass-by-Name Literally substitute formal parameters with actual parameters. Formal parameter is bound to an access method at the time of the subprogram call. Actual binding to a value or an address is delayed until the formal parameter is assigned or referenced. In C++, it is used at compile time, i.e., macro and template. #define min(x, y) ((x) < (y)? (x) : (y)) int main() { min(3, 4); min(3 + 4, 4 + 100); } 27 / 30

Copy-on-Write (COW) AKA implicit sharing, is an an optimization strategy. foo <- function(x) { ## now formal parameter and actual parameter uses the same buffer ## until x <- 3 ## now they use different buffer } It is not restricted to parameter passing. #include <string> int main() { std::string x{"hello"}; } std::string y = x; y += ", World!"; // y and x use the same buffer // now y uses a different buffer, x still uses // the same old buffer 28 / 30

Outline Introduction Closure Parameter Passing Summary 29 / 30

Summary 1. Actual and formal parameter 2. Closure 3. Parameter passing 30 / 30