8.1.6 Procedures and functions, Library Routines, Local and Global Variables
8.1.6 Procedures and functions, Library Routines, Local and Global Variables
Procedure Name
PROCEDURE Stars
OUTPUT "************"
ENDPROCEDURE
CALL Stars
Computer Science 2210
Compiled By: Bilal Khan
Parameters:
Parameters are the variables that store the
values of the arguments passed to a
procedure or function. Some but not all
procedures and functions will have
parameters.
Note:
When procedures and functions are defined, the first statement in the definition
is a header, which contains:
• the name of the procedure or function
• any parameters passed to the procedure or function, and their data type
• the data type of the return Computer
value for a function.
Science 2210
Compiled By: Bilal Khan
Because a function returns a value, it can be
called by assigning the return value directly into
a variable as follows:
MyTemp ← Celsius(MyTemp)
Inches * 2.4
ENDFUNCTION
ENDFUNCTION
ENDFUNCTION
Main Program
OUTPUT Total
Computer Science 2210
Compiled By: Bilal Khan
Practice Question 3:
ENDFUNCTION
ENDFUNCTION
Main Program
INPUT Num
SquareResult ← CalculateSquare(Num)
OUTPUT SquareResult
Computer Science 2210
Compiled By: Bilal Khan
8.1.7 Library routines
Library routines are fully tested and ready for use in a
program. An IDE includes a standard library of functions
and procedures.
Library routines Pseudocode Description
returns remainder of a
MOD Value1 ← MOD(10, 3) division.
(10 divided by 3, Outputs 1)
returns the quotient (that
is, the whole number part)
DIV Value2 ← DIV(10, 3)
of a division.
(10 divided by 3, Outputs 3)
returns a value rounded to
a given number of decimal
ROUND Value3 ← ROUND(6.97354, 2)
places.
(Outputs 6.97)
returns a random number
RANDOM Value4 ← RANDOM()
Computer Science 2210
between 0 and 1 inclusive
Compiled By: Bilal Khan
Local and global variables
• A global variable • A local variable
can be used in any can only be used
part of a program by the part of the
– its scope covers program it has
the whole been declared in;
program. it is restricted to
that part of the
program.
Computer Science 2210
Compiled By: Bilal Khan
Computer Science 2210
Compiled By: Bilal Khan
8.1.8 Creating a maintainable program