Menu Bar of the blog

Monday, 3 November 2014

Tutorial#3.1: Arrays and Functions in VB Script

Arrays constitute an important part of VB Script. They divide the memory space into equal parts. The below diagram depicts an array with 6 elements. First element of the array is at 0th position.
In VB, each element of an array can be of any data type, for eg, the below array x contains integers, string, boolean and decimal.
Dim x(5)    'Declaring an array with 6 elements.
x(0) = 10
x(1) = 22
x(2) = "Hi All"
x(3) = true
x(4) = 123.64
x(5) = 13


To find size of an array -
print "Size of the Array:" & ubound(x) + 1.
Output: 6.
Hence, ubound() method returns Largest subscript of a given array.
To print an element of the array:
print x(2)
Output: Hi All

To iterate an array -
For i = 0 To ubound(x)     
   print x(i)
Next


To iterate an array in reverse order -
For i = ubound(x) To 0 Step -1     
   print x(i)
Next


One of the practical uses of arrays in automation is:-
If we need to fetch a large amount of data from browsers and store them in the form of variables, then arrays can be used. Instead of making numerous variables, we can store the values in different elements of an array.

Re-dimensional Arrays:-
Arrays are redimensional, that is, their dimension can be changed at run-time.
Dim y()
ReDim y(2)     'Redimensional array with size 2.
y(0) = 10
y(1) = 21
y(2) = 32
ReDim Preserve y(4)
y(3) = 43
y(4) = 58
For i = 0 To ubound(y)
    print y(i)
Next

In the above code, an array 'y' is declared as re-dimensional with size 2. Redim keyword represents that the size of the array can be changed at a later stage.
In the highlighted statement, dimension of the array is changed to 4 with a Preserve keyword. Here, preserve keyword represents that already existing elements of the array will remain preserved while other elements are being added. Hence, dimension of the array is extended from 3 to 5, while keeping the existing elements preserved.
So, the Output will be:-
10
21
32
43
58

Functions in VB Script -
Hence, a function is something which can accept any number of values from 0 to n and can return either a single value or no value at all. The below example explains how a function is invoked, declared and defined; and how we can return a value through a function.
Writing a function to find sum of first n numbers-
sum = 0
Function sumFirstN(n)       'Declaring and defining a function.
    For i = 0 To n
        sum = sum + i     
    Next
    sumFirstN = sum    'A function can return a value by assigning function name to the value to be returned.
End Function
msgbox "Sum of first 10 numbers:" & sumfirstN(10)   ' SumFirstN() function is called with 10 as a parameter.


Note:-
  1. Scope of a variable inside a function is always the function in which it is defined. Out of the function, the variable doesn't exist.
  2. Whenever a return value is expected from a function, then brackets are used while calling the function. Eg: Function call - sumfirstN(10
==================*********************=====================

No comments:

Post a Comment