Concepts of programming language – Chapter 9

1. 3 General characteristics of subprogram

• 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.   A 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?

• 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.   Characteristics of Python subprograms that sets them apart from those of other languages:
– function def statements are executable

5.   Languages allow a variable number of parameters:
– C,C++,Perl JavaScript, and Lua

8.   Formal parameters are The parameters in the subprogram header. They are sometimes thought of as dummy variables because they are not variables in the usual sense.

Actual parameters are a list of parameters to be bound to the formal parameters of the subprogram.

9.   The advantage of keyword parameters is that they can appear in any order in the actual parameter list.

The disadvantage to keyword parameters is that the user of the subprogram must know the names of formal parameters.

15.   3 semantic models of parameter passing?

• They can receive data from the corresponding actual parameter;

• They can transmit data to the actual parameter; or

• They can do both.

These models are called in mode, out mode, and inout mode, respectively.

24.   An overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment.

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

30. The design issues for functions:

• Are side effects allowed?
• What types of values can be returned?
• How many values can be returned?

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.

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

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

3.   Argue in support of the templated functions of C++. How is it different from the templated functions of other languages?

It is different as C++ differentiates function based on overloading. It is not practical to make multiple function overloading in regard to writability and readability. Instead, creating a template allows a function to receive any datatype as long as the variation is based on the formal parameter definition.

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

Answer:

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 }

6.   Compare and contrast PHP’s parameter passing with that of C#

PHP’s parameter passing is similiar to that of C#, excfept that either the actual parameter or 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. Passed by value
b. Passed by reference
c. Passed by value-result

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 }

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

Leave a comment