Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Interest of Procedural Programming: Solution: Idea

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 7

Interest of Procedural Programming

Solving a complex problem can generate thousands of lines of code.


Long algorithms, difficult to write, interpret, and maintain.
Solution:
Using a new resolution methodology: Procedural Programming
Idea:
Divide the complex problem into less complex sub-problems and write each one of them separately
within its proper environmen

Benefits:
• Clarity of the algorithm
• A more readable algorithm
• Ease of maintenance
• Reuse of sub-algorithms

More about benefits


As we have seen in the previous slides, the main key benefits of procedural programming are:
The clarity and readability of the algorithm,
The reuse of code,
The facility of maintaining it.
Let’s create an algorithm that calculates the sum of the successive elements.
The code below is our first try:
ALGORITHM array_sum_two_element
VAR
tab : ARRAY_OF type[50];
i,n : INTEGER;
BEGIN
// Read the size
Read(n);
// Read elements
FOR i FROM 0 TO n-1 STEP 1 DO
Read(tab[i]);
END_FOR
// Add successive element
FOR i FROM 0 TO n-2 STEP 1 DO
tab[i] := tab[i]+tab[i+1];
END_FOR
// Display the array
FOR i FROM 0 TO n-1 STEP 1 DO
Write(tab[i])
END_FOR
END

This algorithm works fine, but we can optimize it more.


As we can notice, the main idea is to create an algorithm that calculates the sum of successive
elements.
Even though they are necessary, the two pieces of code do not fit in the solution
They are, in fact, the reading of the array elements and the displaying of the result.
If we want to take advantage of procedural programming, we can divide the previous algorithm into
three sub-algorithm (each one of them will resolve a sub-problem). Our final version will look like:
ALGORITHM array_sum_two_element
VAR
tab : ARRAY_OF type[50];
i,n : INTEGER;
BEGIN
// Read the size
Read(n);
// Read elements
/* *** *** */
// Sub-algorithm reading elements of array
/* *** *** */
// Add successive element
// Sub-algorithm calculating successive elements of array
/* *** *** */
// Dispalay the array
// Sub-algorithm displaying elements of array
/* *** *** */
END

Don’t worry, this is just a draft, we will see how to create a procedure and function with more
details in the next slide
The idea behind procedural programing is to divide the complex
Problem
into less complex
sub-problems
and write each one of them separately within its proper
environment
.

2 types of sub algorithms:


• Procedure
• Function
The sub-algorithms (procedures or functions) are called (used) inside an algorithm, the variables of
this main algorithm will be the vessel that links between the sub-algorithms.
In other words, communication between two sub-algorithms will be via parameters (variables).
Each sub algorithm can have an input parameters, and can give an output parameters.

Using functions and procedures


In a computer algorithms there are often sections of the program that we want to re-use or repeat.
Chunks of instructions can be given a name - they are called functions and procedures. This saves
time by only having to execute (call) the function when it is required, instead of having to type out
the whole instruction set.
Algorithm and even Programming languages have a set of pre-defined (also known as built-in)
functions and procedures. Use some of them in our previous code such as Read(), Write(), push()....
If the programmer makes their own ones, they are custom-made or user-defined.

Procedures or functions?
A procedure performs a task, whereas a function produces information.
Functions differ from procedures in that functions return values, unlike procedures which do not.
However, arguments can be passed to both procedures and functions.
In a program for drawing shapes, the program could ask the user what shape to draw. The
instructions for drawing a square could be captured in a procedure. Nothing to return here, the
square is already drawn on the screen.
A function could calculate something like the VAT due on goods sold. the set of instructions of
calculation the VAT could be given the name 'calculate_VAT' and would be executed by running
(calling) that function, and returning the calculated value as a result.
A procedure performs a task whereas a function produces information
Call a function and asking it to run
How many values can be returned from a given function? 1

Description of Procedure:
PROCEDURE procedure_name(List_parameters)
VAR
set_declarations
BEGIN
Procedure’s body (set_instructions)
END

Use of Procedure: call it directly in the code “procedure_name(List_parameters);

Description of Function:
FUNCTION function_name(args) : return_type
VAR
set_declarations
BEGIN
set_instructions
RETURN value ;
END

Use of Function: call it like an affecting expression in the code “variable := function_name(args);”
Variable must be compatible with the return type of the function
Which of the following can be given an identifier?
Functions, procedures, parameters, arguments, variables and constants
Which of the following is a function?
Algorithm to calculate VAT on a product

Parameters
Parameters allow us to pass information or instructions into functions and procedures. They are
useful for numerical information such as stating the size of an object. The values passed in
parameters are called arguments.
Back to the draw a square example, we can create a procedure for that- but for it to be useful we
need to also be able to specify how large it should be. The following example creates a procedure
called 'draw_square'.
PROCEDURE draw_square(side_length : FLOAT)
.
END

In the line where we define the name for this procedure, we have included a variable called
‘side_length’ inside the brackets. Side_length is a parameter - it allows us to pass a value into the
procedure for it to use.

Parameters
Scope refers to where a function, procedure, variable or constant can be used. They can be local or
global.
global variable is declared in a way that makes it available to every other part of the program,
meaning we can call it in any function/procedure without passing as parameter. Whereas a local
variable is declared in a way that makes it only available within a specific function or procedure,
they are the variables declared in the VAR part of the function, the procedure, or the main algorithm
itself.
We do not have a global keyword to declare global variables. However, variables declared outside
functions, procedures and main algorithm have "file scope," meaning they are visible within the
file, unless they are shadowed by a like-named variable in a local scope. For example: if we have a
global variable called ‘X’ and inside a function we declared a local variable ‘X’, we cannot use the
global ‘X’ inside that function anymore

Parameters
Calling the previous procedure to create a square of size 50 for each side will be with this
instruction :
draw_square(50);

The 50 here is called the argument - because it is a value passed in.


You can add more parameters to a function or procedure by separating the variable names with
commas. For example:
PROCEDURE draw_rectangle(height : FLOAT, width : FLOAT, color : STRING[10])

Or you can optimize if two parameters with same type follow each other :
PROCEDURE draw_rectangle(height, width : FLOAT, color : STRING[10])

Note: You must respect the same order of the parameters when you call a function or procedure.
Global variables can be used in any function/procedure without the need of passing them in
parameters. True

Parameter Passing
There are different ways in which parameter data can be passed into and out of procedures and
functions. Let us assume that a function Y() is called from another function X(). In this case X is
called the “caller function” and Y is called the “called function”. Also, the arguments which X sends
to Y are called actual arguments and the parameters of Y are called formal arguments.
• Formal Parameter : A variable and its type as they appear in the prototype of the function or
method.
• Actual Parameter : The variable or expression corresponding to a formal parameter that
appears in the function or method call in the calling environment.
• Modes:
1. IN: Passes info from the caller to called.
2. OUT: Called writes values in caller.
3. IN/OUT: Caller tells called value of variable, which may be updated by called.

Parameter Passing
• Important methods of Parameter Passing
• Pass By Value : This method uses in-mode semantics. Changes made to formal parameter do
not get transmitted back to the caller. In other words, if you pass a parameter from your
caller function to the called function by value, that means that you are actually taking a copy
of the parameter first, and then passing that copy to the function. If you make any changes to
the copy of the parameter inside the function, it doesn't change the data held in the original
parameter. This method is also called as call by value.
• Pass by reference (aliasing) : This technique uses in/out-mode semantics. Changes made to
formal parameter do get transmitted back to the caller through parameters passing. If you
pass a parameter from your caller function to a called function by reference, that means that
you are actually passing a pointer to the function that points to the original parameter, the
original piece of data. If you make any changes to the parameter inside the function, then the
original parameter will be changed as well. This method is also called as call by reference.
Parameter Passing examples
Let’s use what we've learned !
In our example, we are going to create a procedure that takes a number as a parameter passed by
value and it will increment it:
PROCEDURE add_num(num:INTEGER) // num is the local variable for the procedure

BEgIN
num := num+1
Write(num) // will display 21
END
ALGORITHM test_by_value
VAR
myData : INTEGER := 20;

BEGIN
// display the variable
Write(myData); // will display 20
// call the procedure add_num
add_num(myData); // Calling the proc add_num with myData as a parameter
// display the variable again
Write(myData) // will display 20
END

Now if we manually execute this algorithm, the result will be: 20 21 20


as we can see that the variable myData doesn't change after executing the procedure add_num, that's
because the parameter was passed by value
The next type of passing parameters is the passing by reference.
As its name explains, the variable num will point at the same memory allocation of the variable
myData so any changes to num variable will affect the myData variable.
Here how it's going to be done:
PROCEDURE add_num(VAR num:INTEGER)
BEGIN
num := num+1
Write(num) // will display 21
END
ALGORITHM test_by_value
VAR
myData : INTEGER := 20;
BEGIN
// display the variable
Write(myData); // will display 20
// call the procedure add_num
add_num(myData); // now the variable num will point at
//the same memory allocation of myData variable so any changes of num will
affect myData
// display the variable again
Write(myData) // will display 21
END

The keyword to differentiate between passing by value and passing by reference is the VAR in the
parameter declaratio
Which Keyword is used to make the call by reference ?Var

You might also like