PLC Session 7

Session 7: Subprograms

A. Introduction to Subprograms

 

A subprogram is a sequence of program instructions that perform a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed. Subprograms may be defined within programs, or separately in libraries that can be used by multiple programs. In different programming languages, a subroutine may be called a function, a procedure, a routine, a method, or a subprogram.

–     Fundamental Abstraction Facilities

Two fundamental abstraction facilities can be included in programming language :

  • Process abstraction
    • Emphasized from early days
    • Discussed in this chapter
  • Data abstraction
    • Emphasized in the 1980s

B. Subprograms

Subprograms are the most important concepts in programming language design. Subprograms usually describe computations. There are 2 ways that a subprogram can gain access to the data that is to process: through direct access to nonlocal variables or through parameter passing (will be explained later in parameters part). Subprograms include the following characteristics :

  • Each subprogram has a single entry point
  • There is only one subprogram execution at any given time
  • Control always returns to the caller when the subprogram execution terminates.

In subprograms, there are some basic definition or term :

  • A subprogram definition describes the interface and the actions of the subprogram abstraction.
  • A subprogram call is the explicit request so the subprogram be executed.
  • A subprogram is active if after having been called, it has begun execution but has not completed that execution.
  • Two fundamental kinds of subprograms are procedures and functions.
  • A function is a subprogram that returns a value. That is, a function be evaluated as a component of an expression, because a function call produces a value. Some languages allow a function result to be “ignored”, in which case a function call can be a statement in a program. (This means, however, that the function would have to have a side-effect to be of value.)
  • A procedure is a subprogram that does not return a value. Any useful work that is done by a procedure is through changes to external data, including files and arguments. A procedure may also be called a subroutine, but this term is not used much any more.
  • As indicated in the text, the components of a subprogram are its name, its signature, and its action. The signature of a subprogram consists of its name and its parameter and return value specifications. (Usually only a single return value is allowed, but often it can be a composite object such as a list or a record.) The action is represented by the “body” of the subprogram definition.
  • A subprogram header, which is the first line of the definition, serves several purposes. It specifies that the following syntactic unit is a subprogram definition, and provides the name for the subprogram. It may also optionally specify list of parameters.
  • The parameter profile of a subprogram is the number, order and types of its formal parameters.
  • The protocol of a subprogram is its parameter profile plus — if it is a function — its return types.
  • Subprograms declarations are common in C programs where they are called

C. Local Referencing Environment

  • Stack-dynamic local variables

–   Advantages

  • Support for recursion
  • Storage for locals is shared among some subprograms

–   Disadvantages

  • Allocation/de-allocation, initialization time
  • Indirect addressing
  • Subprograms cannot be history sensitive
  • Static local variables
  • Advantages and disadvantages are the opposite of those for stack-dynamic local variables.

D. Parameters

Data passed through parameters are accessed through names that are local to the subprogram.  Parameter passing is more flexible than direct access to nonlocal variables. The parameters in the subprogram header are called formal parameters. Subprograms 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. The binding of actual parameters to formal parameters – is done by simple position: the first actual parameter is bound to the first formal parameter and so forth.  Such parameters are called positional parameters. When lists are long, it is easy for the program writer to make mistakes in the order of 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. It is sometimes convenient to pass subprogram names as parameter.

E. Parameter Passing

Parameter-passing methods are the ways in which parameters are transmitted to and / or from called programs. There are 2 conceptual models of how data transfers take place in parameter transmission, either an actual value is physically moved (to the caller, to the callee, or both ways), or an access path is transmitted. Formal parameters are characterized by one of three semantics models :

  • In mode
  • Out mode
  • Inout mode

Pass by Value (In mode)

They can receive data from the corresponding actual parameter. The value of the actual parameter is used to initialize the corresponding formal parameter. Normally this model implemented by copying. They can be implemented by transmitting an access path but not recommended (enforcing write protection is not easy).

Disadvantages :

  • By physical move : additional storage is required (stored twice) and the actual move can be costly (for large parameters)
  • By access path method : must write-protect in the called subprogram and accesses cost more (indirect addressing

Pass by Result (Out mode)

They can transmit data to the actual parameterWhen a parameter is passed by result, no value is transmitted to the subprogram; the corresponding formal parameter acts as a local variable; its value is transmitted to caller’s actual parameter when control is returned to the caller, by physical move. Requires extra storage location and copy operation. Some potential problems of this model :

  • sub (p1, p1) ; whichever formal parameter is copied back will represent the current value of p1
  • sub (list[sub], sub) ; compute address of list[sub] at the beginning or the end of subprogram (?)
  • Pass by Value Result (Inout mode)

Combination of pass by value and pass by result. The formal parameters of this model have local storage. Sometimes called pass-by-copy.

Disadvantages: Those of pass-by-result and pass-by-value

Pass by Reference (Inout mode)

Pass an access path, also called pass-by-sharing. Pass-by-reference are the simplest to implement; only an address is placed in the stack.

Advantage : Passing process is efficient (no copying and no duplicated storage)

Disadvantages :

  • Slower accesses (compared to pass-by-value) to formal parameters
  • Potentials for unwanted side effects (collisions)
  • Unwanted aliases (access broadened)

fun(total, total);  fun(list[i], list[j];  fun(list[i], i);

Pass by Name (Inout mode)

Worked by textual substitution. Formals are bound to an access method at the time of the call, but actual binding to a value or address takes place at the time of a reference or assignment. Allows flexibility in late binding. Implementation requires that the referencing environment of the caller is passed with the parameter, so the actual parameter address can be calculated.

F. Design Considerations for Parameter Passing

  • Two important considerations
    • Efficiency
    • One-way or two-way data transfer

But the above considerations are in conflict with :

  • Good programming suggest limited access to variables, which means one-way whenever possible
  • Pass-by-reference that is more efficient to pass structures of significant size

G. Referencing Environment

  • Shallow binding: The environment of the call statement that enacts the passed subprogra Most natural for dynamic-scope languages
  • Deep binding: The environment of the definition of the passed subprogram. Most natural for static-scoped languages
  • Ad hoc binding: The environment of the call statement that passed the subprogram

 

Usually when there are several possible subprograms to be called and the correct one on a particular run of the program is not know until execution (e.g., event handling and GUIs). In C and C++, such calls are made through function pointers.

H. Overloaded Subprograms

An overloaded subprogram is subprograms that have the same name in the same referencing environment. Every version of an overloaded subprogram has a unique protocol. C++, Java, C#, and Ada include predefined overloaded subprograms and allow user to write multiple versions of subprograms with the same name.

I. Generic Subprograms

A generic or polymorphic subprogram takes parameters of different types on different activations. Overloaded subprograms provide ad hoc polymorphism, while subtype polymorphism means that a variable of type T can access any object of type T or any type derived from T (OOP languages). A subprogram that takes a generic parameter that is used in a type expression that describes the type of the parameters of the subprogram provides parametric polymorphism – a cheap compile-time substitute for dynamic binding -.

 

J. User Defined Overloaded Operators (C++)

Example :

#include <iostream>

using namespace std;

 

class printData {

public:

void print(int i) {

cout << “Printing int: ” << i << endl;

}

 

void print(double  f) {

cout << “Printing float: ” << f << endl;

}

 

void print(char* c) {

cout << “Printing character: ” << c << endl;

}

};

 

int main(void) {

printData pd;

 

// Call print to print integer

pd.print(5);

 

// Call print to print float

pd.print(500.263);

 

// Call print to print character

pd.print(“Hello C++”);

 

return 0;

}

When the above code is compiled and executed, it produces the following result:

Printing int: 5Printing float: 500.263Printing character: Hello C++

 

 

 

K. Closures

A closure is a subprogram and the referencing environment where it was defined. The referencing environment is needed if the subprogram can be called from any arbitrary place in the program. Closures are only needed if a subprogram can access variables in nesting scopes and it can be called from anywhere. To support closures, an implementation may need to provide unlimited extent to some variables (because a subprogram may access a nonlocal variable that is normally no longer alive).

L. Coroutines

A coroutine is a subprogram that has multiple entries and controls them itself, also called symmetric control: caller and called coroutines are on a more equal basis, A coroutine call is named resume. The first resume is to its beginning, but subsequent calls enter at the point just after the last executed statement. Coroutines repeatedly resume each other, and provide quasi-concurrent execution of program units (the coroutines); their execution is interleaved, but not overlapped.

 

Source(s) :

http://groups.engin.umd.umich.edu/CIS/course.des/cis400/maxim/lectures/chp8.htm

https://en.wikipedia.org/wiki/Subroutine

https://people.cs.clemson.edu/~turner/courses/cs428/spring00/webct/content/pz/ch5/ch5_2.html

Slide Binus Maya

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.