June 17, 2013

KBP chapter 10

Review questions 

1.What is the definition used in this chapter for “simple” subprograms?
-Subprograms cannot be nested and all local variables are static.

2. Which of the caller or callee saves execution status information?
-Either can sqve the execution status

3. What must be stored for the linkage to a subprogram?
- Execution status information

4. What is the task of a linker?
- find files that contain the translated subprograms referenced in that program and load them into memory, set target addresses of all calls to those subprograms in the main program to the entry addresses of those subprograms.

6. What is the difference between an activation record and an activation
record instance?
-The format, or layout, of the noncode part of a subprogram is called an
activation record.

-An activation record instance is a concrete example of an activation

8. What kind of machines often use registers to pass parameters?

11. What is an EP, and what is its purpose?
Enviroment Pointer.
The EP is used as the base of the offset addressing of the data contents of
the activation record instance—parameters and local variables.

Problem Set

6. Although local variables in Java methods are dynamically allocated at the beginning of each activation, under what circumstances could the value of a local variable in a particular activation retain the value of previous activation ?
-only if variable is declared as static. 
-Static modifier is a modifier that makes a variable history sensitive.

7. It is stated in this chapter that when nonlocal variables are accessed in a dynamic-scoped language using the dynamic chain, variable names must be stored in the activation records with the values. If this were actually done, every nonlocal access would require a sequence of costly string comparisons on names. Design an alternative to these string comparisons that would be faster.
-auxiliary data structure called a display. Or, to write variable names as integers. These integers act like an array. So when the activation happens, the comparisons will be faster.

9. The static-chain method could be expanded slightly by using two static links in each activation record instance where the second points to the static grandparent activation record instance. How would this approach affect the time required for subprogram linkage and nonlocal references ?
-Including two static links would reduce the access time to nonlocals that are defined in scopes two steps away to be equal to that for nonlocals that are one step away. Overall, because most nonlocal references are relatively close, this could significantly increase the execution efficiency of many programs.

11. If a compiler uses the static chain approach to implementing blocks, which of the entries in the activation records for the subprograms are needed in the activation records for blocks?
-the static chain connects all the static ancestors of an executing subprogram, in order of static parent first.

KBP chapter 9

Rickvian Aldi / 02PCT / 1601253441
Assignment By Mr. Tri Djoko Wahjono

Review Questions
1. What are three general characteristics of subprograms ?
- each subprograms 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 ?
- if it has been called and started execution but the execution is not completed yet.

3. What is given in the header of a subprogram ?
- specifies that the following syntactic unit is a subprogram definition of some particular kind, provides name of the subprogram, optionally specifies a list of parameters
4. What characteristics of Python subprograms sets them apart from those of other languages ?
- function def statements are executable
5. What languages allow a variable number of parameters ?
- C,C++,Perl JavaScript, and Lua
6. What is a Ruby array formal parameter ?
- Substitute of keyword parameter which Ruby does not support
7. What is a parameter profile ? What is a subprogram protocol ?
- Parameter profile contains number, order and types of its formal parameter. Subprogram Protocol is the parameter profile and the return value if it is a function.
8. What are formal parameters ? What are actual parameters ?
- formal parameters are parameters in the subprogram header. Actual parameters are a list of parameters included in subprogram call statements
9. What are the advantages and disadvantages of keyword parameters ?
- The advantage is they can appear in any order in the actual parameter list. The disadvantage is the user must know the names of formal parameters.
10 . What are the differences between a function and a procedure ?
- Functions return values while procedure does not .
- Procedure defines new statements, while function define new user-defined operators.

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 :
-Operator overloading is created to make coding easier by able to add a non number object. This will act like an function overloading, which is represent by a operator symbol. but behind that, operator overloading is user defined, so the use of it can be known by only the programmer who coded the overloading. too much overloading will make ambiguity of the program increasing.

3. Argue in support of the templated functions of C++. How is it different from the templated functions of other languages ?
Answer : 
-Templated function will allow function to recieve many diffrent datatyle, for sake of readibility, templated functions is preferable than creating function overloading.

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. )Passed by value
 list[2] = {3,5}
b. )Passed by reference
 list[2] = {6,10}
c. )Passed by value-result
 list[2] = {6,10}

8.Argue against the Java design of not providing operator overloading.
Answer :
Once again, operator overloading is user defined. This will decrease readibility significantly which is make sense if operator overloading is not provided.