Thursday, April 15, 2010

VB Scripting - Functions and Procedures

Previous11121314151617181920Next

Functions and Procedures in VB Script are used to make the task very easy for the user.

Procedure is a named block of code used for performing a specific task. Procedures help us to break a complex architecture into reusable blocks and yields modularity.

Syntax

Function [name](arguments)
Code
Code
End Function

Functions in VB Script

Functions helps us to avoid using the same piece of code at different places making the code bulky instead putting this piece of code in a function and calling it would solve all the issues and also it maintainable.

Functions are of two types.

1. In built Functions
2. User Defined Functions


In built Functions are those which are already loaded in the VB Script Library while User Defined Functions are those which the user defines for specific task.

User Defined Function Example


Function Multiply(byVal Int1, byVal Int2)
Prod = Int1*Int2
End Function

Multiply 3,4


When this script is run the function returns a value of 12. The Variable Multiply is known as Function Name, byVal is known as Passby, Int1 and Int2 are called as arguments. The code within the Function name and End Function is called the Function Body. End Function marks the end of the Function and the whole piece of code is known as Function Definition.

Generally a function is written to perform a single function i.e. one specific task at a time. There is a difference between a Function or Procedure and a sub procedure. a function or procedure returns a value to the calling function while a sub procedure does not return a value. To put in general

A procedure returning a value is called function procedure.

A procedure which does not return a value is called Sub Procedure.

Previous11121314151617181920Next

No comments:

Post a Comment