VB Procedures and Functions

Introduction
Visual Basic applies the concept of modular programming, this is where a program is divided into smaller, simple and manageable parts. In Visual basic, this is achieved through the use of procedures and functions.

Procedures
A procedure is a sub program (subroutine) that is designed to perform a specific task. It is a complete program on its own. Procedures make otherwise complex programs to be broken down into smaller manageable parts (modules) making it easy to write and debug a program.

The are two types of procedures: General procedures and Event procedures.

General Procedures
General procedures do not have events attached to them. Syntax: access specifier sub procedurename statements End sub Example: private sub oNames print "Jane", "John", "Peter': end sub A general procedure is activated by a call statement. Example: private sub oNames  print "Jane", "John", "Peter': end sub private sub cmdShow_click call oNames end sub

Event Procedures
This is a sub routine that has an event attached to it. The event can be triggered by a user action or a system action.

Examples of events: Syntax: access specifier sub [object|controlname]_Event statements end sub Example of event procedures: private sub cmdCalculate_KeyPress(keyASCII As integer) print "Hallo world": end sub
 * 1) click : This is activated when a mouse is clicked.
 * 2) mousemove: Activated when a moue moves.
 * 3) dblclick :Triggered when a mouse is double clicked.
 * 4) mouseover : Triggred when a mouse passes over an object.
 * 5) keypress :Trigerred when a key is pressed and released.
 * 6) keydown : Triggered when a key is pressed and held down.

Visual Basic Functions
It is a sub-program designed to solve a given task and return a value. There are two types of functions; Builtin and User defined.

Built in Functions
These are functions found in the Visual Basic programming (standard)library. Some examples are: In writing games and learning software, the Rnd function is used to introduce randomness. This ensures different results each time you try a program. The Visual Basic function Rnd returns a single precision, random number between 0 and 1 (actually greater than or equal to 0 and less than 1). In order to produce random integers (I) between Imin and Imax, we can apply the formula: I = Int((Imax - Imin + 1) * Rnd) + Imin The Visual Basic random number generator must be seeded. The seed value initializes the generator. The Randomize statement is used in this case e.g: Randomize Seed

User Defined Functions
These are functions whose name is supplied by the user. The function name should follow the rules of naming identifiers. Syntax: function functionname(parameters as datatype) return type statements functionname = value end function The function declaration must contain the key word function and ends with end function. The functionname must be assigned a value within the function itself.
 * 1) Should not be a key word.
 * 2) Must start with a letter.
 * 3) should not contain spaces (space characters).
 * 4) Should not contain special symbols apart from the underscore.

Below is another example of a function syntax: access specifier function functionname(parameters as datatype) return type statements functionname = value 'Assigning a value to the function end function Access specifier specifies the scope of access e.g local [private] or global[public].

Example 1: function volumeCylinder (ByVal p, r, h As Single) As Single volumeCylinder = p*r^2*h end function

Example 2: private function volumeCylinder (ByVal p, r, h As Single) As Single volumeCylinder = p*r^2*h end function private sub cmdCalculate_Click Const pie = 3.1415 Dim radius, heit, Area As Single Area = volumeCylinder(pie,radius,heit) 'function call and passing of parameters end sub