Thursday, January 3, 2019


Sub Procedures in VBScript


Sub Procedures in VBScript

Sub procedures are used to perform the specific task. Sub procedures are used to increase the reusability of the code.

Simple example of the Sub Procedures


Suppose you want to find the sum of 2 numbers. Without procedures, you will write below code.

a = 10
b = 20
c = a+ b
Msgbox c

To add another 2 different numbers, you will use below code

a = 33
b = 65
c = a+ b
Msgbox c

In above examples, we have to add 2 numbers. So the operation is same (Repeating) So we can write the procedure which will take 2 parameters as shown below.

sub sum(byref a, byref b)

    msgbox cint(a) + cint(b)

end sub

Calling procedures in VBScript

To call the procedures we can use below statements.

Call sum(10,20)
Call sum(33,65)

Thus we can call procedures many times in the code. This reduces the lines of code as well as maintainability of the code.

Passing arguments to the procedure

We can pass the arguments to the procedure by 2 ways.
  1. Pass by reference 
  2. Pass by value

Pass by reference

By default, values are passed to the procedure by reference. When we pass the values by reference the changes made to the variables in the called procedure are reflected in the calling procedure.

a = 10
call findsqr(a)
msgbox a  'prints 100

sub findsqr(byref a)

  a = a*a

  msgbox a   'prints 100

end sub


Pass by value

When we pass the values by value, the changes made to the variables in the called procedure are not reflected in the calling procedure.

a = 10
call findsqr(a)
msgbox a  'prints 10

sub findsqr(byval a)

  a = a*a

  msgbox a   'prints 100

end sub


Related Posts:

  • Date time functions in vb script Date time functions in vb script It is very important that you know how to work with date and time in VBScript as most of the VBScript programs will have date and time involved in it.Below is the list of all date and … Read More
  • Error handling in VBScript Error handling in VBScript VBScript provides below statements and keywords to handle errors. on error resume next Err object - err.description, err.number Sample code to handle the error is given below If er… Read More
  • Classes in VBScript Classes in VBScript Here is an example that shows how we can create a class in QTP. Once we define the class, we can create its objects and then access its method and properties. 'declare the class book. Class Bo… Read More
  • What are the different operators in vbscript? What are the different operators in vbscript? At broad level there are 3 kinds of operators as mentioned below. Arithmetic Operators Comparison Operators Logical Operators Arithmetic Operators in the order of pre… Read More
  • How we can define array in vb script How we can define array in vb script Arrays are used to store multiple values in the same variable.We can define array in vb script in 4 different ways. dim a(10)  - static array of 11 elements dim b()  … Read More

0 comments:

Post a Comment

Blog Archive

Translate

Popular Posts

Total Pageviews

150,614

Blog Archive