You are on page 1of 11

FUNCTIONS

and PROCEDURES
(Subroutines)
What is a subroutine?
• Computer programs can consist of thousands of lines of code.
(Just like a text book can have thousands of words).
• Program is divided into related functionality using modules
(In the same way that the text book is divided into chapters).
• In a computer program, specific functionality is divided up into
subroutines.

• A subroutine can be classified as a procedure or a function.


A procedure is a block of code that is called to perform a task.
A function is a block of code that is called to perform a task and will return
one or more values.
Syntax of defining a Procedure:
PROCEDURE<procure identifier> ( ) // This is a header procedure.
<statement(s)> // These statements are the procedure body.
ENDPROCEDURE
PROCEDURES:
A procedure is a named block of code that performs a specific task
but does not return a value. The procedure can be called by another
part of the program.

Syntax of defining a Procedure.


PROCEDURE<procure identifier> ( ) // This is the procedure header.
<statement(s)> // These statements are the procedure body.
ENDPROCEDURE
A procedure with parameters is defined as follows:
PROCEDURE<procure identifier> (parameterlist:Data Type)
<statement(s)>
ENDPROCEDURE

The <identifier> is the identifier used to call the procedure. Where used,
param1, param2, etc. are identifiers for the parameters of the procedure.
These will be used as variables in the statements of the procedure.

Procedure should be called as follows”


CALL <identifier>()
CALL <identifier>(Value1, Value2…….)
When parameters are used, Value1, Value2… must be of the correct data type as in the
definition of the parameter.

Parameters and Arguments


 A parameter is a variable in a procedure/ function definition. (Parameter is a variable in
the declaration of function)
When a procedure /function is called, the arguments are the data you pass into the procedure
/function’s parameters.

 Argument is the actual value of this variable that gets passed to procedure/function.

PROCEDURE<identifiers> (value1: Datatype, Value2:Datatype)

Parameters
CALL <identifier> (V1, V2)
Arguments

Use different variable names as it clashes or else.


Questions
1. Write a pseudocode to enter 2 subject marks in main program and pass them into CalTotAvg()
procedure to calculate total and average and output them.

PROCEDURE CalTotAvg (Mark1, Mark2: Integer)


DECLARE Total: Integer
DECLARE Avg: Real
Total Mark1+Mark2
Avg Total/2
OUTPUT “The Total is”, Total
OUTPUT “The Average is”, Avg
ENDPROCEDURE

DECLARE Suject1, Subject2:INTEGER


OUTPUT “Enter Subject 1”
INPUT Subject1
OUTPUT “Enter Subject 2”
INPUT Subject2
CALL CalTotAvg (Subjet1,Subject2)
FUNCTIONS:
Functions operate in a similar way to procedures, except that in
addition they return a single value to the point at which they are called.
Their definition includes the data type of the value returned.

A function with no parameter is defined as follows:

FUNCTION<identifier> RETURNS <data type>


<statement(s)>
RETURNS <identifier>
ENDFUNCTION
 The keyword RETURN is used as one of the statements within the body of the function to
specify the value to be returned. Normally, this will be the last statement in the function
definition.

A function with parameters is defined as follows:

FUNCTION<identifier>(parameterlist : Data type) RETURNS <data type>


<statement(s)>
RETURNS <identifier>
ENDFUNCTION

CALLING A FUNCTION:
<identifier> <Function Name>(value1,value2…..)

 Because a function returns a value that is used when the function is called, function calls are not
complete program statements. The keyword CALL should not be used when calling a function.
Functions should only be called as part of an expression.
Questions
1. Write a pseudocode to enter 2 subject marks in main program and pass them into CalculateAvg()
function to calculate the average and output the average within the main program.

FUNCTION CalculateAvg (Mark1, Mark2: INTEGER) RETURNS REAL


DECLARE Avg: Real
Average (Mark1+Mark2)/2
RETURN Average
ENDFUNCTION

DECLARE Suject1, Subject2:INTEGER


DECLARE SubjectAverage:REAL
OUTPUT “Enter Subject 1”
INPUT Subject1
OUTPUT “Enter Subject 2”
INPUT Subject2
SubjectAverage CalculateAvg (Suject1, Subject2)
OUTPUT “The Average is”, SubjectAverage

You might also like