Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 11 Memory Management C makes it easy to shoot.
Advertisements

Programming Languages and Paradigms
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
Subprograms A subprogram allows process abstraction (as opposed to data abstraction). Characteristics –single entry point –caller suspended until control.
CPSC 388 – Compiler Design and Construction Parameter Passing.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
ISBN Chapter 10 Implementing Subprograms.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
Semantics of Calls and Returns
Overview Parameter passing modes.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
Chapter 8 :: Subroutines and Control Abstraction
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Runtime Environments Compiler Construction Chapter 7.
Chapter 7 Runtime Environments. Relationships between names and data objects As execution proceeds, the same name can denote different data objects Procedures,
Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Paradigms Imperative Programming.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
CSC 8505 Compiler Construction Runtime Environments.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
CSE 3302 Programming Languages
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
ISBN Chapter 10 Implementing Subprograms.
Dr. Philip Cannata 1 Functions and Recursion Programming Languages.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
CS314 – Section 5 Recitation 9
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Functions.
Implementing Subprograms
Principles of programming languages 4: Parameter passing, Scope rules
Implementing Subprograms
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Implementing Subprograms
CSC 533: Programming Languages Spring 2015
Implementing Subprograms
Implementing Subprograms
UNIT V Run Time Environments.
CSC 533: Programming Languages Spring 2018
Implementing Subprograms
CSC 533: Programming Languages Spring 2019
Presentation transcript:

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis

Copyright © 2006 The McGraw-Hill Companies, Inc. 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

Copyright © 2006 The McGraw-Hill Companies, Inc. 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);

Copyright © 2006 The McGraw-Hill Companies, Inc. 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); }

Copyright © 2006 The McGraw-Hill Companies, Inc. 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.

Copyright © 2006 The McGraw-Hill Companies, Inc. 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 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)

Copyright © 2006 The McGraw-Hill Companies, Inc. 9.4 Parameter Passing Mechanisms By value By reference By value-result By result By name

Copyright © 2006 The McGraw-Hill Companies, Inc. 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) { … }

Copyright © 2006 The McGraw-Hill Companies, Inc. Pass by Reference Compute the address of the argument at the time of the call and assign it to the parameter. Example Fig 9.3 Since h is passed by reference, its value changes during the call to B. 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); }

Copyright © 2006 The McGraw-Hill Companies, Inc. 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 are the same, 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.

Copyright © 2006 The McGraw-Hill Companies, Inc. 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. –Associated with lazy evaluation in functional languages (see, e.g., Haskell discussion in Chapter 14).

Copyright © 2006 The McGraw-Hill Companies, Inc. 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

Copyright © 2006 The McGraw-Hill Companies, Inc. 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

Copyright © 2006 The McGraw-Hill Companies, Inc. 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.

Copyright © 2006 The McGraw-Hill Companies, Inc. Stack Activity for the Call factorial(3) Fig. 9.7 n 3 n 2 n 1 First callSecond call Third call returns 1 Second call returns 2*1=2 First call returns 3*2=6

Copyright © 2006 The McGraw-Hill Companies, Inc. Stack Activity for Program in Fig. 9.1 Fig. 9.8 (links not shown) h undef i undef Activation of main main calls A A calls B a 3 b 2 h 5 i undef a 3 b 2 h 5 i 10 a 3 b 2 x 3 y 2 i undef j undef x 3 y 2 i undef j undef w 5 j undef k undef