0% found this document useful (0 votes)
814 views

Fundamentals of Subprograms

This document discusses subprograms, which are fundamental building blocks of programs. It covers subprogram definitions and calls, local referencing environments, parameter passing methods, parameters as subprograms, and other concepts like overloaded and polymorphic subprograms. Key points include that subprograms have a single entry point and control returns to the caller on completion, and there are different ways of passing parameters between subprograms, such as pass by value, reference, or result.

Uploaded by

Von Gary Ras
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
814 views

Fundamentals of Subprograms

This document discusses subprograms, which are fundamental building blocks of programs. It covers subprogram definitions and calls, local referencing environments, parameter passing methods, parameters as subprograms, and other concepts like overloaded and polymorphic subprograms. Key points include that subprograms have a single entry point and control returns to the caller on completion, and there are different ways of passing parameters between subprograms, such as pass by value, reference, or result.

Uploaded by

Von Gary Ras
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

IT2009

Subprograms
Fundamentals of Subprograms
• A subprogram is a program called by another program to perform a specific task for the program.
• Subprograms are the fundamental building blocks of programs.
• Characteristics of subprograms:
o Each subprogram has a single entry point.
o There is only one (1) subprogram in execution at any given time.
o Control always returns to the caller when the subprogram execution terminates.
• A subprogram definition is a description of the interface and the actions of the subprogram
abstraction.
• A subprogram call is the explicit request that a specific subprogram be executed.
• A subprogram header, which is the first part of the definition, includes the name, the kind of
subprogram, and the formal parameters.
• Examples of headers:
Fortran: subroutine avg (a, b, c)
Ada: procedure Avg (A, B: in Integer; C: out Integer)
C: void avg (float a, float b, float c)
Python: def avg (a, b, c)
• The subprogram’s parameter profile, which is the first part of the definition, includes the name, the
kind of subprogram, and the formal parameters.
• Actual parameters represent values or addresses used in the subprogram call statement.
• A parameter is said to be a:
o Positional parameter: The first actual parameter is bound to the first formal parameter and
so forth.
o Keyword parameter: The name of the formal parameter to which an actual parameter is to
be bound is specified with the actual parameter in a call.
• A default value is used if no actual parameter is passed to the formal parameter in the subprogram
header.
• Two (2) categories of subprograms:
o Procedures provide user-defined parameterized computation statements.
o Functions are semantically modeled on mathematical functions.

Local Referencing Environments


• An association is the binding of identifiers to particular data objects and subprograms.
• The set of identifier associations for a given subprogram is called the referencing environment of the
subprogram.
• A local referencing environment is the set of associations created on entry to a subprogram that
represent formal parameters, local variables, and subprograms defined only within that subprogram.
• Local variables are variables that are defined inside subprograms. A local variable can be:
o A static variable is bound to a memory cell before program execution to the end of program
execution.
o A stack-dynamic variable is bound to storage when the subprogram begins execution and
unbound from storage when the execution terminates.
• A C example with static and stack-dynamic local variables:
int adder(int list[], int list_len) {
static int sum = 0;
int count;
for(count = 0; count < list_len; count++)
sum += list[count];
return sum;
}

08 Handout 1 *Property of STI


Page 1 of 3
IT2009

The variable sum is static while count is stack-dynamic.


C++, Java, and C# use only stack-dynamic variables in their methods.

Parameter-Passing Methods
• A parameter-passing method is a mechanism in which a parameter is transmitted to and/or from a
called subprogram.
• Three (3) semantic models of formal parameters:
o In mode: They can receive data from the corresponding actual parameter.
o Out mode: They can transmit data to the actual parameter.
o Inout mode: They can do both.

Figure 8.1 Three (3) Semantic Models of Formal Parameters

• Five (5) parameter-passing methods:


o Pass-by-Value – the value of the actual parameter is used to initialize the corresponding
formal parameter
o Pass-by-Result – no value is transmitted to the subprogram
o Pass-by-Value-Result – a combination of pass-by-value and pass-by-result
o Pass-by-Reference – transmits an access path (usually an address) to the called subprogram
o Pass-by-Name – the actual parameter is textually submitted for the corresponding formal
parameter in all its occurrences in the subprograms

Figure 8.2 Implementation of Parameter-Passing Methods

08 Handout 1 *Property of STI


Page 2 of 3
IT2009

The function header is:


void sub(int a, int b, int c, int d)
The subprogram sub is called from main with the call sub(w, x, y, z), where w is passed
by value, x is passed by result, y is passed by value-result, and z is passed by reference.
Parameters as Subprograms
• Languages that allow nested subprograms need to select one of the three (3) referencing
environments to use in executing the passed subprogram:
o Shallow binding – the environment of the call statement that enacts the passed subprogram
o Deep binding – the environment of the definition of the passed subprogram
o Ad hoc binding – the environment of the call statement that passed the subprogram as an
actual parameter

Other Concepts
• An overloaded subprogram is a subprogram that has the same name as another subprogram in the
same referencing environment.
• An overloaded subprogram must be different from the others in terms of number, order, types of its
parameters, and return type (for functions).
• A polymorphic subprogram takes parameters of different types on different activations.
• Ad hoc polymorphism is a kind of polymorphism provided by overloaded subprograms.
• Subtype polymorphism occurs when a variable of a type can access any object of that type or any
type derived from it.
• Parametric polymorphism is provided by a subprogram that takes generic parameters that are used
in type expressions that describe the types of the parameters of the subprogram.
• Parametrically polymorphic subprograms are called generic subprograms.
• A closure is a subprogram and the referencing environment where it was defined.
• Example of a closure in JavaScript:
function adder(x) {
return function(y) {return x + y;}
}
...
var add10 = adder(10);
var add5 = adder(5);
document.write(“Add 10 to 20: ” + add10(20) + “<br />”);
document.write(“Add 5 to 20: ” + add5(20) + “<br />”);
• A coroutine is a special program that has multiple entries. It can be used to provide interleaved
execution of subprograms.
• A skeletal coroutine:
sub co1(){
...
resume co2();
...
resume co3();
...
}

References:

Ben-Ari, Mordechai (2006). Understanding programming languages. Chichester: John Wiley & Sons, Inc.

Sebesta, Robert W. (2015). Concepts of programming languages. (11th ed.). USA: Pearson Education, Inc.

Tucker, Allan B. and Noonan, Robert E. (2002). Programming languages: Principles and paradigms. (2nd ed.).
New York: Mc-Graw Hill

08 Handout 1 *Property of STI


Page 3 of 3

You might also like