Rabu, 26 Juni 2013

Chap 9 - Review Question and Problem Set - Concepts of Programming Languages, Sebesta

CONCEPTS OF
PROGRAMMING LANGUAGES
TENTH EDITION
Robert W. Sebesta
Chapter 9-Subprogram

Review Question

1. What are the three general characteristics of subprograms?
Answer: Each subprogram has a single entry point, excluding co-routine.
The calling program 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?
Answer: It means that after having been called, a subprogram has begun execution but has not yet completed that execution.

4. What are formal parameters? What are actual parameters?
Answer: The parameters in the subprogram header are called formal parameters.
Actual parameters are some names of the subprogram and a list of parameters to be bounded to the formal parameters of the subprogram when a subprogram call a statement

5. What languages allow a variable number of parameters ?
Answer: C,C++,Perl JavaScript, and Lua

6. What is a Ruby array formal parameter?
Answer: 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. This parameter is called the array formal parameter.

7. What is a parameter profile? What is a subprogram protocol?
Answer: Parameter profile is the number, order, and types of its formal parameters.
Subprogram protocol 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.

8. What are formal parameters? What are actual parameters?
Answer: 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 ?
Answer:
- Functions return values while procedure does not .
- Procedure defines new statements, while function define new user-defined operators.

22. Which ristriction are called bounds?
Answer: Restriction in Java that specify the range of classes that can pass to the generic method as generic parameters.

24. What is an overloaded subprogram?
Answer: Overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment. Every version of an overloaded subprogram must have a unique protocol; that is, it must be different from the others in the number, order, or types of its parameters, and possibly in its return type if it is a function.

32. What exactly is a delegate?
Answer: A delegate is the power and flexibility of method pointers in C# increased by making them objects.

34. What is a closure?
Answer: Closure is a nested subprogram ant its referencing environment, which together  allow the subprogram to be called from anywhare in a program

37. In what way coroutine different from conventional subprogram?
Answer: coroutine control mechanism is often called symmetric unit control model, in the otherhand conventional subprogram have a master-slave relationship between a caller and a called subprogram.
coroutine can have multiple entry point which are controlled by the coroutine themselves.


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.
Answer:
I think it is good. User program building addition definition can give an access for those who wants to make their own specific datatype and also their custom operator.
User define operator overloading is good I think, because the user can custom their own operator overloading as long as it does not make them confused.

3. Argue in support of the template functions of C++. How is it different from the template functions in other languages?
Answer: C++ templated classes are instantiated to become typed classes at compile time. For example, a templated Count class, can be created with the following declaration:
Count<int> doCount;


5.Consider the following program writen in C syntax:
void swap(int a, int b){
int temp;
temp = a;
a = b;
b = temp;
}
void main() {
int value = 1, list[5] = {2, 4, 6, 8, 10};
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
value =1 , list[5] = {2,4,6,8,10}
b. Passed by reference
value =6, list[5] ={4,1,2,8,10}
c. Passed by value-result
value =6, list[5] ={4,1,2,8,10}




7. Consider the following program written in C syntax:

void fun (int first, int second){
first += first;
second += second;
}
void main() {
int list [2] = {3,5};
fun(list[0], list[1]);
}
For each of the following parameter-passing methods, what are the values of the list array after execution?
Answer:
a. Passes by value : 3, 5
b. Passes by reference : 6, 10
c. Passes by value-result : 6, 10

8 . Argue against the Java design of not providing operator overloading
Answer: Arithmetic operators are often used for more than one purpose. For example, + usually is used to specify integer addition and floating-point addition. Some languages—Java, for example—also use it for string concatenation. This multiple use of an operator is called operator overloading and is generally thought to be acceptable, as long as neither readability nor reliability suffers.

 Special thanks to Mr. Tri Djoko Wahjono, Ir., M.Sc.

Tidak ada komentar:

Posting Komentar