Pages

Sunday 23 June 2013

Chapter 9

An assignment from Mr. Tri Djoko Wahyono (again). As what the title said, it is the answer for some question (9 numbers, quite randomly in Review Qustion and 4 numbers in Problem Set) in Chapter 9 in "Concept of Programming Language" book.


Review Question

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.

4. What characteristic of Phyton subprogram sets them apart from those of other language?
One characteristic of Phyton functions that sets them apart from  the functions of other common programming languages is that function def statements are executable.

7. What is a parameter profile? What is a subprogram protocol?
A parameter profile of a subprogram contains the number, order, and types of its formal parameters.
A protocol of a subprogram is its parameter profile plus, if it is a function, its return type.

11. What are the design issues for subprograms?

  • Are local variables statically or dynamically allocated?
  • Can sub program definitions appear in another subprogram definitions?
  • What parameter-passing method or methods are used?
  • Are the types of the actual parameters checked against the types of the formal parameters?
  • If subprograms can be passed as parameters and subprograms can be nested, what is the referencing environment of a passed subprogram?
  • Can subprograms be overloaded?
  • Can subprograms be generic?
  • If the language allows nested subprograms, are the closures supported?


14. What languages allow subprogram definitions to be nested?
JavaScript, Phyton, Ruby, Lua, Algol 60, Algol 68, Pascal, and Ada.

15. What are the three semantic models of parameter passing?

  • In mode.
  • Out mode.
  • Inout mode.


20. What is the parameter-passing method of Phyton and Ruby called?
Pass-by-assignment.

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

Problem Set
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 = 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
b. Passed by reference
c. Passed by value-result

a. value =1 , list[5] = {2,4,6,8,10}
b. value =6, list[5] ={4,1,2,8,10}
c. value =6, list[5] ={4,1,2,8,10}

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



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?
a. Passes by value
b. Passes by reference
c. Passes by value-result

a. 3, 5
b. 6, 10
c. 6, 10

15.
How is the problem of passing multidimensional arrays handled by Ada?
Ada compilers are able to determine the defined size of the dimensions of all arrays that are used as parameters at the time subprograms are compiled.

No comments:

Post a Comment