Thursday, January 3, 2019


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 procedure


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:

  • Difference between procedures and functions in vb script Difference between procedures and functions in vb script Major difference between procedures and functions in vb script is that procedures can not return the value but functions can return the value.Procedure Example… Read More
  • Types of Array in vb script Types of Array in vb script There are 2 types of arrays in vb script. Static(Fixed Size) Dynamic Static ArraysStatic array can contain fixed number of elements in array.Example -dim a (10) - This static array will … Read More
  • Loops in Vbscript Loops in Vbscript Here is the list of different looping statements in vbscript. For ....Next For Each ...Next Do While...Loop Do Until....Loop While...Wend 'You can exit from For Loop with Exit For statement'For Loo… Read More
  • What are different conditional statements in vbscript? What are different conditional statements in vbscript? In Vbscript there are 2 types of conditional statements. if ...else Select Case Example of if else -  a = 10 If a > 10 Then Msgbox "a is greater t… Read More
  • 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 procedure Suppose you want to find the sum of … Read More

0 comments:

Post a Comment

Blog Archive

Translate

Popular Posts

Total Pageviews

151,698

Blog Archive