Functions and Recursion

Similar documents
Functions and Recursion. Dr. Philip Cannata 1

Reminder About Functions

22c:111 Programming Language Concepts. Fall Functions

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

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

CS 345. Functions. Vitaly Shmatikov. slide 1

Programming Languages. Dr. Philip Cannata 1

Programming Languages. Dr. Philip Cannata 1

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

CS4215 Programming Language Implementation

fjyswan Dr. Philip Cannata 1

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

CSCI312 Principles of Programming Languages!

Implementing Subprograms

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

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

An Introduction to Functions

Programming Languages: Lecture 12

CS558 Programming Languages

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

CMSC 330: Organization of Programming Languages. Operational Semantics

1 Lexical Considerations

PROCEDURES, METHODS AND FUNCTIONS

Chapter 10. Implementing Subprograms ISBN

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

The role of semantic analysis in a compiler

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

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

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

CSE 307: Principles of Programming Languages

CS 415 Midterm Exam Spring 2002

LECTURE 18. Control Flow

Concepts Introduced in Chapter 7

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Informatica 3 Syntax and Semantics

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Comp 311: Sample Midterm Examination

High Level Languages. Java (Object Oriented) This Course. Jython in Java. Relation. ASP RDF (Horn Clause Deduction, Semantic Web) Dr.

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

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Lexical Considerations

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

Higher-Order Functions

Weeks 6&7: Procedures and Parameter Passing

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

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

NOTE: Answer ANY FOUR of the following 6 sections:

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

COP4020 Programming Assignment 1 - Spring 2011

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Programming Languages. Dr. Philip Cannata 1

CS 161 Intro to CS I. Finish Pointers/Start Recursion

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Compiler Theory. (Semantic Analysis and Run-Time Environments)

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

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

CS1150 Principles of Computer Science Methods

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

Types and Type Inference

Higher-Order Functions (Part I)

Implementing Subprograms

Programmiersprachen (Programming Languages)

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

Compiler construction

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

Today's Topics. CISC 458 Winter J.R. Cordy

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

Lexical Considerations

Chapter 10. Implementing Subprograms

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

Cost of Substitution

CS 242. Fundamentals. Reading: See last slide

Principles of Programming Languages

CMSC 330: Organization of Programming Languages

EK131 E5 Introduction to Engineering

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

Functional Programming. Big Picture. Design of Programming Languages

Parsing Scheme (+ (* 2 3) 1) * 1

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

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

CS 6353 Compiler Construction Project Assignments

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Scope, Functions, and Storage Management

4 Bindings and Scope. Bindings and environments. Scope, block structure, and visibility. Declarations. Blocks. 2004, D.A. Watt, University of Glasgow

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

CSE P 501 Exam 8/5/04

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Introduction to Functional Programming in Haskell 1 / 56

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

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

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

EC 413 Computer Organization

Functional Programming. Pure Functional Programming

CS558 Programming Languages. Winter 2013 Lecture 4

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

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

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

CS1150 Principles of Computer Science Methods

Transcription:

Programming Languages Functions and Recursion Dr. Philip Cannata 1

10 High Level Languages This Course Jython in Java Java (Object Oriented) ACL2 (Propositional Induction) Relation Algorithmic Information Theory (Information Compression and Randomness) - Kolmogorov Complexity Orc (Parallel Computing) GpH (Parallel Computing) RDF (Horn Clause Deduction, Semantic Web) Dr. Philip Cannata 2

Lisp Function Definition (Declaration) and Application (Invocation) Using Scheme8 on the class calendar webpage, evaluate the following: (interp (parse (quote (with ((z 7)) (with ((z 3) (a 5) (x (fun (x y) (- x (+ y z))))) (with ((z 10) (a 5)) (x z a))))) ) (mtenv)) The (parse (quote (with ((z 7))... evaluates to the following: (with (list (binding 'z (num 7))) (with (list (binding 'z (num 3)) (binding 'a (num 5)) (binding 'x (fun (list 'x 'y) (binop - (id 'x) (binop + (id 'y) (id 'z)))))) (with (list (binding 'z (num 10)) (binding 'a (num 5))) (app (id 'x) (list (id 'z) (id 'a)))))) -------------------------------------------------------------------------------------------------------------------------------------------------- Where are the assignments? Where are the global variables? Are there any side affects? Dr. Philip Cannata 3

10 High Level Languages This Course Jython in Java Java (Object Oriented) ACL2 (Propositional Induction) Relation Algorithmic Information Theory (Information Compression and Randomness) - Kolmogorov Complexity Orc (Parallel Computing) GpH (Parallel Computing) RDF (Horn Clause Deduction, Semantic Web) Dr. Philip Cannata 4

Parameter and Arguments Definitions An argument is an expression that appears in a function call. A parameter is an identifier that appears in a function declaration. In the code on the right the call A(a, b) has arguments a and b. The function declaration A has parameters x and y. int h, i; void B(int w) { int j = 1, k = 2; i = 2*w; w = w+1; printf("in B - w, j, k, h, i: %d, %d, %d, %d, %d\n", w, j, k, h, i); void A(int x, int y) { float i = 1.1, j = 2.2; B(h); printf("in A - x, y, i, j, h: %d, %d, %f, %f, %d\n", x, y, i, j, h); int main() { int a, b; h = 5; a = 3; b = 2; A(a, b); printf("in Main a, b, h, i: %d, %d, %d, %d\n", a, b, h, i); Dr. Philip Cannata 5

Parameter Passing Mechanisms By value By reference By value-result By name Dr. Philip Cannata 6

Compute the value of the argument at the time of the call and assign that value to the parameter. 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 Value int h, i; void B(int w) { int j = 1, k = 2; i = 2*w; w = w+1; printf("in B - w, j, k, h, i: %d, %d, %d, %d, %d\n", w, j, k, h, i); void A(int x, int y) { float i = 1.1, j = 2.2; B(h); printf("in A - x, y, i, j, h: %d, %d, %f, %f, %d\n", x, y, i, j, h); int main() { int a, b; h = 5; a = 3; b = 2; A(a, b); printf("in Main a, b, h, i: %d, %d, %d, %d\n", a, b, h, i); $./a In B - w, j, k, h, i: 6, 1, 2, 5, 10 In A - x, y, i, j, h: 3, 2, 1.100000, 2.200000, 5 In Main a, b, h, i: 3, 2, 5, 10 Dr. Philip Cannata 7

Compute the address of the argument at the time of the call and assign it to the parameter. Since h is passed by reference, its value changes during the call to B. Pass by Reference int h, i; void B(int *w) { int j = 1, k = 2; i = 2*(*w); *w = *w + 1; printf("in B - w, j, k, h, i: %d, %d, %d, %d, %d\n", w, j, k, h, i); void A(int *x, int *y) { float i = 1.1, j = 2.2; B(&h); printf("in A - x, y, i, j, h: %d, %d, %f, %f, %d\n", x, y, i, j, h); int main() { int a, b; h = 5; a = 3; b = 2; A(&a, &b ); printf("in Main a, b, h, i: %d, %d, %d, %d\n", a, b, h, i); $./a In B - w, j, k, h, i: 4206640, 1, 2, 6, 10 In A - x, y, i, j, h: 2280676, 2280672, 1.100000, 2.200000, 6 In Main a, b, h, i: 3, 2, 6, 10 Dr. Philip Cannata 8

int h, i; void B(int w) { int j = 1, k = 2; i = 2*w; w = w+1; Pass by Value printf("in B - w, j, k, h, i: %d, %d, %d, %d, %d\n", w, j, k, h, i); void A(int x, int y) { float i = 1.1, j = 2.2; B(h); printf("in A - x, y, i, j, h: %d, %d, %f, %f, %d\n", x, y, i, j, h); int main() { int a, b; h = 5; a = 3; b = 2; A(a, b); printf("in Main a, b, h, i: %d, %d, %d, %d\n", a, b, h, i); int h, i; void B(int *w) { int j = 1, k = 2; i = 2*(*w); *w = *w + 1; Pass by Reference printf("in B - w, j, k, h, i: %d, %d, %d, %d, %d\n", w, j, k, h, i); void A(int *x, int *y) { float i = 1.1, j = 2.2; B(&h); printf("in A - x, y, i, j, h: %d, %d, %f, %f, %d\n", x, y, i, j, h); int main() { int a, b; h = 5; a = 3; b = 2; A(&a, &b ); printf("in Main a, b, h, i: %d, %d, %d, %d\n", a, b, h, i); $./a In B - w, j, k, h, i: 6, 1, 2, 5, 10 In A - x, y, i, j, h: 3, 2, 1.100000, 2.200000, 5 In Main a, b, h, i: 3, 2, 5, 10 $./a In B - w, j, k, h, i: 4206640, 1, 2, 6, 10 In A - x, y, i, j, h: 2280676, 2280672, 1.100000, 2.200000, 6 In Main a, b, h, i: 3, 2, 6, 10 Dr. Philip Cannata 9

Pass by Value-Results Pass by value at the time of the call and/or copy the result back to the argument at the end of the call also called copy-in-copyout. Dr. Philip Cannata 10

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. real procedure Sum(j, lo, hi, Ej); value lo, hi; integer j, lo, hi; real Ej; begin real S; S := 0; for j := lo step 1 until hi do S := S + Ej; Sum := S end; x[j]*j Exemplifies late binding, since evaluation of the argument is delayed until its occurrence in the function body is actually executed. Associated with lazy evaluation in functional languages (e.g., Haskell). Dr. Philip Cannata 11

Recursive Functions A function that can call itself, either directly or indirectly, is a recursive function. $ cat test.c int factorial (int n) { int i; if (n < 2) { printf("factorial returning 1\n"); return 1; else { i = n * factorial(n-1); printf("factorial returning %d\n", i); return i; int main() { printf("factorial(3) returns: %d\n", factorial(3)); $./a factorial returning 1 factorial returning 2 factorial returning 6 factorial(3) returns: 6 Dr. Philip Cannata 12

Runtime 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. Dr. Philip Cannata 13

Activation Records A block of information associated with each function call, which includes: parameters and local variables Return address We call this constructed value a closure because it closes the function body over the substitutions that are waiting to occur. When the interpreter encounters a function application, it must ensure that the function s pending substitutions aren t forgotten. It must, however, ignore the substitutions pending at the location of the invocation, for that is precisely what led us to dynamic instead of static scope. It must instead use the substitutions of the invocation location to convert the function and argument into values, hope that the function expression evaluated to a closure, then proceed with evaluating the body employing the repository of deferred substitutions stored in the closure. [textbook. Pages 46-47] Dr. Philip Cannata 14

Runtime Stack for Functions Program 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); parameters and local variables Return address Saved registers Temporary variables Return value Dr. Philip Cannata 15

Calling function: factorial BasePtr = 3, printing runtime stack n: 3 answer: null number: 3 Calling function: factorial BasePtr = 5, printing runtime stack n: 2 n: 3 answer: null number: 3 Calling function: factorial BasePtr = 7, printing runtime stack n: 1 n: 2 n: 3 answer: null number: 3 Calling function: factorial BasePtr = 9, printing runtime stack n: 0 n: 1 n: 2 n: 3 answer: null number: 3 Hmm Runtime Stack for Factorial 3 int factorial(int n) { if(n < 1) { return 1; else { return n * factorial(n - 1); int main() { int number, answer; number = 3; answer = factorial(number); print(answer); Exiting function: factorial BasePtr = 9, printing runtime stack n: 0 return#factorial: 1 n: 1 n: 2 n: 3 answer: null number: 3 Exiting function: factorial BasePtr = 7, printing runtime stack return#factorial: 1 n: 1 return#factorial: 1 n: 2 n: 3 answer: null number: 3 Exiting function: factorial BasePtr = 5, printing runtime stack return#factorial: 1 n: 2 return#factorial: 2 n: 3 answer: null number: 3 Exiting function: factorial BasePtr = 3, printing runtime stack return#factorial: 2 n: 3 return#factorial: 6 answer: null number: 3 Dr. Philip Cannata 16

A Taxonomy of Functions 6.1 A Taxonomy of Functions first-order Functions are not values in the language. They can only be defined in a designated portion of the program, where they must be given names for use in the remainder of the program. The functions in F1WAE are of this nature, which explains the 1 in the name of the language. higher-order Functions can [be defined anywhere in a program and] return other functions as values. first-class Functions are values with all the rights of other values. In particular, they can be supplied as the value of arguments to functions, returned by functions as answers, and stored in data structures. [They can also be defined anywhere in a program.] Dr. Philip Cannata 17