Procedures and Functions
Procedures and Functions
4 Program Modules
• This refers to a modular program design
• Subroutines: self-contained section of code, performing a specific task; part
of the main program
Subroutine / Module
Procedure Function
Example
• Calling a procedure:
CALL Procedure Name (Parameters)
Example
CALL Area Rectangle (Area, Length, width)
3.7 Function subroutine that performs a specific task and returns a single
value by its Function Name.
RETURNS result
<statement(s)>
END FUNCTION
In pseudocode, a function definition is written as:
RETURN <value>
ENDFUNCTION
Example 1
Function area (ByVal L As Integer) As Integer
Area = L * L
End Function
area = x * y
'Return a
End Function
Sub Main()
Console.Write("Enter lenght ")
l = Console.ReadLine
Console.ReadLine()
End Sub
End Module
Example 3
Module Module1
Dim l, w As Integer
Dim a As Double
End Sub
Sub Main()
Console.Write("Enter lenght ")
l = Console.ReadLine
End Sub
End Module
a=l*l
End Function
Sub Main()
Dim l As Integer
l=4
Console.ReadLine()
End Sub
Dim l, w As Integer
Dim a As Double
End Sub
Return l * w
End Function
Sub Main()
Call areaPro(l, w, a)
End Sub
End Module
Sub Main()
Console.Write("Enter lenght ")
l = Console.ReadLine
End Sub
End Module
ALTERNATIVE
Module Module1
Dim l, w, H As Integer
Dim V As Double
Sub Main()
Console.Write("Enter lenght ")
l = Console.ReadLine
End Sub
End Module
Module Module1
v=l*w*h
End Sub
End Function
Sub Main()
Dim l, w, h, v As Integer
l=1
w=2
h=3
v=0
Call volume(l, w, h, v)
Console.ReadLine()
End Sub
End Module
Module Module1
Sub Main()
Dim text, REVERSE, surname, othername As String
Dim x, pos As Integer
x = Len(text)
For i = 1 To x
If Mid(text, i, 1) = " " Then
pos = i
End If
Next
End Sub
End Module
Module Module1
Dim num1 As Integer
Dim num2 As Integer
Dim answer As Integer
Sub input_sub()
Console.Clear()
Console.WriteLine("Enter number 1")
num1 = Console.ReadLine
Console.WriteLine("Enter number 2")
num2 = Console.ReadLine
End Sub
Sub Calculation()
answer = num1 * num2
End Sub
Sub output_sub()
Console.Write("the product of " & num1 & " and " & num2 & " is ")
Console.WriteLine(answer)
Console.ReadLine()
End Sub
Sub Main()
input_sub()
Calculation()
output_sub()
End Sub
End Module
JUNE 2016 PAPER 2-1
NOVEMBER 2016 PAPER 2-1
NOVEMBER 2016 PAPER 2-2
JUNE 2017 PAPER 21
JUNE 2017 PAPER 21
JUNE 2017 PAPER 22
NOVEMBER 2017 PAPER 21
NOVEMBER 2017 PAPER 21
NOVEMBER 2018 PAPER 21
NOVEMBER 2018 PAPER 22
NOVEMBER 2018 PAPER 23
JUNE 2019 PAPER 21
ANSWER
JUNE 2019 PAPER 22
ANSWER