Understanding Procedures in VBScript - Using Parameters
(Page 3 of 5 )
Parameters are used to put data into a procedure. Both Subs and Functions may use parameters in VBScript. To understand this concept, however, you first need to understand variable scope.
In VBScript, a variable’s life is determined by its scope at the time it is declared or initialized. In other words, that variable reference is only valid within a certain scope.
There are two scope levels in VBScript, script-level (sometimes referred to as “global”) and procedural. In other words, a variable that is declared within a procedure is only valid within that procedure. A script-level, or global, variable however is valid anywhere within the script. To understand why this is important, let’s take a look at an example.
strGlobal = "This variable is global."
Sub MySub
intProcedural = 5
End Sub
Function MyFunction
Set objFso = CreateObject("Scripting.FileSystemObject")
End Function
In this example, the strGlobal variable is declared at the top-most level, or script-level. This variable can be used anywhere throughout the script and will continue to be valid until it is either destroyed or script execution ends.
The intProcedural and objFso variables are a little different. Since they were created inside of procedures, they may only be used within those procedures. Any attempt to use them outside will result in an error. This is where parameters come into play.
PrintMessage
Sub PrintMessage
intBorn = 1979
WScript.Echo "Mary was born in", intBorn
WScript.Echo "She is", CalculateAge, "years old."
End Sub
Function CalculateAge
intAge = Year(Date) - intBorn
CalculateAge = intAge
End Function
Here, I’ve taken our previous example and moved all of the Echo statements into a subroutine. Now when I run this script is says that Mary is 2008 years old! That’s not right. What’s happening here?
The problem is with the variable intBorn. This variable only exists within the scope of the PrintMessage subroutine. Therefore, it’s not available when the CalculateAge subroutine attempts to perform the age calculation so the year 2008 minus Nothing equals 2008! To avoid this incorrect calculation, we need a way to put the intBorn variable into the scope of the CalculateAge function.
PrintMessage
Sub PrintMessage
intBorn = 1979
WScript.Echo "Mary was born in", intBorn
WScript.Echo "She is", CalculateAge(intBorn), "years old."
End Sub
Function CalculateAge(intBorn)
intAge = Year(Date) - intBorn
CalculateAge = intAge
End Function
In order to make the intBorn variable available to the CalculateAge function, I have added a parameter. Notice how I’ve changed the Function declaration. When declaring a function or subroutine, you may include a list of comma-separated parameters in parentheses. You must then provide a value for these parameters whenever you call the function or subroutine, as you can see in the Echo statement above.
Next: More on Scopes and Parameters >>
More Windows Scripting Articles
More By Nilpo