|
VBScript Procedures
We have two kinds of procedures: The Sub procedure and the
Function procedure.
A Sub Procedure:
· Is a series of statements, enclosed by the Sub and End Sub
statements
· Perform actions, but do not return a value
· Can take arguments that are passed to it by a calling
procedure
· Without arguments, must include an empty set of parentheses
()
Sub mysub()
some statements
End Sub
The Function Procedure
· Is a series of statements, enclosed by the Function and End
Function statements
· Perform actions, but can also return a value
· Can take arguments that are passed to it by a calling
procedure
· Without arguments, must include an empty set of parentheses
()
· Returns a value by assigning a value to its name
Function myfunction()
some statements
myfunction = some value
End Function
Using Sub and Function Procedures in Code
When you call a Function in your code, you do like this:
name = findname()
Here you call a Function called "findname", the Function returns
a value that will be stored in the variable "name".
Or, you can do like this:
msgbox "Your name is " & findname()
Here you also call a Function called "findname", the Function
returns a value that will be displayed in the message box.
When you call a Sub procedure you can just type the name of the
procedure. You can use the Call statement, like this:
Call MyProc(argument)
Or, you can omit the call statement, like this:
MyProc argument
Examples
1. This procedure does not return a value.
Coding
<html>
<head>
<script language="VBScript">
sub yourcar()
msgbox("You are driving a Ford")
end sub
</script>
</head>
<body>
<script language="VBScript">
call yourcar()
</script>
</body>
</html>
2. Use this procedure if you want to return a value.
Coding
<html>
<head>
<script language="VBScript">
function mycar()
mycar = "Volvo"
end function
</script>
</head>
<body>
<script language="VBScript">
msgbox "I am driving a " & mycar()
</script>
</body>
</html> |