Thursday 27 June 2013

Chapter 9



Concept of Programming Languages by Robert W. Sebesta Answer

Review Questions

1. What are the three characteristics of subprogram?


  • Each subprogram has single entry point 
  • 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. 
  • Control always returns to the caller when the subprogram execution terminates


2. What does it mean for a subprogram to be active?
A subprogram is to be called active if, after having bee called, it has begun execution but it hasn’t yet completed that execution.

7. What is a parameter profile? What is a subprogram protocol?
The parameter profile of a subprogram is the container of the number, order, and types of its formal parameters. While a subprogram protocol is subprogram’s parameter profile as well as its return type if it’s a function.

8. What are formal parameters? What are actual parameters?
Formal parameters are parameters in the subprogram header, sometimes thought as dummy variables because they are not variables in the usual sense, mostly because they are bound to storage only when the subprogram is called while binding is often through some other program variables. Actual parameters, on the other hand, are a list of parameters to be bound to the formal parameters of the subprogram upon statement call by the subprogram.

9. What are the advantages and disadvantages of keyword parameters?
Keyword parameters, by default is a solution to simplify long lists, in which the name of the formal parameter to which actual parameter is to be bound and specified with the actual parameter in a call. The advantage is, they can appear in any order in the actual parameter list. The disadvantage is, the user of the subprograms must know the names of the formal parameters.

15. What are the three semantic models of parameter passing?
In mode: formal parameters can receive data from te corresponding actual parameter 
Out mode: formal parameters can transmit data to the actual parameter 
In-out mode: do both of In mode and Out mode.

20. What is the parameter-passing method of Python and Ruby called?
The parameter-passing method of Python and Ruby is called passing-by-assignment. Because all data values are objects, every variable is a reference to an object. So, the actual parameter value is assigned to the formal parameter. Quite similar to pass-by reference.

21. If a variable’s lifetime is that of a whole program, what is it known as?

It is known as having unlimited extent, that is, the variables must be heap dynamic rather than stack dynamic.

23. What is automatic generalization?
Automatic generalization is the case in F# when for some functions F# infers a generic type for the parameters and the return value. This happens when the type inferencing system in F# cannot determine the type of parameters or the return tye of the function.

24. What is an overloaded subprogram?
An overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment.

34. What is a closure?
A closure is a subprogram and the referencing environment where it was defined.


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. 
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.

3. Argue in support of the templated functions in C++. How is it different from the templated functions of other languages?

C++ differentiates function based on overloading, and it’s not practical to make multiple overloading functions regarding writeability and readability. Creating template, like those on generic programming, allows function to receive any data type as long as the variation is based on the formal parameter definition.


6. Compare and contrast PHP’s parameter passing with that of C#.
PHP’s parameter passing is similar to 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 parameters with an ampersand.


11. Compare the use of closures by programming languages.

Almost every functional programming languages, most of the scripting languages, and at least one primarily imperative language, C#, support closures. These languages are static-scoped, and they allow nested subprograms and passing subprograms 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 dimension of all arrays that are used as parameters at the time of subprograms are compiled.

0 comments:

Post a Comment