I am new to VBScript, and I learned that VBScript does not have return
and that to return a value, you assign the value to the name of the procedure.
When I was doing research on how to return a value, I found two different programs that both return a value, and I am not sure what the difference is.
Function Test1()
Dim value
'Do something'
If value < 10 Then
Test1 = value * 2
Else
Test1 = value
End If
End Function
Function Test2()
Dim value
'Do something'
If value < 10 Then
Test2 = value * 2
Exit Function
Else
Test2 = value
Exit Function
End If
End Function
It seems like Exit Function
exits the procedure immediately when this program comes to this line, but what is the necessity of this line of code?
I have been learning other major programming languages such as C#, Java, etc, and in those programming languages, once the program comes to the line return
or return something
, the program exits that function/method even if there is mode code after that.
Does this mean, in VBScript, assigning a value to the name of its own procedure serves as return
but it still keeps going without exiting until the end of the procedure unless you use Exit Function
?
VBScript needs the Exit Function
command if you want to instruct the function to return IMMEDIATELY instead of continuing the function. To assign the return value in VBScript, you use FUNCTIONNAME=VALUE_TO_RETURN
. But in VBScript, that type of statement DOES NOT EXIT THE FUNCTION (although in C-like languages the assignment to the return value (return X;
) exits the function immediately.)
For example, say you're trying to generate a filename that doesn't exist yet.
Function GenName( ROOT, file )
targetPath = ROOT & "\" & file.Name
If Not ofso.FileExists( targetPath ) Then
' ASSIGN RETURN VALUE THEN..
GenName = targetPath
Exit Function ' __GET OUT OF THE FUNCTION__
' If you neglect EXIT FUNCTION here, then
' THIS FUNCTION WILL CONTINUE RUNNING TO THE END
' OF IT, even though YOU'VE ALREADY ASSIGNED THE FUNCTION
' A RETURN VALUE
End If
For num = 1 To 10000
' append numbers until you find one that doesn't exist or quit
targetPath = ROOT & "\" & ofso.GetBaseName( file ) & "-" & num & "." & ofso.GetExtensionName( file )
If Not ofso.FileExists( targetPath ) Then
GenName = targetPath ' WANTS to be "return targetPath"
' but you can't do that in VBSCRIPT. So ASSIGN VALUE to
' function name.. AND THEN..
Exit Function '' MUST EXIT FUNCTION otherwise function
' will just continue to run (for loop will keep executing)
' else can break loop with EXIT FOR
End If
Next
End Function
Specifying the function's return value and returning to the caller (via reaching the end of the function's body or an explicit statement) are clearly different things. Being able to express both distinctly is a pro:
>> Function preinc(ByRef i) : i = i + 1 : preinc = i : End Function
>> Function postinc(ByRef i) : postinc = i : i = i + 1 : End Function
>> i = 0
>> WScript.Echo i
>> WScript.Echo preinc(i), i
>> WScript.Echo postinc(i), i
>>
0
1 1
1 2
Languages that combine setting the value and leaving the function (return(x)
, returning the value of the 'last' expression) don't let you do work or cleanup (cf. here) after determining the return value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With