Review Questions
1. What are the three general characteristics of subprograms?
• Each subprogram has a single entry point.
• The calling program unit is suspended during the execution of the called
subprogram, which implies that there is only one subprogram in execution
at any given time.
• Control always returns to the caller when the subprogram execution
terminates.
2. What does it mean for a subprogram to be active?
A subprogram is said to be active if, after having been called, it has begun execution but has not yet completed that execution.
3. What is given in a header of a subprogram?
A
subprogram header, which is the first part of the definition, serves
several purposes. First, it specifies that the following syntactic unit
is a subprogram definition of some particular kind.1 In languages that
have more than one kind of subprogram, the kind of the subprogram is
usually specified with a special word. Second, if the subprogram is not
anonymous, the header provides a name for the subprogram. Third, it may
optionally specify a list of parameters.
4. What characteristic of Python subprograms sets them apart from those of other languages?
One
characteristic of Python functions that sets them apart from the
functions of other common programming languages is that function def
statements are executable. When a def statement is executed, it assigns
the given name to the given function body. Until a function’s def has
been executed, the function cannot be called. Consider the following
skeletal example:
if . . .
def fun(. . .):
. . .
else
def fun(. . .):
. . .
5. What languages allow a variable number of parameters?
C# allows methods to accept a variable number of parameters, as long as they are of the same type.
6. What is a Ruby array formal parameter?
Ruby
supports a complicated but highly flexible actual parameter
configuration. The initial parameters are expressions, whose value
objects are passed to the corresponding formal parameters. The initial
parameters can be following by a list of key => value pairs, which
are placed in an anonymous hash and a reference to that hash is passed
to the next formal parameter. These are used as a substitute for keyword
parameters, which Ruby does not support. The hash item can be followed
by a single parameter preceded by an asterisk. This parameter is called
the array formal parameter.
23. What is Automatic Generalization?
The type
inferencing system of F# is not always able to determine the type of
parameters or the return type of a function. When this is the case, for
some functions, F# infers a generic type for the parameters and the
return value. This is called automatic generalization.
24. What is an overloaded subprogram?
An overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment.
25. What is ad hoc binding?
Ad hoc binding is when the environment of the call statement that passed the subprogram as an actual parameter.
26. What is multicast delegate?
All of the
methods stored in a delegate instance are called in the order in which
they were placed in the instance. This is called a multicast delegate.
30. What are the design issues for functions?
The following design issues are specific to functions:
• Are side effects allowed?
• What types of values can be returned?
• How many values can be returned?
31. What two languages allow multiple values to be returned from a function?
Ruby allows the return of more than one value from a method. Lua also allows functions to return multiple values.
Problem Set
3. Argue in support of the template functions of C++. How is it different from the template functions in other languages?
C++
templated classes are instantiated to become typed classes at compile
time. For example, an instance of the templated Stack class, as well as
an instance of the typed class, can be created with the following
declaration:
Stack<int> myIntStack;
However, if an instance
of the templated Stack class has already been created for the int type,
the typed class need not be created.
6 . Compare and contrast PHP’s parameter passing with that of C#.
PHP’s
parameter passing is similar to that of C#, except that either the
actual parameter or the formal parameter can specify pass-by-reference.
Passby- reference is specified by preceding one or both of the
parameters with an ampersand.
8 . Argue against the Java design of not providing operator overloading
Arithmetic
operators are often used for more than one purpose. For example, +
usually is used to specify integer addition and floating-point addition.
Some languages—Java, for example—also use it for string catenation.
This multiple use of an operator is called operator overloading and is
generally thought to be acceptable, as long as neither readability nor
reliability suffers.
12 . Research Jensen’s Device, which was a widely known use
of pass-by-name parameters, and write a short description of what it is
and how it can be used.
Implementing a pass-by-name parameter requires a subprogram to be
passed to the called subprogram to evaluate the address or value of the
formal parameter. The referencing environment of the passed subprogram
must also be passed. This subprogram/referencing environment is a
closure. Pass-by-name parameters are both complex to implement and
inefficient. They also add significant complexity to the program,
thereby lowering its readability and reliability. Because pass-by-name
is not part of any widely used language, it is not discussed further
here. However, it is used at compile time by the macros in assembly
languages and for the generic parameters of the generic subprograms in
C++, Java 5.0, and C# 2005.
15. How is the problem of passing multidimensional arrays handles by Ada?
Ada
compilers are able to determine the defined size of the dimensions of
all arrays that are used as parameters at the time subprograms are
compiled. In Ada, unconstrained array types can be formal parameters. An
unconstrained array type is one in which the index ranges are not given
in the array type definition. Definitions of variables of unconstrained
array types must include index ranges. The code in a subprogram that is
passed an unconstrained array can obtain the index range information of
the actual parameter associated with such parameters
Tidak ada komentar:
Posting Komentar