Saturday, 13 March 2021

Fundamentals of Subprograms | PPL | Sebesta

                      In this post, we will see Fundamentals of Subprograms | PPL | Sebesta | fundamentals of subprograms, ppl, sebesta, fundamentals of subprograms 

1. Fundamentals of Subprograms


Characteristics of Subprograms:

a. Each subprogram has a single entry point.

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

c. Control always returns to the caller when the subprogram execution terminates.




1. Subprogram Call

              A subprogram call is the explicit request that a specific subprogram be executed.



2. Active Subprogram

              A subprogram is said to be active if, after having been called, it has begun execution but has not yet completed that execution.


3. Subprogram Header

              A subprogram header, which is the first part of the definition, serves several purposes.



              def adder parameters :

       This is the header of a Python subprogram named adder .

       Ruby subprogram headers also begin with def .

       The header of a JavaScript subprogram begins with function.

       In C, the header of a function named adder might be as follows:

              void adder ( parameters )


4. Body of Subprogram

              The body of subprograms defines its actions.

              In the C-based languages (and some others—for example, JavaScript) the body of a subprogram is delimited by braces.

              In Ruby, an end statement terminates the body of a subprogram.

              As with compound statements, the statements in the body of a Python function must be indented and the end of the body is indicated by the first statement that is not indented.



5. Parameter Profile

              The parameter profile of a subprogram contains the number, order, and types of its formal parameters.


6. Protocol

              The protocol of a subprogram is its parameter

profile plus, if it is a function, its return type.


7. Subprogram Declaration

              Subprogram declarations provide the subprogram’s protocol but do not include their bodies. Function declarations are common in C and C++ programs, where they are called prototypes. In most other languages (other than C and C++), subprograms do not need declarations, because there is no requirement that subprograms be defined before they are called.


8. Formal Parameters

              The parameters in the subprogram header are called formal parameters.


9. Actual Parameters

              Subprogram call statements must include the name of the subprogram and a list of parameters to be bound to the formal parameters of the subprogram. These parameters are called actual parameters.


10. Positional Parameters

              The correspondence between actual and formal parameters or the binding of actual parameters to formal

parameters is done by position. Such parameters are called positional parameters.


11. Keyword Parameters

              When lists are long, however, it is easy for a programmer to make mistakes in the order of actual parameters in the list. One solution to this problem is to provide keyword parameters, in which the name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter in a call.


e.g. Python functions can be called using this technique, as in

sumer(length = my_length, list = my_array, sum = my_sum)

       where the definition of sumer has the formal parameters length, list, and sum.


              Some authors call actual parameters arguments and formal parameters just parameters.

Watch following video:

Watch on YouTube:

No comments:

Post a Comment