2013-06-23

Programming Language Concept (9)

Book : Concept of Programming Language

Page 436
Review Question
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

Page 438
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.
- It is good, as long as the user knows what he or she is doing. C++ default operators are only working for default data types. As users can make their own datatypes, custom operators are also needed.

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 :
(page 458)
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
-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}

7. Consider the following program written in C syntax :
(page 459)
For each of the following parameter-passing methods, what are the values of the list array after execution ?
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 }

Tidak ada komentar:

Posting Komentar