11. VbScript | Subroutines

Today we are going to talk about Subroutines in VbScript. Subroutines are used to store a bunch of codes inside a document and recall it to use it whenever you want. …

Subroutines store scripts inside the “sub” tag; like this:

Sub Greeting
msgbox "hello"
End Sub

If you run a file with only subroutines, nothing will happen. You need to call the subroutine to run what is inside the subroutine. Like this:

Sub Greeting
msgbox "hello"
End Sub
Call Greeting

Output:

1

Also, the order of the subroutine and when you call it does not matter. Like this:

Call Greeting
Sub Greeting
msgbox "hello"
End Sub

You can also change what is inside of the subroutine without directly changing the code. You can do this by setting a variable inside of the subroutine as x. And change the value of x as you like. For example:

Sub Greet(x)
msgbox "Hello",20,x
End Sub
Call Greet("Welcome person")

Output:

2

Expanding upon the usage of variables, you can use more than 1 variable to change what is inside the subroutine. Like so:

Sub Greet(x,word)
msgbox word,20,x
End Sub
Call Greet("Title","Hello there")

Output:

3


Example1:

Sub Greet(user)
msgbox "Hello, "& user
End Sub
Call Greet("Jeremy")

Output:

4

Example2:

Sub Greet(user)
msgbox"Hello, "& user
End Sub
x=InputBox("Please Type in your name")
Call Greet(x)

Output:

4


What's Your Opinion?