Subprogram ธนวัฒน์ แซ่ เอียบ. Subprograms as computational abstractions.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #16 Lecture #16 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Programming Languages and Paradigms
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Overview Fundamentals of Subprograms (Sec )
1 CHAPTER 9 SUBPROGRAM. 2 SUBPROGRAM Topics: b Definitions of subprogram b general subprogram characteristics b parameters b Functions and procedures.
Procedures and Control Flow CS351 – Programming Paradigms.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Chapter 9 Subprograms.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
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.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
ISBN Chapter 9 Subprograms. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Chapter 9 Topics Introduction Fundamentals.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
ISBN Chapter 10 Implementing Subprograms.
1 Subprograms Fundamentals of subprograms Design issues for subprograms –Parameter-passing methods –Type checking of parameters –Static or dynamic storage.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
1 Chapter 9 © 2002 by Addison Wesley Longman, Inc. 9.2 Fundamentals of Subprograms - General characteristics of subprograms: 1. A subprogram has a single.
ISBN Chapter 9 Subprograms. 1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Week 7 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for.
Subprograms subroutines, procedures, functions, methods.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.
1 Subprograms In Text: Chapter 8. 2 Chapter 8: Subprograms Outline Definitions Referencing environments Parameter passing modes and mechanisms Independent.
Chapter 9 Subprograms. 2 Fundamentals of Subprograms Each subprogram has a single entry point The calling program is suspended during execution of the.
ISBN Chapter 10 Implementing Subprograms.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
ISBN Chapter 9 Subprograms Sections
CSI 3125, Subprograms, page 1 Subprograms General concepts Parameter passing Functions Subprograms as parameters.
Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
ISBN Chapter 9 Subprograms. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
Parameter Passing Mechanisms CS308 Compiler Theory.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
Implementing Subprograms
Chapter Ten: Implementing Subprograms Lesson 10. Implementing?  Previous lesson: parameter passing  In, out, inout  By value  By reference  Passing.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms ธนวัฒน์ แซ่ เอียบ. Memory states during program execution 1. At program start 2. Immediately after entering the procedure 3.
Chapter 9 Chapter 9 Subprograms. The Structure of Run-Time Memory.
ISBN Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
Implementing Subprograms
Chapter 9 Subprograms.
Chapter 9 Subprograms.
Chapter 10 : Implementing Subprograms
Functions.
Implementing Subprograms
Chapter 10: Void Functions
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
Chapter 9 Subprograms.
Subprograms In Text: Chapter 9.
Subroutines – parameter passing
Chapter 9 Subprograms.
Simulating Reference Parameters in C
Subject : T0152 – Programming Language Concept
UNIT V Run Time Environments.
Chapter 9 Subprograms Fundamentals of Subprograms
Implementing Subprograms
Subprograms General concepts Parameter passing Functions
Implementing Subprograms
Presentation transcript:

Subprogram ธนวัฒน์ แซ่ เอียบ

Subprograms as computational abstractions

Basic notions

General view of a subprogram

A subprogram´s data

Functions and procedures

Example of a function

General form of a function definition

Formal parameters of functions

Actual parameters and function calls

Calling a function in a main program

General form of a main program

Rewriting the factorial function into a procedure

General form of a procedure definition

Generalizing parameter passing

Models of Parameter Passing

Parameter Passing Methods Implementation Models: 1. Pass-by-value (in mode) –Either by physical move or access path –Disadvantages of access path method: Must write-protect in the called subprogram Accesses cost more (indirect addressing) –Disadvantages of physical move: Requires more storage (duplicated space) Cost of the moves (if the parameter is large)

Parameter Passing Methods 2. Pass-by-result (out mode) –Local’s value is passed back to the caller –Physical move is usually used –Disadvantages: If value is passed, time and space In both cases, order dependence may be a problem e.g. procedure sub1(y: int, z: int);... sub1(x, x); –Value of x in the caller depends on order of assignments at the return

Parameter Passing Methods 3. Pass-by-value-result (inout mode) –Physical move, both ways –Also called pass-by-copy –Disadvantages: Those of pass-by-result Those of pass-by-value

Parameter Passing Methods 4. Pass-by-reference (inout mode) –Pass an access path –Also called pass-by-sharing –Advantage: passing process is efficient (no copying and no duplicated storage) –Disadvantages: Slower accesses

Parameter Passing Methods Pass-by-reference - disadvantages (cont) b. Allows aliasing: i. Actual parameter collisions: e.g. procedure sub1(a: int, b: int);... sub1(x, x);

Example of Parameter Passing Consider the following C function void swap1(int a, int b){ int temp = a; a = b; b = temp; } Suppose this function is called with swap1(c,d); ให้เขียนความสัมพันธ์ระหว่างตัวแปรทั้งหมดทุก ขั้นตอนที่เกิดขึ้น

Example of Parameter Passing Consider the following C function (pass by reference) void swap2(int *a, int *b){ int temp = *a; *a = *b; *b = temp; } swap2 can be called with swap2(&c, &d); ให้เขียนความสัมพันธ์ระหว่างตัวแปรทั้งหมดทุกขั้นตอนที่เกิดขึ้น

Example of Parameter Passing Consider the following C++ function (pass by reference) void swap3(int &a, int &b){ int temp = a; a = b; b = temp; } swap3 can be called with swap3(c, d); ให้เขียนความสัมพันธ์ระหว่างตัวแปรทั้งหมดทุกขั้นตอนที่เกิดขึ้น

Memory states during program execution 1. At program start 2. Immediately after entering the procedure 3. During the execution of the body 4. Immediately before leaving the procedure 5. Immediately after leaving the procedure

A procedure with in/out parameters

Handling of in/out parameters

Memory states during program execution

1. At program start 2. Immediately after entering the procedure 3. During the execution of the body 4. Immediately before leaving the procedure 5. Immediately after leaving the procedure

Subprogram calls: control flow

Execution trace

Call stack

ที่มา Bernhard Westfechtel : RWTH Aachen University Concepts of programming languages : Sebesta, Robert W.