Chapter 9 Subprograms Fundamentals of Subprograms

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

Subprogram ธนวัฒน์ แซ่ เอียบ. Subprograms as computational abstractions.
Overview Fundamentals of Subprograms (Sec )
ISBN Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
1 CHAPTER 9 SUBPROGRAM. 2 SUBPROGRAM Topics: b Definitions of subprogram b general subprogram characteristics b parameters b Functions and procedures.
Chapter 9 Subprograms.
Chapter 9 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 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.
Chapter 9: Subprogram Control
ISBN Chapter 9 Subprograms CE2004 Principles of Programming Languages.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
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,
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
Chapter 9 Subprograms Fundamentals of Subprograms
1 Chapter 9 © 2002 by Addison Wesley Longman, Inc. 9.2 Fundamentals of Subprograms - General characteristics of subprograms: 1. A subprogram has a single.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 8 Fundamental Characteristics of Subprograms 1. A subprogram has a single entry point 2. The.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
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.
ISBN Chapter 9 Subprograms. Copyright © 2015 Pearson. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
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.
C HAPTER 9 Subprograms. CCSB314 Programming Language C HAPTER 9 T OPICS Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing.
ISBN Chapter 9 Subprograms Sections
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.
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.
Chapter 9 Subprograms. Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing.
Implementing Subprograms
Chapter 9 Subprograms.
Object Lifetime and Pointers
Chapter 9 Subprograms.
Chapter 10 : Implementing Subprograms
Data Types In Text: Chapter 6.
Implementing Subprograms
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
Subprograms Support process abstraction and modularity
Chapter 9 Subprograms.
CSC 533: Programming Languages Spring 2015
Chapter 9 Subprograms.
Chapter 9: Subprograms Sangho Ha
Subprograms In Text: Chapter 9.
Implementing Subprograms
Chapter 9 Subprograms.
Subject : T0152 – Programming Language Concept
UNIT V Run Time Environments.
CSE 452: Programming Languages
Chapter 9 Subprograms.
Chapter 9 Subprograms.
CSC 533: Programming Languages Spring 2018
Implementing Subprograms
Chapter 9 Subprograms Subprograms are the fundamental building blocks of programs and are therefore the most important concepts in programming language.
CSC 533: Programming Languages Spring 2019
Subprograms General concepts Parameter passing Functions
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Chapter 9 Subprograms Fundamentals of Subprograms Design issues for subprograms Local referencing environments Parameter-passing methods Generic subprograms Implementing subprograms with stack-dynamic local variables April 8, 2019

General Subprogram Characteristics A . subprogram has a single entry point. B . The caller is suspended during execution of the called subprogram. “Only one subprogram in execution at any given time.” C . Control always returns to the caller when the called subprogram’s execution terminates Basic Definitions A subprogram definition is a description of the actions of the subprogram abstraction. A subprogram call is an explicit request that the called subprogram be executed. A subprogram is said to be active if, after having been called, it has begun execution but has not yet completed that execution. The two fundamental types of the subprograms are: Procedures - Functions April 8, 2019

The header provides a name for the subprogram. A subprogram header is the first line of the definition, serves several definitions: It specifies that the following syntactic unit is a subprogram definition of some particular kind. The header provides a name for the subprogram. May optionally specify a list of parameters. Consider the following examples: Fortran -Subroutine Adder(parameters) Ada -procedure Adder(parameters) C- void Adder(parameters) The protocol of a subprogram is its parameter profile plus, if it is a function, its return type. April 8, 2019

A subprogram declaration provides the protocol, but not the body, of the subprogram. A formal parameter is a dummy variable listed in the subprogram header and used in the subprogram. An actual parameter represents a value or address used in the subprogram call statement. Function declarations are common in C and C++ programs, where they are called prototypes. April 8, 2019

Design Issues for Subprograms What parameter passing methods are provided? Are parameter types checked? Are local variables static or dynamic? Can subprogram definitions appear in other subprogram definitions? What is the referencing environment of a passed subprogram? Can subprograms be overloaded? Are subprograms allowed to be generic? April 8, 2019

Local Referencing Environments Vars that are defined inside subprograms are called local vars. Local vars can be either static or stack dynamic “bound to storage when the program begins execution and are unbound when execution terminates.” Advantages of using stack dynamic: a. Support for recursion. b. Storage for locals is shared among some subprograms. Disadvantages: a. Allocation/deallocation time. b. Indirect addressing “only determined during execution.” c. Subprograms cannot be history sensitive “can’t retain data values of local vars between calls.” April 8, 2019

Advantages of using static vars: a. Static local vars can be accessed faster because there is no indirection. b. No run-time overhead for allocation and dea llocation. c. Allow subprograms to be history sensitive. Disadvantages: Inability to support recursion. Their storage can’t be shared with the local vars of other inactive subprograms. In C, and C++ functions, locals are stack-dynamic unless specifically declared to be static. Ex:int adder(int list[ ], int listlen) { static int sum = 0; int count; //count is stack-dynamic for (count = 0; count < listlen; count++) sum += list[count]; return sum; } April 8, 2019

Parameter-Passing Methods (Cont.) Semantics Models of Parameter Passing Formal parameters are characterized by one of three distinct semantics models: They can receive data from the corresponding actual parameter. They can transmit data to the actual parameter; or They can do both. These models are called in mode, out mode, and inout mode, respectively. The figure on the next slide, illustrates these semantics models of parameter passing. April 8, 2019

Parameter-Passing Methods (Cont.) April 8, 2019

Parameter-Passing Methods (Cont.) Implementation Models of Parameter Passing A variety of models have been developed by language designers to guide the implementation of the three basic parameter transmission modes. In the following sections, we discuss several of these, along with their relative strengths and weaknesses. Pass-by-Value When a parameter is passed by value, the value of the actual parameter is used to initialize the corresponding formal parameter, which then acts as a local variable in the subprogram, thus implementing in-mode semantics. April 8, 2019

Parameter-Passing Methods (Cont.) Pass-by-Value (Cont.) Pass-by-value is normally implemented by copying. In this case, additional storage is required. It could be implemented by transmitting an access path to the value of the actual parameter in the caller, but that would require that the value be in a write-protected cell (one that can only be read). The advantage of pass-by-value is that for scalars it is fast, in both linkage cost and access time. The main disadvantage of the pass-by-value method if copies are used is that the storage and the copy operations can be costly if the parameter is large, such as an array with many elements. April 8, 2019

Parameter-Passing Methods (Cont.) Pass-by-Result Pass-by-result is an implementation model for out-mode parameters. When a parameter is passed by result, no value is transmitted to the subprogram. The corresponding formal parameter acts as a local variable, but just before control is transferred back to the caller, its value is transmitted back to the caller’s actual parameter, which obviously must be a variable. The pass-by-result method has the advantages and disadvantages of pass- by-value. April 8, 2019

Parameter-Passing Methods (Cont.) Pass-by-Reference Pass-by-reference is an implementation model for inout-mode parameters. The pass-by-reference method transmits an access path, usually just an address, to the called subprogram. Thus, the called subprogram is allowed to access the actual parameter in the calling program unit. In effect, the actual parameter is shared with the called subprogram. The advantage of pass-by-reference is that the passing process itself is efficient, in terms of both time and space. Duplicate space is not required, nor is any copying required. April 8, 2019

Generic Subprograms Software reuse can be an important contributor to software productivity. One way to increase the reusability of software is to reduce the need to create different subprograms that implement the same algorithm on different types of data. For example, a programmer should not need to write four different sort subprograms to sort four arrays that differ only in element type. A generic or polymorphic subprogram takes parameters of different types on different activations. Generic functions in C++ have the descriptive name of template functions. April 8, 2019

Generic Subprograms (Cont.) As an example of a template function, consider the following: template <class Type> Type max(Type first, Type second) { return first > second ? first : second; } where Type is the parameter that specifies the type of data on which the function will operate. April 8, 2019