VBScript Functions and Procedures

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 9, 2024

Introduction to VBScript Procedures and Functions: VBScript Tutorial #6

In my previous tutorial, I talked about ‘Loops’ in the VBScript. In this tutorial, I will be covering Procedures and Functions that are used in the VBScript. This tutorial is part of our VBScript Training tutorial series.

The Procedures and Functions that are used in the VBScript provide the reusability of the code and thus, they form the basis of writing code in real scenarios. Hence a clear understanding of it is required for better programming experiences.

This tutorial gives you a complete overview of the Procedures & Functions in VBScript and its types along with simple examples for your clear understanding.

Let’s see implementation of Inbuilt Functions

Overview of Functions And Procedures

When a need arises to accomplish a particular task then a piece of code can be written including several statements under a named section inside a program which is known as Function/Procedure in terms of programming language.

Functions and Procedures are mainly used to provide assistance for arranging the code in a program in an organizable way. Using Function/Procedure, the same code can be used multiple times by calling them and this reduces the pain of writing the same code again and again at the Script level.

This is a good programming practice to follow some guidelines while creating the scripts and by working with the functions and procedures you can ensure this to happen. Instead of writing the code in a linear manner in the scripts, it is advisable to divide the scripts into Procedures to provide better readability and understanding of the Scripts.

Dividing the scripts is an important part while dealing with the creation of frameworks in QTP/UFT. As they help in making the code manageable, it is easy to debug and less complex to use.

This is just an overview of Functions and Procedures.

Let’s move on to the next topics to gain more knowledge about Functions and Procedures.

Types of Procedures in the VBScript

Basically, there are two different types of Procedures in the VBScript.

  • Sub Procedures
  • Function Procedures

Both are the same in usage as both work for providing reusability of code but with few differences, let’s discuss each of them in detail along with some examples.

#1) Sub Procedures

This is a type of procedure that includes a set of statements inside the block of code and after execution, it does not return any value.

This can be defined by making use of keywords like ‘Sub’ and ‘End Sub’ respectively. It may or may not take input.

This can be parameterized i.e. can take arguments inside the brackets if required.

Below is an example to show the usage of Sub Procedure:

<html>
<head>
<title>Let’s see implementation of Sub Procedure</title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
Sub Returnvalue()
 Msgbox “Let’s get back to work!!”
End Sub
Call Returnvalue()
</script>
</body>
</html>

The output is: Let’s get back to work!!

In the above example, Sub Procedure is created without arguments and is called in a program using the ‘Call’ keyword following the name of the Sub procedure. If you do not want to use the ‘Call’ keyword while calling a Sub procedure then you can simply call it by just writing the name of the Sub Procedure without making use of the Call keyword.

Defining Sub Procedure

Next, let’s see about Function Procedures.

#2) Function Procedures

This is a type of procedure that includes a set of statements inside the block of the code and after execution may return value also. This can take input if required, depending upon the situation.

This can be defined by making use of keywords like ‘Function’ and ‘End Function’ respectively.

Function Procedures when used with arguments i.e. by taking parameters inside the brackets, will return a value in such a case.

Below is an example to show the usage of Function Procedure:

<html>
<head>
<title>Let’s see implementation of Function Procedure</title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
Function addition(a,b)
 Dim result
 result = a + b
 addition = result ‘storing value of result in the name of the function
End Function
Dim a, b, output
a= 10
b=20
output = addition(10,20)
Msgbox(output)
</script>
</body>
</html>

The Output is: 30

In the above example, Function Procedure is created using arguments and is called in a program by using the name of the Function procedure. This is the case when the value is returned. Value of a resultant from the function is stored in another variable ‘output’ and its value is displayed in the message box.

Function Procedure without argument works in the same way as shown in the Example of Sub Procedure.

Function Procedure

Let’s summarize a few basic differences between these 2 types of procedures.

Difference Between Sub Procedure and Function Procedure

The Differences include:

  • Sub Procedure never takes an input while Function Procedure may take input if required.
  • Sub Procedure starts and ends using Sub and End Sub respectively while Function Procedure starts and ends with Function and End Function respectively.
  • The most important difference is Sub Procedure never returns a value while the Function Procedure may return a value (as shown above).

Ways to Pass a Value in the Function

There are 2 ways to pass a value in the function.

They are:

#1) Pass by Value:

While passing an argument, the Changes that take place in the called procedure and if the value of a variable does not remain to persist, then it means it is passing by value.

The keyword used in this case is ByVal.

Let’s understand this with the help of a simple Example:

<html>
<head>
<title>Let’s see implementation of Pass by Value </title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
Function output1(ByVal a)
 Dim result
 result = a / 5
 output1 = result ‘storing value of result in the name of the function
End Function
Dim a, val
a= 10
val = output1(10)
Msgbox(val)
</script>
</body>
</html>

Outputis: 10

Pass By Value

#2) Pass by Reference:

While passing an argument, the Changes that take place in the called procedure and if the value of a variable remains to persist then it means it is passing by reference.

The keyword used in this case is ByRef.

Let’s understand this with the help of a simple Example:

<html>
<head>
<title>Let’s see implementation of Pass by Reference</title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
Function output1(ByRef a)
 Dim result
 result = a / 5
 output1 = result ‘storing value of result in the name of the function
End Function
Dim a, val
a= 10
val = output1(10)
Msgbox(val)
</script>
</body>
</html>

Output is: 2

Pass By Reference

There are several types of In-Built Functions in the VBScript.

Let’s have a look at them in detail.

Inbuilt Functions in the VBScript

Some important inbuilt functions that can be used directly in the script are explained below in detail.

#1) Format Functions

There are various inbuilt functions available for formatting the expression and they can be used directly in the script without any need for writing the code explicitly before using them in the script.

  • FormatCurrency: This is used to convert a specified expression into the form of a Currency value.
  • FormatDateTime: This is used to convert a specified expression in the form of a Date and Time value.
  • FormatNumber: This is the most important and widely used Format Function and is used to convert the specified expression into the form of a Number.
  • FormatPercent: This is used to convert the specified expression into the form of a Percentage value.

#2) Math and Conversion Functions

There are various inbuilt functions for performing mathematical operations and conversion purposes and can be used directly in the script without any need of writing the code explicitly before using them in the script.

  • Abs: This is the frequently used Math function for the purpose of returning the Absolute value for a related number which is specified as a parameter.
  • Int: This is Math Function and is used in those cases where it is required to fetch an integer part from a given expression/ number.
  • cDate: This is one of the frequently used Conversion Functions for converting the expression which includes the Date or Time parameters into the Date subtype.
  • cStr: This is the Conversion function that is used for converting the expression into String subtype.

Let’s see a simple example using these inbuilt functions

<html>
<head>
<title>Let’s see implementation of Inbuilt Functions</title>
</head>
<body>
<script language=”vbscript” type=”text/vbscript”>
Dim a , b 
a = Abs (-10)
b = CStr(#10-10-17#)
Msgbox(a)
Msgbox(b)
</script>
</body>
</html>

Output is:

10

10/10/2017

Conversion Function

Conclusion

I hope that this tutorial would have provided insight regarding the importance and effectiveness of these reusable statements and this, in turn, will help you in proceeding with subsequent tutorials in an easy manner.

Next Tutorial #7: I will cover about ‘VBScript Arrays’ in my next tutorial.

Feel free to share your thoughts and suggestions about this tutorial.

Was this helpful?

Thanks for your feedback!

Leave a Comment