Harmony Clean Flat Responsive WordPress Blog Theme

Assignment 9

Review Questions:
  1. What is a Ruby array formal parameter?
  • Ruby supports a complicated but highly flexible actual parameter configuration. The initial parameters are expressions, whose value objects are passed to the corresponding formal parameters. The initial parameters can be following by a list of key => value pairs, which are placed in an anonymous hash and a reference to that hash is passed to the next formal parameter. These are used as a substitute for keyword parameters, which Ruby does not support. The hash item can be followed by a single parameter preceded by an asterisk.
  1. What is a parameter profile? What is a subprogram protocol?
  • The parameter profile of a subprogram contains the number, order, and types of its formal parameters. The protocol of a subprogram is its parameter profile plus, if it is a function, its return type. In languages in which subprograms have types, those types are defined by the subprogram’s protocol.
  1. What are formal parameters? What are actual parameters?
  • The parameters in the subprogram header are called formal parameters. They are sometimes thought of as dummy variables because they are not variables in the usual sense: In most cases, they are bound to storage only when the subprogram is called, and that binding is often through some other program variables. While actual parameters is a parameter that bound to the formal parameters of the subprogram. Subprogram call statements must include the name of the subprogram and a list of parameters to be bound to the formal parameters of the subprogram.
  1. 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.
  1. What are the differences between a function and a procedure?
  • There are two distinct categories of subprograms—procedures and functions— both of which can be viewed as approaches to extending the language. All sub- programs are collections of statements that define parameterized computations. Functions return values and procedures do not. Procedures can produce results in the calling program unit by two methods: (1) If there are variables that are not formal parameters but are still visible in both the procedure and the calling program unit, the procedure can change them; and (2) if the procedure has formal parameters that allow the transfer of data to the caller, those parameters can be changed. However Functions are called by appearances of their names in expressions, along with the required actual parameters. And functions define new user-defined operators.
Problem Sets:
  1. Present one argument against providing both static and dynamic local variables in subprograms.
  • In subprograms local variables can be static or dynamic; If local variable treated statically: This allows for compile-time allocation/ deallocation and ensures proper type checking but does not allow for recursion. And if local variables treated dynamically: This allows for recursion at the cost of run-time allocation/ deallocation and initialization because these are stored on a stack, referencing is indirect based on stack position and possibly timeconsuming
  1. 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?
  1. Passed by value
  2. Passed by reference
  3. Passed by value-result
  • Passed by value : list[2] = { 3 , 5 }
  • Passed by reference : list[2] = { 6 , 10 }
  • Passed by value-result : list[2] = { 6 , 10 }
  1. Argue against the C design of providing only function subprograms.
  • If a language provides only functions, then either programmers must live with the restriction of returning only a single result from any subprogram, or functions must allow side effects, which is generally considered bad. Since having subprograms that can only modify a single value is too restrictive, C’s choice is not good.
  1. From a textbook on Fortran, learn the syntax and semantics of statement functions. Justify their existence in Fortran.
  • The Fortran 1966 standard provided a reference syntax and semantics, but vendors continued to provide incompatible extensions. These standards have improved portability.
  1. Study the methods of user-defined operator overloading in C++ and Ada, and write a report comparing the two using our criteria for evaluating languages.
  • One of the nice features of C++ is that you can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading. You can implement C++ operator overloads by providing special member-functions on your classes that follow a particular naming convention. For example, to overload the + operator for your class, you would provide a member-function named operator+ on your class. Meanwhile for Ada, since much of the power of the language comes from its extensibility, and since proper use of that extensibility requires that we make as little distinction as possible between predefined and user-defined types, it is natural that Ada also permits new operations to be defined, by declaring new overloading of the operator symbols.