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

Chapter 7 Functions Procedures (Part 1)

Cours ingénieur informatique batna fonction

Uploaded by

Edu Ca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Chapter 7 Functions Procedures (Part 1)

Cours ingénieur informatique batna fonction

Uploaded by

Edu Ca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter 7 : Sub-algorithms (Procedures and Functions)

Chapter 7 : Sub-algorithms (Procedures


and Functions)
1. Introduction

As we progress in designing an algorithm, it can grow in size and complexity. In addition, sequences
of instructions can be repeated in different places. An algorithm written this way quickly becomes difficult
to understand and manage as soon as it exceeds a certain size.
The solution to this problem is to divide the algorithm into several smaller parts. These parts are
commonly referred to as sub-algorithms. The sub-algorithm is written independently of the body of the
main algorithm and is invoked by it or by another sub-algorithm when required. There are two main types
of sub-algorithms, namely procedures and functions. They play an essential role in the structuring and
organization of programs.

The obvious advantage of sub-algorithms is the reduction in the size of program code, since procedure
or function calls avoid duplicating multiple lines of code. Here are some key characteristics of sub-
algorithms:
• Structuring (Independence): Sub-algorithms are designed to perform a specific task
independently. They represent parts or sub-parts of the problem to be solved and which thus
logically structure the program. This promotes modularity and makes code maintenance easier.
• Location : Functions are textual units in which not only the actions to be performed are defined,
but also local data or objects whose existence is limited to that of the procedure or function in
which they are defined.
• Parameterization: Sub-algorithms can take parameters as input and return results as output.
Thanks to the concept of parameter, it will be possible to call a sub-algorithm to execute the same
series of statements on different data values.
• Clarity and abstraction: The use of sub-algorithms improves the clarity of the code by breaking
it up into smaller, more understandable sections. They also allow implementation details to be
abstracted, making it easier to understand the overall program.

The judicious use of procedures and functions therefore contributes to making the code more
readable, more modular and more easily maintainable. In addition, it allows for better code reuse and
abstraction of implementation details, making it easier to understand and manage computer programs.

2. Types of sub-algorithms

Sub-algorithms can be classified into two types: functions and procedures.

2.1. Functions: Functions are sub-algorithms that return, after execution, a simple type value that
may appear in an expression, in a comparison, to the right of an assignment, etc. They are
commonly used to perform calculations and provide a result to the calling program.
Page | 1
Chapter 7 : Sub-algorithms (Procedures and Functions)

2.2. Procedures: Unlike functions, procedures are sub-algorithms that do not have an explicit return
value. They are used to group a set of instructions without the need to return a specific value.
3. Declaration of sub-algorithms
3.1. Declaration of a procedure
In algorithmic language
Procedure name_procedure (Parameter_1:type_1, …,Parameter_n:type_n)
// Part of local declarations
Begin
// Instructions
End;

In C language
void name_procedure(type_1 Parameter_1, …,type_n Parameter_n){
// Part of local declarations
// Instructions
}

Call of the procedure


• To start the execution of a procedure in a program, simply call it.
• The call of a procedure is written by putting its name and the list of parameters, separated by
commas.
• When a procedure is called, the program interrupts its normal flow, executes the instructions of
the procedure, then returns to the calling program and executes the next instruction.
Syntax :
name_procedure (param_1, param_2, …., param_n);
Example : Let N and M be two positive integers.
Q1. Write an algorithm that displays all even numbers less than or equal to N.
Q2. Write a procedure without parameters that displays all even numbers less than or equal to N.
Q3. Using the previous procedure, write an algorithm that displays all even numbers less than or
equal to N.
Q4. Write a procedure with parameters that displays all even numbers less than or equal to N.
Q5. Using the previous procedure, write an algorithm that displays all even numbers less than or
equal to M.
Answer of Q1:
In algorithmic language
Algorithm display_even_numbers;
Var i,N : integer;
Begin
Repeat
read(N);
Until(N>=0);
write("the even numbers <= ",N, " are : ");
for (i0 to N by 2) do
write(i);
endfor;
End;

Page | 2
Chapter 7 : Sub-algorithms (Procedures and Functions)

In C language
#include <stdio.h>

int main(){
int i,N;
do{
scanf("%d",&N);
}while(N<0);
printf("the even numbers <= %d are : \n",N);
for (i=0;i<=N ;i=i+2) {
printf("%d\n",i);
}
return 0;
}

Answer of Q2:
In algorithmic language
Procedure even_numbers()
Var i: integer;
Begin
for (i0 to N by 2) do // N is a global variable
write(i);
endfor;
End;

In C language
void even_numbers()
{
int i;
printf("the even numbers <= %d are : \n",N);
for (i=0;i<=N ;i=i+2) { // N is a global variable
printf(“%d\n”,i);
}
}

Answer of Q3
In algorithmic language
Algorithm even_numbers_using_procedure_without_parameters;
Var N : integer;
Procedure even_numbers()
Var i: integer;
Begin
write("the even numbers <= ",N, " are : ");
for (i0 to N by 2) do
write(i);
endfor;
End;
Begin
Repeat
read(N);
Until(N>=0);
even_numbers();
End;

Page | 3
Chapter 7 : Sub-algorithms (Procedures and Functions)

In C language
#include <stdio.h>
int N;
void even_numbers();
int main(){
do {
scanf("%d",&N);
}while(N<0);
even_numbers();
return 0;
}
void even_numbers(){
int i;
printf("the even numbers <= %d are : \n",N);
for (i=0;i<=N ;i=i+2) {
printf("%d\n",i);
}
}

Answer of Q4
In algorithmic language
Procedure even_numbers(N: integer)
Var i: integer;
Begin
for (i0 to N by 2) do
write(i);
endfor;
End;

In C language
void even_numbers(int N) {
int i;
printf("the even numbers <= %d are : \n",N);
for (i=0;i<=N ;i=i+2) {
printf(“%d\n”,i);
}
}
Answer of Q5
In algorithmic language
Algorithm even_numbers_using_procedure_with_parameters;
Var N : integer;
Procedure even_numbers(N : integer)
Var i: integer;
Begin
write("the even numbers <= ",N, " are : ");
for (i0 to N by 2) do // N : formal parameter
write(i);
endfor;
End;
Begin
Repeat
read(N);
Until(N>=0);
even_numbers(N); //N : global variable and actual parameter
End.

Page | 4
Chapter 7 : Sub-algorithms (Procedures and Functions)

In C language
#include <stdio.h>
int N;
void even_numbers(int N);
int main(){
do {
scanf("%d",&N);
}while(N<0);
even_numbers(N);
return 0;
}
void even_numbers(int N){
int i;
printf("the even numbers <= %d are : \n",N);
for (i=0;i<=N ;i=i+2) {
printf(“%d\n”,i);
}
}

3.2. Declaration of a function


In algorithmic language
Function name_function(Parameter_1:type_1,…,Parameter_n:type_n)of return_type
// Part of local declarations
Begin
// instructions
Return (result);
End;

In C language
return_type name_function(type_1 Parameter_1, …,type_n Parameter_n) {
// Part of local declarations
// instructions
retrun (result) ;
}

3.3. Call of a function


To call (invoke) a function, we use its name followed, in parentheses, by the list of actual parameters.
The call can usually be made within an expression, in an assignment statement, in a write statement, or
even as an actual parameter of another call.
Example of syntax :

name_var  name_function (Param_1, Param_2, …., Param_n); or


write(name_function (Param_1, Param_2, …., Param_n));
In order to transmit the calculated value (result) to the calling algorithm (or sub-algorithm), we use one
of the following instructions:

Return (Result_Value); or
name_function  Result_Value ;

Page | 5
Chapter 7 : Sub-algorithms (Procedures and Functions)

Notes :
- The parameters (Parameter_1, Parameter_2, …., Parameter_n) used in the declaration of a
procedure or function are called formal parameters.
- For each formal parameter, specify its name and type. If no parameters are needed, the
parentheses must remain empty.
- The parameters (Param_1, Param_2, ...., Param_n) used when calling a procedure or function
are called actual (effective) parameters. Generally, formal parameters are used to receive
the values of actual parameters.
- The formal and effective parameters of the same sub-algorithm, in the same algorithm, must
be of the same number, while respecting their order and their types.
- To run an algorithm that contains procedures and functions, you have to start execution from
the main part (main algorithm).
- In C language, the expression after return must be of the same type as the function. The actual
parameters must be of the same types as the formal parameters. If these two rules are not
followed, the C compiler does not declare an error, but this can lead to unexpected or
erroneous results.
- In C language, if you call a function directly as a procedure, without it appearing in an
expression, the C compiler does not declare an error. In this case, the processing related to
the function will be performed, but the result returned by the function will not be used. If we do
the opposite, i.e. we invoke a procedure in an expression, this time the compiler declares an
error, and does not execute the program.

Example : Let N be a positive integer.


Q1. Write an algorithm that calculates and displays the factorial of N.
Q2. Write a function without parameters that calculates and returns the factorial N.
Q3. Using the previous function, write an algorithm that calculate and displays the factorial of N.
Q4. Write a function with parameters that calculates and returns the factorial of N.
Q5. Using the previous function, write an algorithm that calculate and displays the factorial of N.
Q6. Rewrite the algorithm of Q3 by replacing the function with a procedure.
Q7. Rewrite the algorithm of Q5 by replacing the function with a procedure.

Answer of Q1:
In algorithmic language
Algorithm factorial_integer;
Var i,N,fact : integer;
Begin
Repeat
read(N);
Until(N>=0);
Fact  1;
for (i1 to N by 1) do
Fact  Fact*i;
endfor;
write(N,"!= ",Fact);
End.

Page | 6
Chapter 7 : Sub-algorithms (Procedures and Functions)

In C language
#include <stdio.h>
int main(){
int i,N,Fact;
do{
scanf("%d",&N);
}while(N<0);
Fact=1;
for (i=1;i<=N ;i++) {
Fact*=i;
}
printf("%d!= %d\n",N,Fact);
return 0;
}
Answer of Q2
In algorithmic language
Function factorial() of integer
Var i,Fact: integer;
Begin
Fact  1;
for (i1 to N by 1) do // N is a global variable
Fact  Fact*i;
endfor;
return(Fact);
End;
In C language
int factorial() {
int i,Fact=1;
for (i=1;i<=N ;i++) { // N is a global variable
Fact*=i;
}
return(Fact);
}
Answer of Q3
In algorithmic language
Algorithm factorial_integer;
Var N,Fact : integer;
Function factorial() of integer
Var i,Fact: integer;
Begin
Fact  1;
for (i1 to N by 1) do // N is a global variable
Fact  Fact*i;
endfor;
return(Fact);
End;
Begin
Repeat
read(N);
Until(N>=0);
Fact  factorial();
write(N,"!= ",Fact);
End.
Page | 7
Chapter 7 : Sub-algorithms (Procedures and Functions)

In C language
#include <stdio.h>
int N,Fact;
int factorial();
int main(){
do {
scanf("%d",&N);
}while(N<0);
Fact=factorial();
printf("%d!= %d \n",N,Fact);
return 0;
}
int factorial(){
int i,Fact=1;
for (i=1;i<=N ;i++) {
Fact*=i;
}
return(Fact);
}

Answer of Q4
In algorithmic language
Function factorial(N:integer) of integer
Var i,Fact: integer;
Begin
Fact  1;
for (i1 to N by 1) do
Fact  Fact*i;
endfor;
return(Fact);
End;

In C language
int factorial(int N) {
int i,Fact=1;
for (i=1;i<=N ;i++) {
Fact*=i;
}
return(Fact);
}

Answer of Q5
In algorithmic language
Algorithm factorial_integer;
Var N,Fact : integer;
Function factorial(N:integer) of integer
Var i,Fact: integer;
Begin
Fact  1;
for (i1 to N by 1) do // N is a formal parameter
Fact  Fact*i;
endfor;
return(Fact);
End;

Page | 8
Chapter 7 : Sub-algorithms (Procedures and Functions)

Begin
Repeat
read(N);
Until(N>=0);
Fact  factorial(N);
write(N,"!= ",Fact);
End.
In C language
#include <stdio.h>
int N,Fact;
int factorial(int N);
int main(){
do {
scanf("%d",&N);
}while(N<0);
Fact=factorial(N);
printf("%d!= %d \n",N,Fact);
return 0;
}
int factorial(int N){
int i,Fact=1;
for (i=1;i<=N ;i++) {
Fact*=i;
}
return(Fact);
}
Answer of Q6
In algorithmic language
Algorithm factorial_integer;
Var N,Fact : integer;
Procedure factorial()
Var i: integer;
Begin
Fact  1;
for (i1 to N by 1) do // N is a global variable
Fact  Fact*i; // Fact is a global variable
endfor;
End;
Begin
Repeat
read(N);
Until(N>=0);
factorial();
write(N,"!= ",Fact);
End.

In C language
#include <stdio.h>
int N,Fact;
void factorial();
int main(){
do {
scanf("%d",&N);
}while(N<0);

Page | 9
Chapter 7 : Sub-algorithms (Procedures and Functions)

factorial();
printf("%d!= %d \n",N,Fact);
return 0;
}
void factorial(){
int i;
Fact=1;
for (i=1;i<=N ;i++) {
Fact*=i;
}
}

Answer of Q7
In algorithmic language
Algorithm factorial_integer;
Var N,Fact : integer;
Procedure factorial(N:integer)
Var i: integer;
Begin
Fact  1;
for (i1 to N by 1) do // N is a formal parameter
Fact  Fact*i; // Fact is a global variable
endfor;
End;
Begin
Repeat
read(N);
Until(N>=0);
factorial(N);
write(N,"!= ",Fact);
End.

In C language
#include <stdio.h>
int N,Fact;

void factorial(int N);

int main()
{
do {
scanf("%d",&N);
}while(N<0);
factorial(N);
printf("%d!= %d \n",N,Fact);
return 0;
}
void factorial(int N){
int i;
Fact=1;
for (i=1;i<=N ;i++) {
Fact*=i;
}
}

Page | 10
Chapter 7 : Sub-algorithms (Procedures and Functions)

Notes :
• If several instruction blocks are similar, but use different variables, it is possible to use
a sub-algorithm with parameters.
• Replacing a function with a procedure : It is possible to replace a function with a
procedure by using a global variable to retrieve the value returned by the function. In
the algorithm of Q3 the factorial function can be replaced with procedure by adding a
global variable Fact.

4. Variable scope (Local variables and global variables)

So far, we have explored how information is exchanged between different sub-algorithms using
parameters for the passage of values and the retrieval of results. However, it is also possible that several
sub-algorithms, as well as the main algorithm, share variables that are then called “global variables.”
Conversely, a sub-algorithm can also use its own variables, which are referred to as “local variables.”

4.1.Global variables : In programming, a global variable is shared by several functions and


procedures. It is declared outside of all functions and procedures.

4.2.Local variables : A variable is said to be local to a function (or procedure) if it is defined within it.
Local variables are only accessible inside the function (or procedure) where they are declared and
are not recognized outside of it.

Notes :
- The scope of a variable refers to the visibility domain of this variable.
- Global variables can be used in all sub-algorithms and in the main algorithm.
- The lifetime of the formal parameters and local variables of the same sub-algorithm is identical.
- It is possible to declare a local variable in a sub-algorithm, using the same name as a global
variable. In this situation, the local variable takes precedence over the global variable, masking
it in the context of the sub-algorithm.
- In C, the variables declared at the beginning of the main function are not global variables, but
they are local to main.
- In C, a variable declared in an instruction block is only visible inside this block. It is a local variable
to this block, it hides all the variables of the same name from the blocks that surround it.

5. Passing the parameters


Exchanges of information between a sub-algorithm and the main algorithm (or another sub-algorithm)
generally take place via parameters.
There are two main ways of passing parameters that allow different uses: passing by value and
passing by address (by reference).

5.1.Passing by value
In this passing mode :
• The formal parameter receives only a copy of the value of the actual parameter.
• The value of the actual parameter will never be changed.

Page | 11
Chapter 7 : Sub-algorithms (Procedures and Functions)

Example :
Let X and Y be two integers. We wish to swap their values. We propose the following algorithm, which
uses the value-passing mode to perform this swapping.
In C language
Algorithm Swapping_passing_value ;
Var X,Y : integer ;
Procedure Swapping(X : integer, Y : integer)
Var Z : integer ;
Begin
Z  X ;
X  Y ;
Y  Z ;
Write ("In the procedure : X= ", X, "Y= ", Y) ;
End ;
Begin //*main algorithm
X  2 ;
Y  5 ;
write ("In the main algorithm, Before the call : X= ",X,"Y= ",Y);
Swapping(X,Y) ;
write ("In the main algorithm, After the call : X= ",X,"Y= ",Y);
End.

In C language
#include <stdio.h>
int X,Y;
void Swapping(int X,int Y);
int main(){
X=2;
Y=5;
printf("In the main algorithm before the call : X=%d Y=%d\n",X,Y);
Swapping(X,Y);
printf("In the main algorithm after the call : X=%d Y=%d\n",X,Y);
return 0;
}
void Swapping(int X, int Y){
int Z ;
Z = X ;
X = Y ;
Y = Z ;
printf ("In the procedure : X= %d Y= %d\n",X,Y) ;
}

Outputs :
In the main algorithm before the call : X=2 Y=5
In the procedure : X= 5 Y= 2
In the main algorithm after the call : X=2 Y=5
The "Swapping_passing_value" algorithm highlights the concept of passing by value of the
parameters. In this algorithm, the integer global variables X and Y, initially set to 2 and 5, are declared
within the main algorithm. To implement passing by value, a procedure named "Swapping" is defined with
the formal parameters X and Y of type integer.
When calling the "Swapping" procedure with the actual parameters X and Y, their current values (2
and 5) are copied to the formal parameters X and Y of the procedure. This means that the formal
Page | 12
Chapter 7 : Sub-algorithms (Procedures and Functions)

parameters act as independent copies of the global variables, isolated within the procedure.
Within the procedure, a swapping is performed on the values of X and Y, changing them from 2 to 5
and from 5 to 2, respectively. However, it is crucial to emphasize that this change does not affect the values
of X and Y outside the procedure in any way. In other words, although significant changes are made to the
formal parameters X and Y within the procedure, the values of the global variables X and Y remain
unchanged after the call of the procedure.
This implementation of passing by value clearly demonstrates the fundamental principle that formal
parameters act as independent copies of the original variables, thus ensuring the preservation of the initial
values of the global variables despite any internal manipulation carried out within the procedure.

5.2. Passing by address (or reference)


In this passing mode :
• The procedure uses the actual parameter address.
• When the parameter address is used, its content is accessed directly.
• The value of the actual (variable) parameter will therefore be changed.
• The parameters passed by address are preceded by the keyword Var.
Example : Let's take the previous example
In C language
Algorithm Swapping_passing_address ;
Var X,Y : integer ;
Procedure Swapping(Var X : integer, Var Y : integer)
Var Z : integer ;
Begin
Z  X ;
X  Y ;
Y  Z ;
Write ("In the procedure : X= ", X, "Y= ", Y) ;
End ;
Begin //*main algorithm
X  2 ;
Y  5 ;
write ("In the main algorithm, Before the call : X= ",X,"Y= ",Y);
Swapping(X,Y) ;
write ("In the main algorithm, After the call : X= ",X,"Y= ",Y);
End.

In C language
#include <stdio.h>
int X,Y;

void Swapping(int* X,int* Y);

int main(){
X=2;
Y=5;
printf("In the main algorithm before the call : X=%d Y=%d\n",X,Y);
Swapping(&X,&Y);
printf("In the main algorithm after the call : X=%d Y=%d\n",X,Y);
return 0;
}

Page | 13
Chapter 7 : Sub-algorithms (Procedures and Functions)

void Swapping(int* X, int* Y){


int Z ;
Z = *X ;
*X = *Y ;
*Y = Z ;
printf ("In the procedure : X= %d Y= %d\n",*X,*Y) ;
}

Outputs :
In the main algorithm before the call : X=2 Y=5
In the procedure : X= 5 Y= 2
In the main algorithm after the call : X=5 Y=2

The “Swapping_passing_address” algorithm highlights the concept of passing parameters by address.


First, in the main algorithm, the global variables X and Y of integer type (initialized to 2 and 5) are declared.
Next, a procedure named "Swapping" is defined with the formal parameters X and Y of integer type, but
this time preceded by the keyword “var.”
When the "Swapping" procedure is called with the effective parameters &X and &Y, it is now the
memory addresses of the X and Y variables that are passed, rather than their direct values.
Within the procedure, operations performed on memory addresses directly change the values stored
at these locations. Thus, when permuting values inside the procedure (going from 2 and 5 to 5 and 2), this
modification directly affects the values of the global variables X and Y outside the procedure. Unlike the
passage by value, the passing by address allows an effective modification of the global variables by the
direct manipulation of their memory addresses.
In summary, the passing by address allows a procedure to manipulate the values of the global
variables by acting on the corresponding memory addresses, thus ensuring a direct and effective
modification of the data, unlike the passing by value which simply copies the values without affecting the
original variables.

Notes :
- When using passing by value, it is possible to call the sub-algorithm by actual parameters
expressed in the form of expressions, for example, name_sub_algo(N*4), name_sub_algo(4).
In C language :
* The parameters of the printf function are passed by value.
* The parameters of the scanf function are passed by address.
* It is possible to define a sub-algorithm with an array parameter.
* the array variable corresponds to the address of the first element (element 0) of this array.
So, passing an array as a parameter is one pass by address, without specifying it with *,
or calling it by &.

Page | 14

You might also like