You are on page 1of 4

CHAPTER 9

Problem Set
1. What are arguments for and against a user program building additional definitions for
existing operators, as can be done in Python and C++? Do you think such user-defined
operator overloading is good or bad? Support your answer.
Arguments for:
It allows the developer to program using notation “closer to the target domain” and allows
user-defined types a similar level of syntactic support as types built into the language. It can
easily be emulated using function calls.
Arguments against:
It can be implemented according to user’s want, eventhough it is not logically true.
It is good, as long as the user knows what he or she is doing. C++ default operators are only
working for default data types. As users can make their own datatypes, custom operators are
also needed. The ultimate goal by using operator overloading is to reduce both the learning
curve and the defect rate.
2. In most Fortran IV implementations, parameters were passed by reference, using access
path transmission only. State both the advantages and disadvantages of this design choice.
The main advantage of this method is the fast accesses to formal parameters in subprograms.
The disadvantages are that recursion is rarely useful when values cannot be passed, and also
that a number of problems, such as aliasing, occur with the method.
3. Argue in support of the Ada 83 designers’ decision to allow the implementor to choose
between implementing inout-mode parameters by copy or by reference.
The tradeoff is one of efficiency of passing parameters vs. efficiency of accessing the
parameters in the subprogram. Ada leaves this choice to the implementor, rather than forcing a
single solution, because different situations may require different approaches. Passing a large
array which the subprogram only accesses a few times is faster with reference. Passing any
argument that the subprogram accesses a lot is faster with copy.
4. Suppose you want to write a method that prints a heading on a new output page, along
with a page number that is 1 in the first activation and that increases by 1 with each
subsequent activation. Can this be done without parameters and without reference to
nonlocal variables in Java? Can it be done in C#?
This can be done in both Java and C#, using a static (or class) data member for the page number
5. Consider the following program written in C syntax:
void swap(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
}
void main() {
int value = 2, list[5] = {1, 3, 5, 7, 9};
swap(value, list[0]);
swap(list[0], list[1]);
swap(value, list[value]);
}
For each of the following parameter-passing methods, what are all of the values of the
variables value and list after each of the three calls to swap?
a. Passed by value b. Passed by reference c. Passed by value-result
2, 1, 3, 5, 7, 9 1, 2, 3, 5, 7, 9 1, 2, 3, 5, 7, 9
2, 1, 3, 5, 7, 9 2, 3, 1, 5, 7, 9 2, 3, 1, 5, 7, 9
2, 1, 3, 5, 7, 9 5, 1, 3, 2, 7, 9 5, 1, 3, 2, 7, 9

6. Present one argument against providing both static and dynamic local variables in
subprograms.
In subprograms local variables can be static or dynamic; If local variable treated statically: This
allows for compile-time allocation/ deallocation and ensures proper type checking but does not
allow for recursion. And if local variables treated dynamically: This allows for recursion at the
cost of run-time allocation/ deallocation and initialization because these are stored on a stack,
referencing is indirect based on stack position and possibly time consuming
7. Consider the following program written in C syntax:
void fun (int first, int second) {
first += first;
second += second;
}
void main() {
int list[2] = {1, 3};
fun(list[0], list[1]);
}
For each of the following parameter-passing methods, what are the values of the list array
after execution?
a. Passed by value 1,3
b. Passed by reference 2,6
c. Passed by value-result 2,6
8. Argue against the C design of providing only function subprograms.
If a language provides only functions, then either programmers must live with the restriction of
returning only a single result from any subprogram, or functions must allow side effects, which
is generally considered bad. Since having subprograms that can only modify a single value is too
restrictive, C’s choice is not good.
9. From a textbook on Fortran, learn the syntax and semantics of statement functions. Justify
their existence in Fortran.
The Fortran 1966 standard provided a reference syntax and semantics, but vendors continued
to provide incompatible extensions. These standards have improved portability.
10. Study the methods of user-defined operator overloading in C++ and Ada, and write a
report comparing the two using our criteria for evaluating languages.
One of the nice features of C++ is that you can give special meanings to operators, when they
are used with user-defined classes. This is called operator overloading. You can implement C++
operator overloads by providing special member-functions on your classes that follow a
particular naming convention. For example, to overload the + operator for your class, you
would provide a member-function named operator+ on your class. Meanwhile for Ada, since
much of the power of the language comes from its extensibility, and since proper use of that
extensibility requires that we make as little distinction as possible between predefined and
user-defined types, it is natural that Ada also permits new operations to be defined, by
declaring new overloading of the operator symbols.
11. C# supports out-mode parameters, but neither Java nor C++ does. Explain the difference.
Only its designers can answer this question definitely. The advantage of including an out mode
for parameter passing is clear: If a parameter is used only to return a value from a subprogram,
it is sensible to restrict its use to that. Such a parameter should not be allowed to have an initial
value and it must be assigned a value before the subprogram terminates. These restrictions can
only be enforced implicitly if a separate mode is included for such parameters. Given the other
insecurities of C++, it is not surprising that it does not include an out mode. Java may not
include out mode parameters because, at least initially, it was meant to be a simple language.
12. Research Jensen’s Device, which was a widely known use of pass-byname 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.
13. Study the iterator mechanisms of Ruby and CLU and list their similarities and differences.
Ruby methods differ from the subprograms of other programming languages in several
interesting ways. Ruby methods are often defined in class definitions but can also be defined
outside class definitions, in which case they are considered methods of the root object, Object.
Such methods can be called without an object receiver, as if they were functions in C or C++. If a
Ruby method is called without a receiver, self is assumed. If there is no method by that name in
the class, enclosing classes are searched, up to Object, if necessary All Lua functions are
anonymous, although they can be defined using syntax that makes it appear as though they
have names.
14. Speculate on the issue of allowing nested subprograms in programming languages—why
are they not allowed in many contemporary languages?
Many contemporary languages do not allow nested subprograms because many designers now
believe that there are better ways to organize programs. Also, they think the additional
complexity of nested subprograms outweighs their value. Finally, there is the problem of the
nested structure of programs deteriorating through continued maintenance, leading to largely
unstructured programs in the end, regardless of their initial structure.
15. What are at least two arguments against the use of pass-by-name parameters?
Expensive to implement.
Repeated calls to the subroutine representing any expression in an actual parameter, rather
than saving results from calls.
Late binding makes type checking difficult.
16. Write a detailed comparison of the generic subprograms of Java 5.0 and C# 2005.

You might also like