Functions.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
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.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Semantics of Calls and Returns
Overview Parameter passing modes.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
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 Compiler Construction Chapter 7.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
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.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Basic Semantics Associating meaning with language entities.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
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.
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.
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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
Procedure Activations Programming Language. Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations.
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
Implementing Subprograms
Chapter 14 Functions.
Chapter 10 : Implementing Subprograms
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
CS 326 Programming Languages, Concepts and Implementation
Principles of programming languages 4: Parameter passing, Scope rules
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Chapter 9 :: Subroutines and Control Abstraction
Implementing Subprograms
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Implementing Subprograms
CSC 533: Programming Languages Spring 2015
Implementing Subprograms
Topic 3-a Calling Convention 1/10/2019.
Implementing Subprograms
UNIT V Run Time Environments.
Languages and Compilers (SProg og Oversættere)
Runtime Environments What is in the memory?.
CSC 533: Programming Languages Spring 2018
Implementing Subprograms
CSC 533: Programming Languages Spring 2019
Implementing Subprograms
Presentation transcript:

Functions

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

Function Call and Return 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); Function Call and Return Control returns from a called function in either of two ways void function or procedure At the end of function body Return statement Non-void function Has an expression for the value to be returned

Parameters void A(int x, int y) { -------; -------; } int main() { A(a, b); } Parameters An argument is an expression that appears in a function call. A parameter is an identifier that appears in a function declaration. Example: 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

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

Pass by Value The value of the argument is computed at the time of the call and is assigned 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.

Pass by Reference 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); 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 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. Value-result is often called copy-in-copy-out. Reference and value-result are the same, except when aliasing occurs. That is, for example when: the same variable is passed for two different parameters.

Passing by reference Vs. by Value-result Void f (int &x, int &y) { x=x+1; y=y+1; } Procedure f (x,y: in out Integer) is begin x := x+1; y := y+1; end f; Call (a.b)  increment a and b Call (a,a)  increment a by 1 (Ada)  increment a by 2 (C++)

Pass by Name Textually substitute the argument for every instance of its corresponding parameter in the function body. late binding, since evaluation of the argument is delayed until its occurrence in the function body is actually executed. Example: C/C++ mechanism for #define Implementation is dynamically for each call

Activation Record 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

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); }

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) n 3 n 2 n 1 First call Second call Third call returns 1 returns 2*1=2 returns 3*2=6