Chapter 9 Programming Languages
15. What are the three semantic models of parameter passing?
-They can receive data from the corresponding actual parameter -They can transmit data to the actual parameter -They can do both. Simply: in mode, out mode, and inout mode.
1. What are the three general characteristics of subprograms?
1. Each subprogram has a single entry point. 2. 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. 3. Control always returns to the caller when the subprogram execution terminates.
3. What is given in the header of a subprogram?
1. It specifies that the following syntactic unit is a subprogram definition of some particular kind. In languages that have more than one kind of subprogram, the kind of the subprogram is usually specified with a special word. 2. If the subprogram is not anonymous, the header provides a name for the subprogram. 3. It may optionally specify a list of parameters.
5. Consider the following program written in C syntax: void swap(int a, inb) { 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
Assume the calls are not accumulative; that is, they are always called with the initialized values of the variables, so their effects are not accumulative. a. 2, 1, 3, 4, 7, 9 2, 1, 3, 4, 7, 9 2, 1, 3, 4, 7, 9 b. 1, 2, 3, 5, 7, 9 2, 3, 1, 5, 7, 9 5, 1, 3, 2, 7, 9 c. 1, 2, 3, 5, 7, 9 2, 3, 1, 5, 7, 9 5, 1, 3, 2, 7, 9
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.
19. What are two fundamental design considerations for parameter-passing methods?
First is how much the method is efficient. And second is wether its a one-way or a two-way data transfer. But obviously the collide since the beginning, cause a safe and reliable programming should be using as many one way parameters as possible and to avoid using two ways parameters as long as it can. But the most efficient way is to use pass-by-Refrence which is a two-way passing method.
8. What are formal parameters? What are actual parameters?
Formal parameters are the parameters in the subprogram header. Actual parameters are a list of parameters to be bound to the formal parameters of the subprogram which must be included with the name of the subprogram by the subprogram call statements.
10. What are the differences between a function and a procedure?
Functions return values and procedures do not.
2. What does it mean for a subprogram to be active?
It means that after having been called, a subprogram has begun execution but has not yet completed that execution.
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.
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(. . .): . . .
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.
9. What are the advantages and disadvantages of keyword parameters?
The advantage of keyword parameters 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.
13. What are the advantages and disadvantages of static local variables?
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.
12. What are the advantages and disadvantages of dynamic local variables?
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.
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
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 b. Passed by reference c. Passed by value-result
a. 1, 3 b. 2, 6 c. 2, 6
16. What are the modes, the conceptual models of transfer, the advantages, and the disadvantages of pass-by-reference parameter-passing methods?
pass-by-value: the value of the actual parameter is used to initialize the corresponding formal parameter. This formal parameter is then used as a local variable in the subprogram. Since the subprogram is receiving data from the actual parameter, this is a model of in-mode semantics. In most cases, pass-by-value is implemented using copy wher an actual value is copied then transmitted. However, it can be implemented by passing an access path to the value of the actual parameter. Copy implementation is more efficient, because when using an access path the value is stored in a write protected cell that is not always simply enforced. An advantage of pass-by-value is it's speed. Since the actual value is passed there is no need to go find it. A disadvantage is that if a copy is used then that copy must be stored, and that storage could be costly if using a large variable. pass-by-result: With pass-by-result, no value is transmitted to the subprogram. Instead, the formal parameter acts like a local variable, and before control is transferred back to the caller the variables value is transmitted back to the actual parameter. Becuase no data is transferred to the subprogram, but it transmits data back to the actual parameter it is an out-mode semantic. Most typically pass-by-result uses a copy conceptual model. Pass-by result hass 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. void Fixer(out int x, out int y) Unknown macro: { x=17; y=35; } f.Fixer(out a, out a); if x first then a = 35, else if y first then a = 17 pass-by-value-result: This passing method is actually a combination of pass-by-value and pass-by-result. The value of the actual parameter is used to intialize the corresponding formal parameter, which then acts as a local variable. The formal paramters must have local storage associated with the called subprogram. At termination, the subprogram transmits the value of the formal parameter back to the actual parameter. As such, it uses inout-mode semantics and copy passing conceptual model. Also, 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 (discussed in the pass-by-reference section). pass-by-reference: With pass-by-reference an address (reference to the memory location of the actual parameter) is passed to the subprogram. Pass-by-reference is another example of an inout-mode semantic. Also, the reference being passed is an example of an access path conceptual model. An advantage of pass-by-reference is that it is efficient in both time and space. This is no duplicate space required or copying. 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.