Tuesday, November 4, 2014

VBScript Function and Sub Procedures

Function is a group of reusable statements (lines of code) which can be called anywhere in your program. This eliminates the need of writing same code over and over again. This will enable programmers to divide a big program into a number of small and manageable functions. Apart from inbuilt Functions, VBScript allows us to write user-defined functions as well. 


'Sub procedures
'Sub Procedures are similar to functions but there are few differences.
'Sub procedures DONOT Return a value while functions may or may not return a value.
'Sub procedures Can be called without call keyword.
'Sub procedures are always enclosed within Sub and End Sub statements.


Function welcome()

MsgBox "Welcome to Functions"

End Function


Function AreaofRect1(x,y)

MsgBox "Area of the Rectangle is " & x*y

End Function



Function AreaofRect2(x,y)

Dim area : area = x * Y
AreaofRect2= area    'If you want to Return the result, assigned to be retured value to the function name itself

End Function


Sub AreaofCircle(x)

MsgBox "Area of Circle is "& 3.141 *x*x

End Sub



Call Welcome()

Call AreaofRect1(12,5)

result =  "Area of the Rectangle is " & AreaofRect2(12,15)

MsgBox result

Call AreaofCircle(4)
AreaofCircle(5)