37

I have two functions, and I am trying to use the result of one function in the second one. It's going to the else part, but it's not printing anything for "cus_number".

How do I get the "cus_number" printed?

Function getNumber
    number = "423"
End Function

cus_number = getNumber

If (IsNull(cus_number)) Then
    WScript.Echo "Number is null"
Else
    WScript.Echo "cus_number : " & cus_number
End If
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jill448
  • 1,745
  • 10
  • 37
  • 62

2 Answers2

90

To return a value from a VBScript function, assign the value to the name of the function, like this:

Function getNumber
    getNumber = "423"
End Function
RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • 8
    Does this mean that the function will stop executing after reaching the first assigning the value to its name? For example: Function getNumber If {condition1} Then getNumber = "423" ElseIf {condition2} Then getNumber = "567" Else getNumber = "890" End Function – BohdanZPM Mar 20 '17 at 11:16
  • 3
    @B.Sverediuk No, the function will continue until it reaches either the end of the function or encounters an explicit `Exit Function` statement. – user692942 Dec 20 '19 at 16:27
0

This is how you can return a value from a function in VBS:

Function shouldSendEmail(Line)
    Dim returnValue
    If Line = False Then
        returnValue = True
    Else
        returnValue = False
    End If
    wscript.echo returnValue
    shouldSendEmail = returnValue
End Function

Call a function:

wscript.echo shouldSendEmail(true)
Shubham Verma
  • 8,783
  • 6
  • 58
  • 79