0% found this document useful (0 votes)
7 views

Procedures and Functions

Uploaded by

Anaïs Ernestine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Procedures and Functions

Uploaded by

Anaïs Ernestine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

1.

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

• Procedures: performs a specific task, no value returned to part of code where


called

• Functions: performs a specific task, returns a value to part of code where


called

Benefits of Procedures and Functions:


• Lines of code can be re-used; don’t have to be repeated
• Can be tested/improved independently of program
• Easy to share procedures/functions with other programs
• Create routines that can be called like built-in command
Procedure
Procedure: subroutine that performs a specific task may return no value or one
value or many values
• Procedure without parameters:
PROCEDURE procedure name ( parameter )
<statement(s)>
END PROCEDURE
SUB Procedure Name ( )
statement(s)
END SUB
• When a procedure has a parameter, the procedure can either pass it by either
reference or value
• Pass by value: data copied into procedure so variable not changed outside
procedure
PROCEDURE Procedure Name (BYVALUE <parameters>: <datatype>)
<statement(s)>
ENDPROCEDURE

SUB Area ( ByVal Length : Integer , ByVal width : Integer )


Area = Length * Width
END SUB

• Pass by reference: link to variable provided so variable changed after going


through procedure
PROCEDURE Procedure Name (BYREF <param>: <datatype>)
<statement(s)>
ENDPROCEDURE

Example

SUB AreaRectangle (BYRef Area: integer, ByVal Length: Integer, By Val


width : Integer )
Area = Length * Width
END SUB

• 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.

FUNCTION Function Name (BYval <parameter>: <data type> ) as Datatype

RETURNS result
<statement(s)>

END FUNCTION
In pseudocode, a function definition is written as:

FUNCTION <functionIdentifier> RETURNS <dataType> // function header

<statement(s)> // function body

RETURN <value>

ENDFUNCTION

Example 1
Function area (ByVal L As Integer) As Integer

Area = L * L

End Function

Calling the Function


Area(L)
Example 2
Module Module1
Dim l, w As Integer
Dim a As Double

Function area(ByVal x As Integer, ByVal y As Integer) As Double

area = x * y
'Return a

End Function

Sub Main()
Console.Write("Enter lenght ")
l = Console.ReadLine

Console.Write("Enter width ")


w = Console.ReadLine
Console.WriteLine("Area = " & area(l, w))

Console.ReadLine()

End Sub

End Module

Example 3
Module Module1
Dim l, w As Integer
Dim a As Double

Sub area(ByRef x As Integer, ByRef y As Integer, ByRef a As Double)


x = l + 2
y = w + 3
Console.WriteLine("inside x = " & x)
Console.WriteLine("inside y = " & y)
a = x * y

End Sub

Sub Main()
Console.Write("Enter lenght ")
l = Console.ReadLine

Console.Write("Enter width ")


w = Console.ReadLine
Call area(l, w, a)
Console.WriteLine("After l = " & l)
Console.WriteLine("After w = " & w)
Console.WriteLine("Area = " & a)
Console.ReadLine()

End Sub

End Module

Function a(ByVal l As Integer) As Integer

a=l*l

End Function

Sub Main()

Dim l As Integer

l=4

Console.WriteLine(" Area = " & a(l))

Console.ReadLine()

End Sub

Area of Rectangle using Procedure and Functions


Module Module1

Dim l, w As Integer
Dim a As Double

Sub areaPro(ByVal l As Integer, ByVal w As Integer, ByRef a As Integer)

' Dim a As Integer


a = l * w

End Sub

Function areaFunc(ByVal l As Integer, ByVal w As Integer) As Double

Return l * w

End Function
Sub Main()

Console.Write("Enter lenght ")


l = Console.ReadLine

Console.Write("Enter width ")


w = Console.ReadLine
a = 0

Call areaPro(l, w, a)

Console.WriteLine("using procedure Area = " & a)


Console.WriteLine("using Function Area = " & areaFunc(l, w))
Console.ReadLine()

End Sub

End Module

Volume of cuboid using Procedure and Functions


Module Module1
Dim l, w, H As Integer
Dim a As Double

Sub areaPro(ByVal l As Integer, ByVal w As Integer, ByRef a As Integer)


a = l * w
End Sub

Function areaFunc(ByVal l As Integer, ByVal w As Integer) As Double


Return l * w
End Function

Sub Main()
Console.Write("Enter lenght ")
l = Console.ReadLine

Console.Write("Enter width ")


w = Console.ReadLine

Console.Write("Enter Height ")


H = Console.ReadLine
a = 0
Call areaPro(l, w, a)
Console.WriteLine("using procedure VOLUME = " & a * H)
Console.WriteLine("using Function VOLUME = " & areaFunc(l, w) * H)
Console.ReadLine()

End Sub

End Module
ALTERNATIVE
Module Module1
Dim l, w, H As Integer
Dim V As Double

Sub VOLPro(ByVal l As Integer, ByVal w As Integer, ByVal H As Integer, ByRef V As


Integer)
V = l * w * H
End Sub

Function VOLFunc(ByVal l As Integer, ByVal w As Integer, ByVal H As Integer) As


Double
Return l * w * H
End Function

Sub Main()
Console.Write("Enter lenght ")
l = Console.ReadLine

Console.Write("Enter width ")


w = Console.ReadLine

Console.Write("Enter Height ")


H = Console.ReadLine
V = 0
Call VOLPro(l, w, H, V)
Console.WriteLine("using procedure VOLUME = " & V)
Console.WriteLine("using Function VOLUME = " & VOLFunc(l, w, H))
Console.ReadLine()

End Sub

End Module

Module Module1

Sub volume(ByVal l As Integer, ByVal w As Integer, ByVal h As Integer, ByRef v As


Integer)

v=l*w*h

End Sub

Function vol(ByVal l As Integer, ByVal w As Integer, ByVal h As Integer) As Integer


Return l * w * h

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.WriteLine(" Volume using procedure = " & v)

Console.WriteLine(" Volume using function = " & vol(l, w, h))

Console.ReadLine()

End Sub

End Module

Module Module1

Sub Main()
Dim text, REVERSE, surname, othername As String
Dim x, pos As Integer

Console.WriteLine("Enter a text ")


text = Console.ReadLine

x = Len(text)

For i = 1 To x
If Mid(text, i, 1) = " " Then
pos = i
End If

Next

surname = Left(text, pos - 1)


othername = Right(text, x - pos)
Console.WriteLine("surname = " & surname)
Console.WriteLine("othername = " & othername)
Console.ReadLine()

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

JUNE 2019 PAPER 22


ANSWER
ANSWER

You might also like