Concepts of Programming Languages

(Sean Pound) #1

436 Chapter 9 Subprograms


Parameters that are multidimensioned arrays pose some issues for the lan-
guage designer, because the called subprogram needs to know how to compute
the storage mapping function for them. This requires more than just the name
of the array.
Parameters that are subprogram names provide a necessary service but
can be difficult to understand. The opacity lies in the referencing environ-
ment that is available when a subprogram that has been passed as a parameter
is executed.
C and C++ support pointers to functions. C# has delegates, which are
objects that can store references to methods. Delegates can support multicast
calls by storing more than one method reference.
Ada, C++, C#, Ruby, and Python allow both subprogram and operator
overloading. Subprograms can be overloaded as long as the various versions can
be disambiguated by the types of their parameters or returned values. Function
definitions can be used to build additional meanings for operators.
Subprograms in C++, Java 5.0, and C# 2005 can be generic, using paramet-
ric polymorphism, so the desired types of their data objects can be passed to the
compiler, which then can construct units for the requested types.
The designer of a function facility in a language must decide what restric-
tions will be placed on the returned values, as well as the number of return values.
A closure is a subprogram and its referencing environment. Closures are
useful in languages that allow nested subprograms, are static scoped, and allow
subprograms to be returned from functions and assigned to variables.
A coroutine is a special subprogram that has multiple entries. It can be used
to provide interleaved execution of subprograms.

REVIEW QUESTIONS



  1. What are the three general characteristics of subprograms?

  2. What does it mean for a subprogram to be active?

  3. What is given in the header of a subprogram?

  4. What characteristic of Python subprograms sets them apart from those
    of other languages?

  5. What languages allow a variable number of parameters?

  6. What is a Ruby array formal parameter?

  7. What is a parameter profile? What is a subprogram protocol?

  8. What are formal parameters? What are actual parameters?

  9. What are the advantages and disadvantages of keyword parameters?

  10. What are the differences between a function and a procedure?

  11. What are the design issues for subprograms?

  12. What are the advantages and disadvantages of dynamic local variables?

Free download pdf