• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog

VBScript Procedures

VBScript procedures are used mainly for code organization and reuse.

We have been using simple examples till now as here the purpose is learning VBScript.

But in real world scenario, the code is not as simple as that.

When you try to implement something that is useful, the code might get more complex.

There could be hundreds of lines stretching across many pages.

If you do not organize the code properly, the whole process of coding, debugging and managing the code will become really complex.

So, you should organize or modularize the code carefully so that your code becomes easily manageable.

In this tutorial, you will learn-

Moreover, suppose you have a set of statements that performs a particular action. You want the same action to be repeated several times. Then, why should you write the same code again and again? By using effective techniques, you can make your code reusable. This will help the developers to organize the code beautifully and the testers to identify bugs easily. In short, code modularization and reuse is very important for making the code more powerful, reliable, and easier to maintain.

Here come procedures into picture. A procedure is a block of code that ideally performs a single function. A block of code that processes an input or handles a file is good example of a function. There are two types of VBScript procedures: Sub procedures and Function procedures.

Sub Procedures

If you want to execute a series of statements without returning any value, then you can use sub procedures.

Sub procedures start and end with Sub and End Sub statements respectively. Sub procedures can take arguments, but cannot return a value. Sub procedures may or may not take an input.

Sub outputMessage()
document.write("Welcome")
End Sub

Just writing this code will not output anything. Here you have created a Sub procedure named outputMessage. Next you need to call it.

call outputMessage()

Combine both these sets of codes inside <script> tag like this.

<script type="text/vbscript">

Sub outputMessage()
document.write("Welcome")
End Sub
call outputMessage()
</script>

The output of this code will be

VBScript Functions & Procedures

Function Procedures

If you want to execute a series of statements and return a value, then you need to use function procedures, commonly known as function.

Function procedures start and end with Function and End Function statements respectively.

A function procedure may or may not take an input.

Function procedures return a value by assigning the value to its name.

<script type="text/vbscript">
Function findArea(radius)
   const pi=3.14
   area = pi*radius*radius
   findArea = area
End Function
document.write("The area of the circle when the radius is 20 is " & findArea(20) &"<br/>")
document.write("The area of the circle when the radius is 10 is " & findArea(10))

</script>

The output of this code will be

VBScript Functions & Procedures

ByRef and ByVal Parameters

You can pass vbscript arguments to the procedures by reference or by value.

If you do not specify anything when you call a procedure, then the argument/arguments are passed by reference by default.

If you the changes made to the arguments to persist even after the procedure is called, then you need to pass the vbscript arguments by reference.

When an argument is passed by value, any changes that the called procedure makes to the value of the variable do not persist after the procedure is called.

The keywords ByRef and ByVal are used to pass arguments by reference and by value respectively.

To understand the difference, first execute the given below code and see the output.

Step 1) Copy the code into your editor

<script type="text/vbscript">
Function returnResult(ByRef value)
   value = value +1
   returnResult = value
End Function
Dim x
x=5
call returnResult(x)
document.write(x)
</script>

Step 2) the output is 6.

Step 3) Change Function returnResult(ByRef value) to Function returnResult(ByVal value)

Step 4) Now your output will be 5.

Summary

  • VBScript procedures are used mainly for better code organization and reuse.
  • There are two types of VBScript procedures: Sub procedures and Function procedures.
  • If you want to execute a series of statements without returning any value, then you can use sub procedures.
  • If you want to execute a series of statements and return a value, then you need to use function procedures.
  • You can pass arguments to the procedures by reference or by value. Arguments are passed by reference by default.
  • If you want to make the changes happened to the arguments persist even after the procedure is called, then you need to pass the arguments by reference and otherwise by value.

 

YOU MIGHT LIKE: