Rabu, 17 Desember 2014

CHAPTER 9

Nama : Aditya Isnugraha
Class   : LM01
NIM    : 1801419606

Assignment from Tri Djoko Wahjono
CHAPTER 9

- REVIEW QUESTIONS
11. What are the design issues for subprograms? 
Answer :
The design issues for subprograms are:

- Are local variables statically or dynamically allocated?

- Can subprogram definitions appear in other subprogram definitions?

- What parameter-passing method or methods are used?

- Are the types of the actual parameters checked against the types of the

formal parameters?

- If subprograms can be passed as parameters and subprograms can be nested,

what is the referencing environment of a passed subprogram?

- Can subprograms be overloaded?

- Can subprograms be generic?

- If the language allows nested subprograms, are closures supported?

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

There are several advantages of stack-dynamic local variables, the primary one being the flexibility they provide to the subprogram. It is essential that recursive subprograms have stack-dynamic local variables. Another advantage of stack-dynamic locals is that the storage for local variables in an active subprogram can be shared with the local variables in all inactive subprograms.

The main disadvantages of stack-dynamic local variables are the following:

First, there is the cost of the time required to allocate, initialize (when necessary), and deallocate such variables for each call to the subprogram. Second, accesses to stack-dynamic local variables must be indirect, whereas accesses to static variables can be direct. This indirectness is required because the place in the stack where a particular local variable will reside can be determined only during execution. Finally, when all local variables are stack dynamic, subprograms cannot be history sensitive; that is, they cannot retain data values of local variables between calls.
 



13. What are the advantages and disadvantages of static local variables?
Answer :
The primary advantage of static local variables over stack-dynamic local variables is that they are slightly more efficient—they require no run-time overhead for allocation and deallocation. Also, if accessed directly, these accesses are obviously more efficient. And, of course, they allow subprograms to be history sensitive. The greatest disadvantage of static local variables is their inability to support recursion. Also, their storage cannot be shared with the local variables of other inactive subprograms.



14. What languages allow subprogram definitions to be nested? 
Answer :

For a long time, the only languages that allowed nested subprograms were those directly descending from Algol 60, which were Algol 68, Pascal, and Ada. JavaScript, Python, Ruby, and Lua are also, most functional programming languages allow subprograms to be nested.
 



15. What are the three semantic models of parameter passing?
Answer :
 Formal parameters are characterized by one of three distinct semantics models:

(1) They can receive data from the corresponding actual parameter; (2) they can transmit data to the actual parameter; or (3) they can do both. These models are called in mode, out mode, and inout mode, respectively.


- PROBLEM SET

11. C# supports out-mode parameters, but neither Java nor C++ does. Explain the difference. 
Answer :



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. 
 Answer :
 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 similari- ties and differences.
 Answer :



14. Speculate on the issue of allowing nested subprograms in programming languages—why are they not allowed in many contemporary languages? 
 Answer :
 Because it is concerned to lead to ambiguity and some error compilation.


15. What are at least two arguments against the use of pass-by-name parameters? 
Answer :
 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
About these ads
Share this:

Tidak ada komentar:

Posting Komentar