Interest of Procedural Programming: Solution: Idea
Interest of Procedural Programming: Solution: Idea
Interest of Procedural Programming: Solution: Idea
Benefits:
• Clarity of the algorithm
• A more readable algorithm
• Ease of maintenance
• Reuse of sub-algorithms
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
.
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
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);
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
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