Saturday, June 8, 2013

Concepts of Programming Languages - Chapter 9


Review Questions

1. What are the three general characteristics of subprograms?
Each subprogram has a single entry point, excluding co-routine.
The calling program 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.

4. What are formal parameters? What are actual parameters?
The parameters in the subprogram header are called formal parameters.
Subprogram call statements must include the name of the subprogram and alist of parameters to be bound to the formal parameters of the subprogram. These parameters are called actual parameters.

5. What are the advantages and disadvantages of keyword parameters?
The advantage of keyword parameter is that they can appear in any order in the actual parameter list.
The disadvantage to keyword parameters is that the user of the subprogram must know the names of formal parameters.

6. What are the design issues for subprograms?
What parameter-passing method or methods are used?
Are the types of the actual parameters checked against the types of the formal parameters?
Are local variable statically or dynamically allocated?
Can subprogram definitions appear in other subprogram definitions?
If subprograms can be passed as parameters and subprograms can be nested, what is the referencing environment of a passed subprogram?
Can a subprogram be overloaded?
Can subprograms be generic?

7. What are the advantages and disadvantages of dynamic local variables?
Advantages:
They provide flexibility to the subprogram
The storage of local variables in an active subprogram can be shared with the local variables in all inactive subprograms.
They efficiently used when computer has small memory (Faster Access).

Disadvantages:
Cost of the time required to allocate
Access to dynamic local variable must be indirect
The stack dynamic local variables, subprograms cannot be history sensitive

9. What are the modes, the conceptual modes of transfer, the advantages, and the disadvantages or pass-by-value, pass-by-result, pass-by-value-result, and pass-by-reference parameter-passing models?
There are two conceptual models of how data transfers take place in parameter transmission. Either an actual value is copied or an access path is transmitted.

The advantage of pass-by-value is its speed.
The disadvantages of pass-by-value are, when copies are used, additional storage is required.
Storage and copy operations can be costly.

Pass-by-result has all of the advantages and disadvantages of pass-by-value, but more disadvantages. An additional disadvantage is that there can be an actual parameter collision, because order of expressions matter.
Pass-by-value-result has the same advantages and disadvantages as pass-by-value and pass-by-result with some more advantages. The largest extra advantage of pass-by-value-result is that it solves pass-by-reference’s aliasing problems.

An advantage of pass-by-reference is that it is efficient in both time and space.
A disadvantage to pass-by-reference is the increase in time to access formal parameters because of the additional level of indirect addressing. Secondly, if only one way communication to the called subprogram is required, inadvertent and erroneous changes may be made to the actual parameter. Finally, aliasing should be expected with pass-by-reference. Since pass-by-reference makes access paths available to the called subprograms, it broadens their access to nonlocal variables. These aliasing problems lead to decreased readability and reliability.

10. In what ways can aliases occur with pass-by-reference parameters?
Aliases can be occurring because pass-by-reference makes access paths available to the called subprograms.

17. What is parametric polymorphism?
Parametric polymorphism is provided by a subprogram that takes a generic parameter that is used in a type expression that describes the types of the parameters of the subprogram. Both Ada and C++ provides a kind of compile-time parametric polymorphism.

18. What causes a C++ template function to be instantiated?
C++ template functions are instantiated implicitly either when the function is named in a call or when its address is taken with the & processor.

19. In what fundamental way do the generic parameters to a Java 5.0 generic method differ from those of C++ methods?
Java does not use objects exclusively, java have no enumeration or record type. Whereas C++ Classes can be defined to have no parent, that is not possible in Java. All Java Classes must be subclass of the root class.

20. If a Java 5.0 method returns a generic type, what type of object is actually returned?
In Java any type or class can be returned by methods. Because methods are not types, they cannot be returned.

22. What are the design issues for functions?
Two design issues are functions.
Are side effects allowed?
What types of values can be returned?

23. In what ways are coroutines different from conventional subprogram?
Conventional subprograms are subordinate to their callers. When a routine calls a subprogram, execution suspends at that point in the program and resumes after the subprogram has run to completion. As a result, conventional subprogram invocation is atomic, much like a built-in statement in the programming language.


Problem Set

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.

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. Pass-by reference is specified by preceding one or both of the parameters with an ampersand.

7. Consider the following program written in C syntax:
void fun (int first, int second){

 first += first;

 second += second;

}

void main() {

 int list [2] = {3,5};

 fun(list[0], list[1]);

}

For each of the following parameter-passing methods, what are the values of the list array after execution?
a. Passes by value
b. Passes by reference.
c. Passes by value-result
a. 3, 5
b. 6, 10
c. 6, 10

11. Compare the use of closures by programming languages.
Nearly all functional programming languages, most scripting languages, and at least one primarily imperative language, C#, support closures. These languages are static-scoped, allow nested subprograms, and allow subprograms to be passed as parameters.

15. How is the problem of passing multidimensional arrays handled 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.

No comments:

Post a Comment