22c:111 Programming Language Concepts. Fall Functions

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

CS 345. Functions. Vitaly Shmatikov. slide 1

Functions and Recursion

Functions and Recursion. Dr. Philip Cannata 1

22c:111 Programming Language Concepts. Fall Types I

22c:111 Programming Language Concepts. Fall Syntax III

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

CS558 Programming Languages

Concepts Introduced in Chapter 7

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

Chapter 9 Subprograms

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

CS:3820 Programming Language Concepts

CS240: Programming in C

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

Subprograms, Subroutines, and Functions

HANDLING NONLOCAL REFERENCES

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Programs as data first-order functional language type checking

CSCI312 Principles of Programming Languages!

INF 212 ANALYSIS OF PROG. LANGS PROCEDURES & FUNCTIONS. Instructors: Kaj Dreef Copyright Instructors.

G Programming Languages - Fall 2012

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

Lecture Notes on Programming Languages

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

9. Subprograms. 9.2 Fundamentals of Subprograms

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

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

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

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Chapter 10. Implementing Subprograms ISBN

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

Chapter 9 :: Subroutines and Control Abstraction

Implementing Subprograms

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

Systems I. Machine-Level Programming V: Procedures

Names, Scopes, and Bindings II. Hwansoo Han

CS558 Programming Languages. Winter 2013 Lecture 4

Run-Time Data Structures

Principles of Programming Languages

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

CS 415 Midterm Exam Spring 2002

CS1150 Principles of Computer Science Methods

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

Programming Languages: Lecture 12

LECTURE 14. Names, Scopes, and Bindings: Scopes

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

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

Informatica 3 Syntax and Semantics

Names and Types. standard hue names. Dr. Philip Cannata 1

MIDTERM EXAM (Solutions)

References and pointers

Wednesday, October 15, 14. Functions

22c:181 / 55:181 Formal Methods in Software Engineering

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

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

PROCEDURES, METHODS AND FUNCTIONS

Chapter 5 Names, Bindings, Type Checking, and Scopes

Types and Type Inference

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

LECTURE 18. Control Flow

Programming Languages: Lecture 11

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

G Programming Languages - Fall 2012

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

22c:181 / 55:181 Formal Methods in Software Engineering

ECE232: Hardware Organization and Design

Weeks 6&7: Procedures and Parameter Passing

Chapter 5. Names, Bindings, and Scopes

Programming Languages 2nd edition Tucker and Noonan"

G Programming Languages - Fall 2012

CS1150 Principles of Computer Science Methods

Chapter 5 Names, Binding, Type Checking and Scopes

Special Topics: Programming Languages

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

Chapter 9. Subprograms

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Memory Management and Run-Time Systems

Module 27 Switch-case statements and Run-time storage management

Programming Languages

Implementing Subprograms

COMP2611: Computer Organization MIPS function and recursion

Chap. 8 :: Subroutines and Control Abstraction

Topic 3-a. Calling Convention 2/29/2008 1

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

CS 314 Principles of Programming Languages. Lecture 13

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

Types and Type Inference

CIT Week13 Lecture

Today's Topics. Last Time Modelling Scopes and Visibility The Run Stack, the Dynamic Pointer Stack, the Display (LL,ON) addressing

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Lecture 6 Introduction to Objects and Classes

Imperative Programming

CS 314 Principles of Programming Languages

Binding and Variables

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

CS 11 C track: lecture 8

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

Chapter 9. Subprograms

Transcription:

22c:111 Programming Language Concepts Fall 2008 Functions Copyright 2007-08, The McGraw-Hill Company and Cesare Tinelli. These notes were originally developed by Allen Tucker, Robert Noonan and modified by Cesare Tinelli. They are copyrighted materials and may not be used in other course settings outside of the University of Iowa in their current form or modified form without the express written permission of one of the copyright holders. During this course, students are prohibited from selling notes to or being paid for taking notes by any person or commercial firm without the express written permission of one of the copyright holders.

Contents 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters 9.4 Parameter Passing Mechanisms 9.5 Activation Records 9.6 Recursive Functions 9.7 Run Time Stack

9.1 Basic Terminology Value-returning functions: known as non-void functions/methods in C/C++/Java called from within an expression. e.g., x = (b*b - sqrt(4*a*c))/2*a Non-value-returning functions: known as procedures in Ada, subroutines in Fortran, void functions/methods in C/C++/Java called from a separate statement. e.g., strcpy(s1, s2);

9.2 Function Call and Return Example C/C++ Program Fig 9.1 int h, i; void B(int w) { int j, k; i = 2*w; w = w+1; } void A(int x, int y) { bool i, j; B(h); } int main() { int a, b; h = 5; a = 3; b = 2; A(a, b); }

9.3 Parameters Definitions An argument is an expression that appears in a function call. A parameter is an identifier that appears in a function declaration. E.g., in Figure 9.1 The call A(a, b) has arguments a and b. The function declaration A has parameters x and y.

Parameter-Argument Matching Usually by number and by position. I.e., any call to A must have two arguments, and they must match the corresponding parameters types. Exceptions: Perl - parameters aren t declared in a function header. Instead, parameters are available in an array @_, and are accessed using a subscript on this array. Ada - arguments and parameters can be linked by name. E.g., the call A(y=>b, x=>a) is the same as A(a, b)

9.4 Parameter Passing Mechanisms By value By reference By value-result By result By name

Pass by Value Compute the value of the argument at the time of the call and assign that value to the parameter. E.g., in the call A(a, b) in Fig. 9.1, a and b are passed by value. So the values of parameters x and y become 3 and 2, respectively when the call begins. So passing by value doesn t normally allow the called function to modify an argument s value. All arguments in C and Java are passed by value. But references can be passed to allow argument values to be modified. E.g., void swap(int *a, int *b) { }

Pass by Reference Compute the address of the argument at the time of the call and assign it to the parameter.

Pass by Value-Result and Result Pass by value at the time of the call and/or copy the result back to the argument at the end of the call. E.g., Ada s in out parameter can be implemented as value-result. Value-result is often called copy-in-copy-out. Reference and value-result have the same effect, except when aliasing occurs. That is, when: the same variable is both passed and globally referenced from the called function, or the same variable is passed for two different parameters.

Pass by Name Textually substitute the argument for every instance of its corresponding parameter in the function body. Originated with Algol 60 (Jensen s device), but was dropped by Algol s successors -- Pascal, Ada, Modula. Exemplifies late binding, since evaluation of the argument is delayed until its occurrence in the function body is actually executed. Similar to lazy evaluation in functional languages (see, e.g., Haskell discussion in Chapter 14).

9.5 Activation Records A block of information associated with each function call, which includes: parameters and local variables Return address Saved registers Temporary variables Return value Static link - to the function s static parent Dynamic link - to the activation record of the caller

9.6 Recursive Functions A function that can call itself, either directly or indirectly, is a recursive function. E.g., int factorial (int n) { if (n < 2) return 1; else return n*factorial(n-1); } self-call

9.7 Run Time Stack A stack of activation records. Each new call pushes an activation record, and each completing call pops the topmost one. So, the topmost record is the most recent call, and the stack has all active calls at any run-time moment. For example, consider the call factorial(3). This places one activation record onto the stack and generates a second call factorial(2). This call generates the call factorial(1), so that the stack gains three activation records.

Stack Activity for the Call factorial(3) Fig. 9.7 n 3 n 3 n 3 n 3 n 3 n 2 n 2 n 2 n 1 First call Second call Third call returns 1 Second call returns 2*1=2 First call returns 3*2=6

Stack Activity for Program in Fig. 9.1 Fig. 9.8 (links not shown) h i a 3 b 2 undef undef h 5 i undef a 3 b 2 x 3 y 2 i j undef undef h 5 i 10 a 3 b 2 x 3 y 2 i undef j undef w 5 j undef k undef Activation of main main calls A A calls B