Chapter7: Subprograms Dr.
Saïfi Lynda
Chapter 7: Subprograms
1. Introduction
When faced with a complex problem, we often need a lengthy or complicated
algorithm. In such cases, the strategy is to divide and conquer, breaking down the
problem into sub-problems. Subsequently, an algorithm is written for each sub-
problem, referred to as a "module" or "subprogram" or "subroutines". Finally, we
integrate everything by calling these modules in the main algorithm.
2. Procedures
2.1. Definition
A procedure is a subprogram that performs one or more actions and can return 0, 1, or
multiple results. It’s a part of algorithm.
2.2. Syntax
Procedure procedure-name (list of formal parameters with their types separated by ',')
Declaration of local variables of the procedure
Begin
Body of the procedure
End
2.3. Procedure call
The procedure call is the instruction that allows its use in the main algorithm by
executing the code with effective parameters.
Procedure-name (list of real or effective parameters)
2.4. Example
A procedure that swaps the content of two integer variables, x and y, passed as
parameters.
Procedure swap (var a: integer, var b: integer)
Variable tmp: integer
Begin
tmp ← a;
a ← b;
b ← tmp;
End
Algorithm swap_main
Variables x, y: integer
Begin
Write('Enter the value of x:')
1
Chapter7: Subprograms Dr. Saïfi Lynda
Read(x)
Write('Enter the value of y:')
Read(y)
Write('before procedure call x= ',x,' and y= ',y)
Swap(x, y)
Write('after procedure call x= ',x,' and y= ',y)
End
2.5. Example in C language
#include <stdio.h>
void swap(int *a, int *b) {
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
int main() {
int x, y;
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of y: ");
scanf("%d", &y);
printf("Before procedure call x = %d and y = %d\n", x, y);
swap(&x, &y);
printf("After procedure call x = %d and y = %d\n", x, y);
return 0;
}
2.6. Parameter Passing
2.6.1. Pass by value
Formal parameters represent local variables within the subprogram, and any changes
to the parameter have no effect on the effective parameter. Values of real parameters
are retrieved in formal parameters for processing. This passage concerns the data
parameters or input variables. They are passed by value.
2.6.2. Pass by address
Formal parameters are associated with the addresses of real parameters. Changes to
formal parameters affect effective parameters. This mode applies to output variables.
These parameters are passed by address.
2
Chapter7: Subprograms Dr. Saïfi Lynda
[Link]
The first line of the procedure is called the "header" or "signature" and
"prototype" in C language.
The parameters passed by address are preceded by the keyword “var” or the
the keyword “Output” or the keyword “I/O”. In C language, they are preceded
by a star “ * ”.
When calling the procedure, the real variables replace the formal variables,
they must match in type, number, and order too.
The formal parameters and the effective parameters can have the same name.
In C language, the effective parameters are called "arguments".
3. Functions
3.1. Definition
A function is a subprogram that must return a single value of scalar type (integer, real,
character, boolean). This value is stored in a variable with the same name as the
function. The function is a particular case of the procedure (the procedure is the
general case of subroutines).
3.2. Syntax
Function function-name (list of formal parameters with their types separated by ',') :
result_type
Declaration of local variables of the function
Begin
List of actions of the function
End
3.3. Function call
Since a function returns a single result, it is called within the main algorithm by
assigning the returned value to a variable declared in that algorithm.
The call takes the following form:
Identifier ← function-name (list of effective parameters)
3.4. Remarks
The types of the identified variable and the returned result must be compatible.
The formal parameters of the function can only be data parameters (passed by
value) since they are input variables. Unlike procedures that can contain data
3
Chapter7: Subprograms Dr. Saïfi Lynda
parameters and result parameters (passed by address), they are the output
variables.
When calling the function or procedure, the effective or real parameter can be:
a variable, an arithmetic or logical expression or a call to another function that
returns a value of the same type as the corresponding formal parameter.
A main program can call several functions, and each function can be called
several times by the same main program.
3.5. Example
Write a function that takes two integers, a and b, as parameters and calculates a^b.
Function power (a: integer, b: integer): integer
Variable p, i: integer
Begin
p←1
For i going from 1 to b do p ← p * a
End for
power ← p
End
Algorithm power_main
Var x, y, z: integer
Begin
Write('Enter the value of x:')
Read(x)
Write('Enter the value of y:')
Read(y) z ← power(x, y)
Write(x, ' to the power of ', y, ' is ', z)
End
3.6. In C language
#include <stdio.h>
int x, y, z;
int power(int a, int b);
// Main function
int main() {
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of y: ");
scanf("%d", &y);
z = power(x, y);
printf("%d to the power of %d is %d\n", x, y, z);
4
Chapter7: Subprograms Dr. Saïfi Lynda
return 0;
}
// Function to calculate power
int power(int a, int b) {
int p = 1;
for (int i = 1; i <= b; i++)
p *= a;
return p;
}
[Link] Variables and Global Variables
Formal or real parameters are global variables known throughout the main program
and the subprogram. However, local variables are declared within a subprogram and
are not known in the main program.
For example, in the swap procedure, variables a and b are "global," and tmp is "local."
In the power function, variables a and b are "global," and variables p and i are "local."
4. Recursion
[Link]
A recursive function is a function that calls itself. It is defined by at least one base
case and at least one general case.
In the base case, we describe the cases for which the result of the function is easy to
calculate: the value returned by the function is directly defined.
On the other hand, in the general case: the function is called recursively and the result
returned is calculated using the result of the recursive call. At each recursive call, the
value of at least one of the effective parameters of the function must change. It is
always necessary to ensure that each general case converges to a base case.
[Link]
Example 1
Factorial of an integer.
Function Fact (n: integer): integer
Variable
Begin
If (n = 0) THEN Fact ← 1
Else Fact ← n * Fact(n-1)
End
5
Chapter7: Subprograms Dr. Saïfi Lynda
Example 2 In C language
A recursive subprogram that calculates the sum of the first N numbers.
int sumN(int n) { if (n == 1) return 1; else return (n + sumN(n-1)); }
Example 3
A recursive subprogram that calculates the sum of the first N squares.
For example, if N is 3, this subprogram will calculate 12 + 22 + 32. This subprogram is
defined only for N greater than 0.
Function SumFirstSquares (N: integer): integer
VAR f: integer
Begin f ← 1
IF (N = 1) THEN SumFirstSquares ← f
ELSE SumFirstSquares ← (N*N) + SumFirstSquares(N-1)
End