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:

CHAPTER 8

Nama : Aditya Isnugraha
Class   : LM01
NIM    : 1801419606

Assignment from Tri Djoko Wahjono
CHAPTER 8

- REVIEW QUESTION

11. What is unusual about C’s multiple-selection statement? 
Answer :
 the C switch statement has virtually no restrictions on the placement of the case expressions, which are treated as if they were normal statement labels. This laxness can result in highly complex structure eithin the switch body.



12. On what previous language was C’s switch statement based? 
Answer :
ALGOL




13. Explain how C#’s switch statement is safer than that of C. 
Answer :
 C# has a static semantics rule that disallows the implicit execution of more than one segment. Every segment must end with an explicit unconditional branch statement which transfer control out of the switch statement, or a goto, which can transfer control to one of the selectable segments


14. What are the design issues for all iterative control statements? 
Answer :
The design issues are how the iteration is controlled and where the control mechanism should appear in the loop statement.



15. What are the design issues for counter-controlled loop statements? 
Answer :
The design issues for counter-controlled loop statement are what the type and scope of the loop variable are, whether it should be legal for the loop variable or loop parameters to be changed in the loop and if so, whether the change affects loop control or not, and if the lop parameters should be evaluated only once or once for every iteration.





- PROBLEM SET

10. In Ada, the choice lists of the case statement must be exhaustive, so that there can be no unrepresented values in the control expression. In C++, unrepresented values can be caught at run time with the default selec- tor. If there is no default, an unrepresented value causes the whole statement to be skipped. What are the pros and cons of these two designs (Ada and C++)? 
Answer :
Ada was designed for military grade software development. The idea is that whenever you modify code in such a way that a new case emerges (for example adding a new value for an enumeration type), you are forced to manually revisit (and therefore re-validate) all the case statements that analyze it. Having a "default" is risky: you may forget that there is a case somewhere where the new case should not have been handled by the default. 

 
11. Explain the advantages and disadvantages of the Java for statement, compared to Ada’s for. 
 Answer :
 Java’s variable in the argument of a switch statement can be of integeral type (byte, short, int, etc), char, and String (JDK 1.7 and newer versions), but C++ can only be int or char.


12. Describe a programming situation in which the else clause in Python’s for statement would be convenient. 
 Answer :



13. Describe three specific programming situations that require a posttest loop. 
 Answer :



14. Speculate as to the reason control can be transferred into a C loop statement.

Answer :
 goto statements can be used to have one cleanup sction in routine rather than multiple return statements, or used to exit out a nested loop that is very long