Procedures are chunks of code that are called upon by name.

## Procedure Scope

There are three scopes that can be declared for procedures:

• Private. Private Procedures can only be called by other procedures within the module.
• Public. Public Procedures in form (.frm) or class (.cls) modules become methods of the module/class and can be called from anywhere in the application by specifying procedure name qualified with the module name. Public Procedures in standard (.bas) modules can be called by specifying the procedure name, in which case the name must be unique across the project.
• Friend. Friend Procedures can be used in form (.frm) or class (.cls) modules but not in standard (.bas) modules. The Friend keyword makes the module available throughout the project but not throughout the application (i.e. it is between Private and Public).

The Static keyword can be used when when declaring a procedure. It make the procedure remember the value of its local variables between calls. This is pretty stupid, because you might as well use a module or public level variable instead.

## Procedure Arguments

[Optional] [ByVal | ByRef] [ParamArray] VarName[( )] [As type] [= DefaultValue]

The arguments/parameters that a procedure can receive are set by listing them when the procedure is declared.

• By default the data type of an argument is Variant. The As keyword can be used to set the data type or object type.
• By default, arguments are passed in ByRef, i.e. the argument is tied to the address of the variable passed and changes to the argument occur to the original variable. The ByVal keyword can be used so just the value of a variable can be passed to the procedure and changes to the argument do not occur to the original variable.
• A default value for an argument can also be provided.
• An argument can be specified as optional by preceding it with the Optional keyword. Once the Optional keyword is used, all the arguments that follow must also be optional.
• The ParamArray keyword can be used to accept arguments as an array of indeterminate length.

EG:

Sub ProcedureName (M, _
N As String, _
ByVal O, _
P = 12, _
Optional Q, _
Optional ParamArray R())

## Procedure Calls

When called, parameters are usually passed to the procedure in the same order as the arguments were listed at the procedure declaration.

• The Call keyword is optional.
• If the procedure requires arguments and Call is used, then the arguments must be in parentheses.
• If the procedure requires arguments and Call is not used, then parentheses must not be used.
• If the procedure does not require arguments, then the parentheses may or may not be used.
• Optionally, parameters may be entered in any order with this syntax: ArgumentName:=Value (the argument name, a colon, an equal sign, and the value). This is a somewhat silly feature since it is not supported by all procedures, methods, etc., including methods for VB objects.

EG:

ProcedureName myVariable, True, "Hello world!", 13
Call ProcedureName (M:=myVariable, O:="Hello world!", N:=True, P:=13)

There are two ways arguments are passed to procedures:

• ByRef. ByRef (the default) means that the procedure can change the value of the argument passed to it.
• ByVal. ByVal means that the procedure cannot change the value of the argument passed to it.

## Sub Procedures

[Private|Public|Friend] [Static] Sub SubProcedureName ([Arguments])
[Statements]
End Sub

Sub Procedures can modify arguments passed to it but cannot return a value itself.

• By default, Sub Procedures are Public in all modules.
• A Sub Procedure is a stand-alone statement and cannot be called from within an expression.
• Public Sub Procedures from other modules can be accessed by qualifying the function with the module name. Sub Procedures are called in one of two ways:
Call SubProcedureName ([Arguments])
SubProcedureName [Arguments]

Sub Procedures come in one of two types:

1. Event Procedures are actions in response to events, such as clicking on a control. Syntax:
[Private|Public|Friend] [Static] Sub Object_Event ([Arguments])
[Statements]
End Sub
1. General Procedures are Sub Procedures which are not Event Procedures but must be explicitly evoked. Syntax:
[Private|Public|Friend] [Static] Sub SubProcedureName ([Arguments])
[Statements]
End Sub

## Function Procedures

[Private|Public|Friend] [Static] Function FunctionName([Arguments])[ As Type]
[Statements]
End Function

Function Procedures can modify arguments passed to it and can return values.

• You can use built-in (intrinsic) Functions or make your own Function Procedures.
• A Function Procedure returns a value by assigning a value to the procedure name itself at some point during the procedure.
• A Function Procedure has a data type or object type since it returns a value. Syntax:

A Function Procedure is called just like a Sub Procedure, but it can also be called by using its name in an expression.

[FirstPartOfExpression1]FunctionName([Arguments])[SecondPartOfExpression]

## Property Procedures

Property Procedures are procedures basically used to set properties for a class of objects. See VB OOP.

Page Modified: (Hand noted: ) (Auto noted: )