Professional Documents
Culture Documents
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
• 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
a. Sub procedure
b. Function
1. Function
a. Performs Calculation
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
• SubName - name used to refer to Sub - rules for naming Sub Procedures are the same
as for variables
Procedure call
• Example:
Example
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.
Declaration syntax
The syntax for declaring a Function procedure is as follows:
• 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.
lvalue = functionname [( argumentlist )]
or
functionname [( argumentlist )]
Example:
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.
Output:
Before swap 10 20
After Swap 10 20
Output:
Before swap 10 20
After Swap 20 10