Search code examples
vbscriptsubroutine

creating a subroutine (VBscript)


This one should be extremely easy but I am new to VB script and for some reason I can't get a subroutine to work. I get the error " VBScript compilation error" it points to line 5 Sub add(a,b)

Language="VBSCRIPT"
Sub CATmain()

add 5, 3
Sub add (a, b)
sum=a+b
msgbox sum
End Sub

End Sub

Sorry for the simple question. Thanks for your time.


Solution

  • One Subroutine or a Function should be defined outside any other Subroutines and it should be called from main Subroutine when needed.

    Function Add(A, B)
      Add = CInt(A) + CInt(B)
    End Function
    'Here Add should be a Function, not a Subroutine.
    
    Sub CATmain()
    Dim Sum
      Sum = Add(5, 3)
      'Calling Add and store its result in Sum Variable
      MsgBox "Sum: " + CStr(Sum)
    End Sub
    
    CATmain
    'Calling your main Subroutine CATmain
    

    Sub is used to declare the name, arguments, and code that form the body of a Sub procedure in VB Script.