You are on page 1of 6

Procedures and functions

Procedures
• A set or block of code statements that is given a name so that it can be invoked by another
part of the program

• Used as Building blocks of applications

• Called as Modular Programming - Break large problems down into smaller ones

• Used for Repeated or Shared Tasks - Container for code to be called multiple times

• Makes the code easier to understand

Two types of Procedures in visual basic

a. Sub procedure

i. (Event) Sub Procedure

1. Performs a logical action

2. Does not return a value

3. Executes when event occurs - not “Called” from code

ii. (Gen Purpose) Sub Procedure

1. Performs a logical action

2. Does not return a value

3. Must be “Called” from other code

b. Function

1. Function

a. Performs Calculation

b. Does return a Value

c. Must be “Called” from other code

Sub procedure
• Sub procedures are written to perform specific tasks
• General purpose Sub procedures are not triggered by events but called from
statements in some other location

• Event procedures are special Sub procedures that are not called from statements,
but rather are executed when a corresponding event occurs

Syntax

[Modifiers] sub subname [(ParameterList)]


[Statements]
End Sub

• Modifiers (optional) - the AccessSpecifier - establishes accessibility to the program. i.e


either private or public. By default it is public.

• Sub and End Sub- keywords

• SubName - name used to refer to Sub - rules for naming Sub Procedures are the same
as for variables

• ParameterList - a list of variables or values being passed to the sub procedure

Procedure call

• Used to invoke (cause to execute) a procedure

• Syntax: Call ProcedureName (Arguments)

• Call (optional) - keyword

• ProcedureName - name of Sub (or Function) to invoke

• Arguments (if any, included in parenthesis) - Data passed to the procedure

• Example:

• Call Power (5, 2)

Example

Private Sub Command1_Click()


Dim a As Integer
Dim b As Integer
a = 10
b = 20
Call sum(a, b)
End Sub
Sub sum(a As Integer, b As Integer)
Dim c As Integer
c=a+b
MsgBox "sum=" & c
End Sub

Function
A Function procedure is a series of Visual Basic statements
enclosed by the Function and End Function statements.
The Function procedure performs a task and then returns control
to the calling code. When it returns control, it also returns a
value to the calling code.

It is Public by default, which means you can call it from


anywhere in your application in which you defined it.

A Function procedure can take arguments, such as


constants, variables, or expressions, which are passed to
it by the calling code.

Declaration syntax
The syntax for declaring a Function procedure is as follows:

[Modifiers] Function FunctionName [(ParameterList)] As ReturnType


[Statements]
Functionname=value
End Function

• Modifiers (optional) - the AccessSpecifier - establishes accessibility to the program. i.e


either private or public. By default it is public.

• Function and End Function- keywords


• FunctionName - name used to refer to the function - rules for naming function are the
same as for variables

• ParameterList - a list of variables or values being passed to the function

• ReturnType – determines the data type of the value the function returns to the calling
code.

• It assigns a value to its own function name in one or more statements of the function.
Control does not return to the calling program until an Exit Function or End Function

Calling syntax
You invoke a Function procedure by including its name and arguments either on the
right side of an assignment statement or in an expression.

The syntax for a call to a Function procedure is as follows.

lvalue = functionname [( argumentlist )]

or

functionname [( argumentlist )]

Example:

Private Sub Command1_Click()


Dim a As Integer
Dim b As Integer
a = 10
b = 20
MsgBox sum(a, b)
End Sub
Function sum(a As Integer, b As Integer) as Integer
sum = a + b
End Function

Passing parameters by value, by reference


Visual Basic supports two ways of passing parameters to functions. By value
and by reference. For this, we have two keywords. ByVal and ByRef.

Pass by value

When we pass arguments by value, the function works only with the copies of
the values. This may lead to performance overheads, when we work with large
amounts of data.

Private Sub Command1_Click()


Dim a As Byte
Dim b As Byte
a = 10
b = 20
Print "Before swap" & a & " " & b
Call Swap(a, b)
Print "After Swap" & a & " " & b
End Sub
Sub Swap(ByVal a As Byte, ByVal b As Byte)
Dim temp As Byte
temp = a
a=b
b = temp
End Sub

Output:
Before swap 10 20
After Swap 10 20

Pass by Reference (default in vb)

When we pass values by reference, the function receives a reference to the


actual values. The original values are affected, when modified. This way of
passing values is more time and space efficient. On the other hand, it is more
error prone.

Private Sub Command1_Click()


Dim a As Byte
Dim b As Byte
a = 10
b = 20
Print "Before swap" & a & " " & b
Call Swap(a, b)
Print "After Swap" & a & " " & b
End Sub
Sub Swap(ByRef a As Byte, ByRef b As Byte)
Dim temp As Byte
temp = a
a=b
b = temp
End Sub

Output:
Before swap 10 20
After Swap 20 10

You might also like