Chapter 9 Problems



CHAPTER 9 REVIEW QUESTIONS

1. What are the three general characteristics of subprograms?
-  Each subprogram has a 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 said to be active, if after having been called, it has begun execution but has not yet completed that execution.

3. What is given in the header of a subprogram?
- The definition, a name, and a specification of parameters

4. What characteristic of Python subprograms sets them apart from those of other language?
- The function def statements are executable, when def statements are executed, the functions cannot be called.

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.

6. What is a Ruby array formal parameter?
- Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. It was also influenced by Eiffel and Lisp.[8] Ruby was first designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan. Ruby supports multiple programming paradigms, including functional, object oriented and imperative. It also has a dynamic type system and automatic memory management; it is therefore similar in varying respects to Smalltalk, Python, Perl, Lisp, Dylan, and CLU.

7. What is a parameter profile? What is a subprogram protocol?
- The parameter profile of a subprogram is 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.

8. What are formal parameters? What are actual parameters?
- A formal parameter is a dummy variable listed in the subprogram header and used in the subprogram. Subprograms call statements must include the name of the subprogram and a list of parameters to be bound to the formal parameters of the subprogram. An actual parameter represents a value or address used in the subprogram call statement. Actual/Formal Parameter Correspondence: The first actual parameter is bound to the first formal parameter and so forth.  “Practical if list is short.” The name of the formal parameter is to be bound with the actual parameter. “Can appear in any order in the actual parameter list.”

SORT(LIST => A, LENGTH => N);

Advantage: order is irrelevant
Disadvantage: user must know the formal parameter’s names.

9. What are the advantages and disadvantages of keyword parameters?
- As the names would be the same as the formal parameters, which in their self-represent their types and all parameters can appear in any order, so there would be no error with types and type checking. But as we are passing them with their real names, the person who is writing the subprogram should know their real names, which would be a hard deal every now and then.

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.
- a user program building additional definitions for existing operators is fine as long as it is supervised. The programmer has a purpose for the program and it will be used one way or another so in my opinion it is fine.

5. Consider the following program written in C syntax:
void swap(int a, int b) {
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


    a. Value : 1 || List : 2,4,6,8,10
    b. Value :  6 || List : 4,1,2,8,10
    c. Value :  6 || List : 4,1,2,8,1

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. list[0] = 3 | list[1] = 5
    b. list[0] = 4 | list[1] = 6
    c. list[0] = 4 | list[1] = 6

12. Research Jensen’s Device, which was a widely known use of pass-byname parameters, and write a short description of what it is and how it can be used.
- Jensen's Device is a computer programming technique that exploits call by name. It was devised by Danish computer scientist Jørn Jensen, who worked with Peter Naur at Regnecentralen. They worked on the GIER Algol compiler, one of the earliest correct implementations of ALGOL 60.[1] ALGOL 60 used call by name. Jensen's device exploits call by name and side-effects. Call by name is an argument passing convention that delays the evaluation of an argument until it is actually used in the procedure (a consequence of the copy rule for procedures). Algol introduced call by name.

1 comments :