3

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?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
JessicaD
  • 89
  • 3
  • 3
  • 8
  • 3
    You usually use the exit function and you want to stop processing the rest of the function. The example you provided doesn't need the exit function and it is unnecessary and redundant – Sorceri Aug 03 '17 at 18:48
  • So assigning a value to the name of its own procedure does not exit the procedure like it does in other major programming languages? – JessicaD Aug 03 '17 at 18:54
  • 1
    correct it does not exit the function and is only assigning the return value. – Sorceri Aug 03 '17 at 19:14

2 Answers2

3

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
bobobobo
  • 64,917
  • 62
  • 258
  • 363
1

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.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96