Friday 28 June 2013

Chapter 9 of Concept of Programming Languanges

Review Questions : 
1. What are the three general characteristics of subprograms?
 Characteristics :
  • Each subprogram has a single entry point
  • The calling program unit is suspended during the execution of the called subprgoram, which implies thtat 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?
  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 statementes 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 the of the same type.

11. What are design issues for subprograms?
 Design issues :
  • Are the local variables statically or dynamically allocated?
  • Can subprogram definitions appear in other subprogram definitions?
  • What parameter passing method are used?
  • Are the type of the actual parameter checked against the type of the formal parameters?
  • Can subprogram be overloaded?
  • Can subprogram be generic?
  • If the language allows nested subprograms, are closure supported ?

12. What are the advantages and disadvantages of dynamic local variable?
 The Advantages :
  • Dynamic local variable is slightly more efficient
  • They require no run time overhead for allocation or deallocation
  • They allow subprograms to be history sensitive
The Disadvantages are :
  • Their inability to support recursion
  • Their storage cannot be shared with local variables

25. What is an ad hoc binding?
 Ad hoc binding is the environment of call statement that passed the subprogram as an actual parameter

34. What is a closure?
 A closure is a subprogram and its referencing environment

___________________________________________________

Problem Set Questions :
5.
void swap(int a, int b){
int temp;
temp = a;
a = b;
b = temp;
}

int main(){
int value = 1, list[5] = {2,4,6,8,10};
swap(value, list[0]);
swap(list[0], list[1]);
swap(value,list[value]);

}
getchar();
return 0;
}

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
Answer :
Passed by :
  • 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.
void fun (int first, int second){
first += first;
second += second;
}

void main(){
int list[2] = {3,5};
fun(list[0],list[1]);
printf(“%d%d”,list[0],list[1]);
}

For each of the following parameter passing methods, what are all of the values of the variable list[0] and list[1] after each of the three calls to swap
  • a. Passed by value
  • b. Passed by reference
  • c. Passed by value-result
 Answer :
Passed by :
  • a. list[0] = 3 | list[1] = 5
  • b. list[0] = 4 | list[1] = 6
  • c. list[0] = 4 | list[1] = 6


8. Argue against Java design of not providing operator overloading.
Java design of not providing operator overloading means safety, however, good programmers should be able to understand the concept of operator overloading. Operator overloading itself while can be confusing if used incorrectly(assigning operator + to a function that does something that subtracts or vice versa), Operator overloading itself can be very useful in creating a readable code. Operator overloading allow experienced programmers to be more flexible in their work.


14. Speculate on the issue of allowing function or method overloading in some programming languages. Why are they not allowed by many contemporary languages?
While function/method overloading is flexible, it can also cause confusion if the programmer were to decide to overload a lot of function with different purposes but under the same name.

No comments:

Post a Comment