Document Details

AccomplishedUniverse8955

Uploaded by AccomplishedUniverse8955

Tags

subprograms programming computer science programming languages

Summary

This document covers the topic of subprograms in programming. It details the fundamentals of subprograms, different types of subprograms, parameters, and design issues.

Full Transcript

CHAPTER 9 SUBPROGRAMS Chapter 9 Topic Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing Methods Parameters That Are Subprogram Names Overloaded Subprograms Generic Subprograms Design Issues for...

CHAPTER 9 SUBPROGRAMS Chapter 9 Topic Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing Methods Parameters That Are Subprogram Names Overloaded Subprograms Generic Subprograms Design Issues for Functions User-Defined Overloaded Operators Coroutines Introduction Subprograms are the fundamental building blocks of programs and are therefore among the most import concepts in programming language design. The reuse results in several different kinds of savings, including memory space and coding time. Fundamentals of Subprograms General Subprogram Characteristics a) A subprogram has a single entry point. b) The caller is suspended during execution of the called subprogram, which implies that there is 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 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 header examples: – Fortran. Subroutine Adder(parameters) Parameters Subprograms typically describe computations. There are two ways that a non-local method program can gain access to the data that it is to process: 1. Through direct access to non-local variables. – The only way the computation can proceed on different data is to assign new values to those non-local variables between calls to the subprograms. – Extensive access to non-locals can reduce reliability. 2. Through parameter passing “more flexible”. – Data passed through parameters are accessed through names that are local to the subprogram. – A subprogram with parameter access to the data it is to process is a parameterized computation. – It can perform its computation on whatever data it receives through its parameters. A formal parameter is a dummy variable listed in the subprogram header and used in the subprogram. Procedures and Functions There are two distinct categories of subprograms, procedures and functions. Procedures: provide user-defined parameterized computation statements. The computations are enacted by single call statements. Procedures can produce results in the calling program unit by two methods: – If there are variables that are not formal parameters but are still visible in both the procedure and the calling program unit, the procedure can change them. – If the subprogram has formal parameters that allow the transfer of data to the caller, those parameters can be changed. Functions: provide user-defined operators which are semantically modeled on mathematical functions. – If a function is a faithful model, it produces no side effects. – It modifies neither its parameters nor any variables defined outside the function. Design Issues for Subprograms 1. What parameter passing methods are provided? 2. Are parameter types checked? 3. Are local variables static or dynamic? 4. Can subprogram definitions appear in other subprogram definitions? 5. What is the referencing environment of a passed subprogram? 6. Can subprograms be overloaded? 7. Are subprograms allowed to be generic? 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: – Support for recursion. – Storage for locals is shared among some subprograms. Disadvantages: – Allocation/deallocation time. – Indirect addressing “only determined during execution.” – Subprograms cannot be history sensitive “can’t retain data values of local vars between calls.” Advantages of using static vars: – Static local vars can be accessed faster because there is no indirection. – No run-time overhead for allocation and deallocation. – Allow subprograms to be history sensitive. Parameter Passing Methods Semantic Models of Parameter Passing Formal parameters are characterized by one of three distinct semantic models: – in mode: They can receive data from corresponding actual parameters. – out mode: They can transmit data to the actual parameter. – Inout mode: They can do both. There are two conceptual models of how data transfers take places in parameter transmission: – Either an actual value is copied (to the caller, to the callee, or both ways), or – An access path is transmitted.

Use Quizgecko on...
Browser
Browser