VB program



VBScript can use two programs:

  • Subprogram

  • Function program



VBScript Subprogram

Subprogram:

  • is a series of statements, encapsulated in Sub and End Sub statements Within

  • you can perform certain operations, but will not return a value

  • can have parameters

Sub mysub()
some statements
End Sub

or

Sub mysub(argument1,argument2)
some statements
End Sub

Example

实例(仅适用于 IE)
Sub mysub()
  document.write("I was written by a sub procedure")
End Sub

Run Example»

Click the "Run Example" button to view the online example


VBScript function program

Function program

  • is a series of statements, encapsulated in Function and End Function statements

  • can perform certain operations, and will return a value

  • can have parameters passed to it through program calls.

  • If there are no parameters, empty parentheses () must be included

  • By assigning a value to the function program name, it can be returned Value

Function myfunction()
some statements
​ myfunction=some value
End Function

or

Function myfunction(argument1,argument2)
some statements
​ myfunction=some value
End Function

Instance

实例(仅适用于 IE)
function myfunction()
  myfunction=Date()
end function

Run Instance»

Click the "Run Example" button to view the online example


Calling the program

This simple function program is called to calculate the sum of two parameters:

Instance

实例(仅适用于 IE)
Function myfunction(a,b)
myfunction=a+b
End Function

document.write(myfunction(5,9))

Run Instance»

Click the "Run Instance" button to view the online instance

Function "myfunction" will return the sum of parameter "a" and parameter "b". What is returned here is 14.

When you call a program, you can use the Call statement as follows:

Call MyProc(argument)

Or, you can omit Call statement, as shown below:

MyProc argument