类别 全部 - functions - polymorphism - parameters

作者:Donaldo Cruz 3 年以前

336

Chapter 9: Subprograms

The chapter delves into the intricacies of subprograms, emphasizing both their fundamental and advanced aspects. It begins with a discussion on the basic characteristics of subprograms, such as their single entry point, suspension of the calling program until completion, and the return to the calling program once execution ends.

Chapter 9: Subprograms

Chapter 9: Subprograms

9.8 Design Issues for Functions

Number of Returned values
Multiple Returns

Tuples

F#

ML

1 Return

Most languages

Types of Returned Values
Subprograms as objects
Any type

Ruby

Python

Ada

No arrays/functions

C

Functional Side Effects
Pass-by-value/pass-by-reference

Functions can possibly cause side effects and aliasing

In-mode only

No side effects from parameters or aliasing of parameters/globals

9.1 Introduction

Two fundamental abstractions
Data abstraction
Process abstraction

Modern versions are written as subprograms

Not the same as methods.

First programmable computer used via reusable cards

9.2 Fundamentals of Subprograms

Procedures and Functions
Functions

Mathematic equation

Procedures

Can change outside parameters

Parameters
Keyword Parameter

Formal Parameter attached to Actual Parameter in Subprogram call

Positional Parameters

Actual parameters which take the values of their corresponding formal parameter in order

Actual Parameters

Parameters initialized within subprogram

Formal parameters

Ones in function call, exist only while the subprogram is called

Definitions
Subprogram Header

Specifies return value

Specifies subprogram variety

Names parameters

Chooses call name

Subprogram call

Considered active during call

Request subprogram execution

Subprogram Definitions

Describes interface

Characteristics
Returns to calling program at end
Calling program suspended until end
Single Entry Point

9.3 Design Issues for Subprograms

Which type of Subprogram
Closure

Nested Subprogram

Generic Subprograms

Subprogram which can be used with any input type

Overloaded Subprograms

Same name for multiple subprograms

9.10 Generic Subprograms

Generic in F#
Automatic generalization

If no type info included, assume generic

Generic in C#
actual type can be omitted if compiler can infer
No support for Wildcard type
Generic in Java 5.0
Collection

is a wildcard type

Uses
Generic in C++
Template function

Typename Identifier

Class Identifier

Polymorphic
Parametric polymorphism

Able to use different parameters per call instance

Subtype polymorphism

The type of an object can be derived from T

Can take parameters of different types on seperate calls

9.9 Overloaded Subprograms

Can be found in
C#
Java
C++
Subprogram has same name as another but has a least one difference in:
Return type
Types of parameters
Order of parameters
Number of parameters

9.6 Parameters That Are Subprograms

Complications
Which referencing environment should be used

Ad Hoc Binding

The environment of the call statement that passed the subprogram as an actual parameter

Deep Binding

The environment of the definition of the passed subprogram

Shallow Binding

The environment of the call statement that enacts the passed subprogram

Type checking the parameters of the activations of the subprograms that were passed as parameters

9.5 Parameter-Passing Methods

Design Considerations
Efficiency
Type Checking Parameters
Types of actual parameters checked for consistency w/ the types of corresponding formal parameters
Models
Pass-by-Name

The actual parameter is textually substituted for the corresponding formal parameter in all its occurrences in the subprogram

Pass-by-Reference

transmits an access path (an address) to the called subprogram

Pass-by-Value-Result

Combination between pass-by-value and pass-by-result

(For inout-mode parameters) Actual values are copied

Pass-by-Result

(For out-mode parameters) No value is transmitted to the subprogram

Pass-by-Value

Value of actual parameter is used to initialize the corresponding former parameter

Semantics
Inout mode

Receive and transmit data from and to the parameter

Out mode

Transmit data to actual parameter

In mode

Receive data from the corresponding actual parameter

9.4 Local Referencing Environments

Nested Subprograms
created to be able to create a hierarchy of both logic and scopes
Local Variables
Stack Dynamic

Bound to storage at start of execution(of subprogram) and unbound when execution terminates

Static

Require no run-time overhead for allocation and deallocation