Senin, 05 Januari 2015

About Me

Name             : Rizky Andiputra
Student ID     : 1801428554
Class              : LC02
Course           :  Programming Language Concept
Lecturer         : Tri Djoko Wahjono, Ir.,M.Sc.

CHapter 9: Subprograms

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 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?
    It means that after having been called, a subprogram has begun execution but has not yet completed that execution.

3. What is given in the header of a subprogram.
    Subprogram header, which is the first part of the definition, serves several purposes. First, it specifies that the following syntactic unit is a subprogram definition of some particular kind.1 In languages that have more than one kind of subprogram, the kind of the subprogram is usually specified with a special word. Second, if the subprogram is not anonymous, the header provides a name for the subprogram. Third, it may optionally specify a list of parameters.

 4. 
What characteristic of Python subprograms sets them apart from those of other languages?
    
One characteristic of Python functions that sets them apart from the functions of other common programming languages is that function def statements are executable. When a def statement is executed, it assigns the given name to the given function body. Until a function’s def has been executed, the function cannot be called.

Consider the following skeletal example:if . . .
def fun(. . .):
. . .
else
def fun(. . .):
. . .

5. What languages allow a variable number of parameters ?
    C,C++,Perl JavaScript, and Lua. Also, C# allows methods to accept a variable number of parameters, as long as they are of the same type.


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.

    FOR:
    It allows the developer to program using notation “closer to the target domain” and allows user-defined types a similar level of syntactic support as types built into the language. It can easily be emulated using function calls.

    AGAINST:
    It can be implemented according to user’s want, eventhough it is not logically true.

I think such user-defined operator overloading is good as long as user use it according to its logical rules. User must use for example, + operator to be overloaded to implement “add” not “substraction”. And sometimes, in C++ there is condition when user need to add many data in class, so user-defined operator like this is needed to make it easier.

2. . In most Fortran IV implementations, parameters were passed by reference, using access path transmission only. State both the advantages and disadvantages of this design choice.
    The main advantage of this method is the fast accesses to formal parameters in subprograms. The disadvantages are that recursion is rarely useful when values cannot be passed, and also that a number of problems, such as aliasing, occur with the method.

3. . Argue in support of the Ada 83 designers’ decision to allow the implementor to choose between implementing in out mode parameters by copy or by reference.
    The tradeoff is one of efficiency of passing parameters vs. efficiency of accessing the parameters in the subprogram. Ada leaves this choice to the implementor, rather than forcing a single solution, because different situations may require different approaches. Passing a large array which the subprogram only accesses a few times is faster with reference. Passing any argument that the subprogram accesses a lot is faster with copy.

4. Suppose you want to write a method that prints a heading on a new output page, along with a page number that is 1 in the first activation and that increases by 1 with each subsequent activation. Can this be done without parameters and without reference to nonlocal variables in Java? Can it be done in C#?
    This can be done in both Java and C#, using a static (or class) data member for the page number.

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. 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}