Procedures and Functions
Subroutine
A subroutine is a self-contained block of code that has an identifier name and performs
a specific task. A set of programming statements grouped together under a single
name that can be called to perform a task.
(a subroutine can be either a procedure or a function).
Procedure
A procedure is a subroutine ( A set of programming statements grouped together under
a single name that can be called to perform a task.)
A procedure that can receive values from another program (or another subroutine) and
can return none, one or many values back to that program.
Function
A function is a subroutine( A set of programming statements grouped together under a
single name that can be called to perform a task.) that performs a specific task and
returns a single value to the part of the program that called it.
Argument
When a main program calls a subroutine, the values being passed to the subroutine are
called arguments of the calling procedure.
Parameter
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.
In a subroutine the values passed to it from the main program are called parameters.
You can define a procedure with no parameters, one parameter, or more than one.
Pseudocode
DECLARING A PROCEDURE
The procedure is declared by using the keyword PROCEDURE followed by the name of the
procedure. Program statements are inserted between PROCEDURE….and
ENDPROCEDURE.
Here is the syntax for declaring a procedure in pseudocode:
PROCEDURE <ProcedureName()
<program statements>
ENDPROCEDURE
In this case no values are being passed when ProcedureName() is called. You shall see
later how values can be passed to the procedure being called.
1
PASSING ARGUMENTS TO A SUBROUTINE BY REFERENCE
Arguments can be passed to a subroutine by reference (ByRef). The arguments of
arguments to be passed by reference must be variables. A pointer to the memory
location of that variable is passed into the subroutine being called. Changes to the
variable value can be changed by the subroutine being called.
Example
Sub ComputeArea(ByRef Length As Integer, ByRef Width As Integer)
In the calling subroutine the Length and Width must be variables and not values.
SUBROUTINE MAIN()
Every Visual Basic application must contain a procedure called Main(). This procedure
serves as the starting point and overall control for you. The Main() procedure is called
when the application is loaded and control is passed to it. Unless you are creating a
Windows Forms application, you must write the Main procedure for applications that
run on their own.
Whenever you start a project in VB.Net the procedure Main() is loaded first. All other
subroutines are called from this procedure Main().
Requirements for the Main Procedure
A file that runs on its own (usually with extension .exe) must contain a Main() procedure.
A library (for example with extension .dll) does not run on its own and does not require a
Main() procedure. The requirements for the different types of projects you can create
are as follows:
• Console applications run on their own, and you must supply at least one Main()
procedure.
• Windows Forms applications run on their own. However, the Visual Basic
compiler automatically generates a Main() procedure in such an application, and
you do not need to write one.
In the above example there are three subroutines in this Module namely, Main(),
ComputeArea(), ComputeVolume() and MySounds().
The Main() subroutine calls the subroutine ComputeArea(30,20). The values in
parentheses (30,20) are the arguments that are passed to the subroutine
ComputeArea(Length, Width). In the subroutine ComputeArea (Length, Width) are the
parameters of the subroutine.
The Main subroutines also calls the subroutine ComputeVolume(30,20,15). The
values in parentheses (30,20,15) are the arguments that are passed to the
ComputeVolume(Length, Width, Height) subroutine. In the subroutine
ComputeVolume the values (Length, Width and Height) are the parameters.
2
Main also calls the subroutine MySounds() but does not pass any arguments to it as it
has no parameters.
Arguments
An argument represents the value that you pass to a procedure parameter when you call
the procedure. The calling code supplies the arguments when it calls the procedure.
When you call a Function or Sub procedure/subroutine, you include an argument list in
parentheses immediately following the procedure name. Each argument corresponds
to the parameter in the same position in the list.
In contrast to parameter definition, arguments do not have names. Each argument is an
expression, which can contain zero or more variables or constants. The data type of the
evaluated expression should typically match the data type defined for the
corresponding parameter, and in any case it must be convertible to the parameter type.
Parameters
A parameter represents a value that the subroutine expects you to pass to it when you
call it. The subroutine’s declaration defines its parameters.
When you define a Function or Sub procedure, you specify a parameter list in
parentheses immediately following the procedure name. For each parameter, you
specify a name, a data type, and a passing mechanism (ByVal ) or ByRef . You can also
indicate that a parameter is optional. This means that the calling code does not have to
pass a value for it.
ByVal
Specifies that the procedure cannot replace or reassign the variable element
underlying the corresponding argument in the calling code.
The advantage of passing an argument ByVal is that it protects a variable from
being changed by the procedure. We usually use the ByVal part of the
declaration.
ByRef
Specifies that the procedure can modify the underlying variable element in the
calling code the same way the calling code itself can.
The advantage of passing an argument ByRef is that the procedure can return a
value to the calling code through that argument. We will rarely use this.
3
CALLING A SUBROUTINE/PROCEDURE
A subroutine or procedure can be called from another procedure or subroutine with the
keyword CALL. CALL is followed immediately by the name of the procedure/subroutine
being called. When a subroutine or procedure is called, control is handed over to it.
When the called subroutine/procedure is done control is handed back to the calling
procedure.
e.g. the syntax for calling a subroutine in pseudocode is:
CALL <SubroutineName>()
e.g.
CALL ComputeArea() will call a subroutine named ComputeArea()
CALL ComputeVolume() will call the subroutine named ComputeVolume()
Please note that in both procedures above, no values are being passed by the calling
procedure.
PROCEDURE Stars
OUTPUT"************"
ENDPROCEDURE
Calling procedure
CALL Stars
Procedures with parameters
Here is an example of how a procedure with parameters can be defined
in
Pseudocode:
We can add parameters to a procedure:
PROCEDURE Stars (Number : INTEGER)
DECLARE Counter : INTEGER
FOR Counter ← 1 TO NUMBER
OUTPUT "*"
NEXT Counter
ENDPROCEDURE
Procedure with parameters are called like this – in this case to
print seven stars:
CALL Stars (7)
Or:
MyNumber ← 7
CALL stars (MyNumber)
4
The main subroutine (Main() below calls three subroutines (PROCEDURES) Mysounds,
ComputeArea and ComputeVolume. The algorithm has been written in pseudocode:
PROCEDURE Main()
CALL mySounds()
CALL ComputeArea(30, 20)
CALL ComputeVolume(30,20,15)
ENDPROCEDURE
PROCEDURE MySounds()
Beep
ENDPROCEDURE
PROCEDURE ComputeArea(BYVALUE Length: INTEGER, BYVALUE Width: INTEGER)
Area = length width
ENDPROCEDURE
PROCEDURE ComputeVolume(BYVALUE Length: INTEGER, BYVALUE Width:INTEGER,
BYVALUE Height: INTEGER)
Volume = length*width*height
ENDPROCEDURE
DECLARING A SUBROUTINE IN VISUAL BASIC
In VB.Net all procedures and subprocedures are subroutines. A subroutine is declared
using the Sub Statement. The sub statement declares the name, parameters and
code that define the sub procedure. The end of a subroutine is End Sub.
The basic syntax for declaring a subroutine in Visual Basic is:
Sub <ProcedureName>(parameterlist)
<statements>
End Sub
parameterlist is a list of the parameters of the subroutine and their data types.
It is possible to call a subroutine which has no parameters, in which case the
parenthesis following the name of the subroutine will be empty.
5
Sub Main()
Dim number As Integer = 30
Dim mytemp As Decimal = 37
Call Stars()
Call Sun(number)
Console.WriteLine(" ")
mytemp = Celsius(mytemp)
Console.WriteLine(mytemp)
Console.ReadKey()
End Sub
Sub Stars()
Console.WriteLine("************")
Console.ReadKey()
End Sub
Sub Sun(Number As Integer)
Dim Counter As Integer
For Counter = 1 To Number
Console.Write("@")
Next
End Sub
Function Celsius(ByVal Temperature As Decimal) As Decimal
Return (Temperature - 32) / 1.8
End Function
Module Module1
Sub Main()
computeArea(25, 10)
Mysounds()
End Sub
Sub computeArea(ByVal length As Double, ByVal width As Double)
Dim area As Double
Area = length * width
End Sub
Sub Mysounds()
For intCount = 1 To 3
Console.Beep()
Next
End Sub
End Module
6
How to declare a function procedure
The syntax for declaring a basic Function procedure is as follows in pseudocode is:
FUNCTION FunctionName[(parameter List)] RETURNS <Data Type>
statement 1
statement 2
statement 3
..................
RETURN value
ENDFUNCTION
For example:
FUNCTION ComputeVolume(BYVALUE Length: INTEGER, BYVALUE Width:
INTEGER, BYVALUE Height: INTEGER) RETURNS INTEGER
In the preceding algorithm we should have actually used the FUNCTION procedures to
calculate the area (or Volume) since these PROCEDURES each return only one value.
Here is the algorithm re-written but using function procedure.
PROCEDURE Main()
CALL mySounds()
CALL ComputeArea(30, 20)
OUTPUT Area
CALL ComputeVolume(30,20,15)
OUTPUT Volume
ENDPROCEDURE
FUNCTION ComputeArea(BYVALUE Length: INTEGER, BYVALUE Width: INTEGER)
RETURNS INTEGER
Area = length * width
RETURN Area
ENDFUNCTION
FUNCTION ComputeVolume(BYVALUE length: INTEGER, BYVALUE width:INTEGER,
BYVALUE
Height: INTEGER) RETURNS INTEGER
Volume = length*width*height
RETURN Volume
ENDFUNCTION
7
Visual Basic
Function ComputeArea(ByVal Length As Integer, ByVal Width As Integer) As Double
Area = Length*Width
Return Area
End Function
You can also return a value to the calling subroutine by naming the expression or value
the same name as the subroutine.
e.g.
Function ComputeArea(ByVal Length As Integer, BYVal Width As Integer) As
Integer|
Area = Length * Width
ComputeArea=Area
End Function
The End function will return Area to the calling subroutine.
Examples in Visual Basic
Module Module1
Sub Main()
Console.WriteLine("Demonstrating Subroutines")
Dim returnedValue As Double
returnedValue = computeArea(30, 20)
Console.WriteLine(returnedValue)
Console.ReadLine()
End Sub
Function computeArea(ByVal length As Double, ByVal width As Double) As
Double
computeArea = length * width
End Function
OR, using the Return statement
Sub Main()
Console.WriteLine("Demonstrating Subroutines")
Dim returnedValue As Double
returnedValue = computeArea(30, 20)
Console.WriteLine(returnedValue)
Console.ReadLine()
End Sub
Function computeArea(ByVal length As Double, ByVal width As Double) As
Double
Return length * width
End Function
8
Advantages of using subroutines
Modern Procedural languages use subroutines because:
1. Subroutines modularise a problem. The problem is broken down into small
manageable sub-problems (subroutines).
2. Each subroutine is easier to understand and debug as opposed to one long
program.
3. Each subroutine performs a specific task.
4. Different programmers can work on different subroutines at the same time to
produce an overall solution to a complex problem.
5. A subroutine can be called by different subroutines.
6. It is possible to build a library of subroutines, always available for use by
different programs.
Local and global variables
A global variable can be used by any part of a program – its scope covers the
whole program.
A local variable can only be used by the part of the program it has been declared in – its
scope is restricted to that part of the program.
For example, in this algorithm the variables Number1, Number2 and Answer are
declared both locally and globally, whereas Number3 is only declared locally
9
Module Module1
Dim area, volume As Decimal
Dim length, width, height As Decimal
Sub Main()
Console.WriteLine(" finding the area of a rectangle")
Console.WriteLine(" enter the length")
length = Console.ReadLine
Console.WriteLine(" enter the width")
width = Console.ReadLine
rectangle()
Console.WriteLine("Area is " & area)
Console.WriteLine(" finding the volume of a rectangular prism")
Console.WriteLine(" enter the length")
length = Console.ReadLine
Console.WriteLine(" enter the width")
width = Console.ReadLine
Console.WriteLine(" enter the height")
height = Console.ReadLine
prism()
Console.WriteLine("volume is " & volume)
Console.WriteLine(" finding the area of a triangle")
triangle()
Console.WriteLine("area of a triangle is " & area)
Console.ReadKey()
End Sub
Sub rectangle()
area = length * width
End Sub
Sub prism()
volume = length * width * height
End Sub
Function triangle() As Decimal
Dim base As Decimal
Console.WriteLine(" enter the height")
height = Console.ReadLine
Console.WriteLine(" enter the base")
base = Console.ReadLine
area = 0.5 * base * height
Return area
End Function
End Module
Exercise
USING BOTH PSEUDOCODE AND VISUAL BASIC
1. Write a program that will allow a user to enter two positive whole numbers. The program will then
find the product of these numbers, addition , division and the difference. The program should do
the following
a. Declare global variables of your choice of two numbers and the answer
b. Write a procedure to validate if the number is positive and a whole number.
c. Write a procedure for addition
d. Write a procedure for subtraction
e. Write a function for division
f. Write a function for multiplication.
g. Edit the main program to allow a user to enter the two numbers, validate and choose the
choice of operation.
10