📄 vbs14.htm
字号:
<HTML>
<HEAD>
<TITLE>VBScript Procedures</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso8859-1">
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="PRODUCT" CONTENT="Visual Basic Scripting Edition">
<META NAME="TECHNOLOGY" CONTENT="SCRIPTING">
<META NAME="CATEGORY" CONTENT="Tutorial">
<META NAME="Description" CONTENT="VBScript Procedures">
</HEAD>
<BODY BGCOLOR=FFFFFF LINK=#0033CC>
<!--TOOLBAR_START-->
<!--TOOLBAR_EXEMPT-->
<!--TOOLBAR_END-->
<FONT FACE="Verdana, Arial, Helvetica" SIZE=2>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%><TR VALIGN=TOP><TD WIDTH=360>
<FONT SIZE=1 COLOR=#660033>Microsoft® Visual Basic® Scripting Edition</FONT><BR>
<FONT SIZE=5 COLOR=#660033><B>VBScript Procedures</B></FONT>
</TD>
<TD ALIGN=RIGHT>
<FONT SIZE=2> <A HREF="vbstutor.htm">VBScript Tutorial</A> <BR> <A HREF="vbs10.htm">Previous</A> | <A HREF="vbs3.htm">Next</A> <P></FONT>
</TD></TR>
</TABLE>
<BR>
<HR NOSHADE SIZE=1>
<H5>Kinds of Procedures</H5>
<BLOCKQUOTE>
In VBScript there are two kinds of procedures; the <A HREF="vbs602.htm"><B>Sub</B></A> procedure and the <A HREF="vbs583.htm"><B>Function</B></A> procedure.
<H5>Sub Procedures</H5>
A <B>Sub</B> procedure is a series of VBScript statements, enclosed by <B>Sub</B> and <B>End Sub</B> statements, that perform actions but don't return a value. A <B>Sub</B> procedure can take arguments (constants, variables, or expressions that are passed by a calling procedure). If a <B>Sub</B> procedure has no arguments, its <B>Sub</B> statement must include an empty set of parentheses ().<P>
The following <B>Sub</B> procedure uses two intrinsic, or built-in, VBScript functions,
<A HREF="vbs164.htm"><B>MsgBox</B></A> and
<A HREF="vbs121.htm"><B>InputBox</B></A>, to prompt a user for some information. It then displays the results of a calculation based on that information. The calculation is performed in a <B>Function</B> procedure created using VBScript. The <B>Function</B> procedure is shown after the following discussion.
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Sub ConvertTemp()
temp = InputBox("Please enter the temperature in degrees F.", 1)
MsgBox "The temperature is " & Celsius(temp) & " degrees C."
End Sub
</FONT></PRE></BLOCKQUOTE>
<H5>Function Procedures</H5>
A <B>Function</B> procedure is a series of VBScript statements enclosed by the <B>Function</B> and <B>End Function</B> statements. A <B>Function</B> procedure is similar to a <B>Sub</B> procedure, but can also return a value. A <B>Function</B> procedure can take arguments (constants, variables, or expressions that are passed to it by a calling procedure). If a <B>Function</B> procedure has no arguments, its <B>Function</B> statement must include an empty set of parentheses. A <B>Function</B> returns a value by assigning a value to its name in one or more statements of the procedure. The return type of a <B>Function</B> is always a <B>Variant</B>.<P>
In the following example, the Celsius function calculates degrees Celsius from degrees Fahrenheit. When the function is called from the ConvertTemp <B>Sub</B> procedure, a variable containing the argument value is passed to the function. The result of the calculation is returned to the calling procedure and displayed in a message box.
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Sub ConvertTemp()
temp = InputBox("Please enter the temperature in degrees F.", 1)
MsgBox "The temperature is " & Celsius(temp) & " degrees C."
End Sub
Function Celsius(fDegrees)
Celsius = (fDegrees - 32) * 5 / 9
End Function
</FONT></PRE></BLOCKQUOTE></BLOCKQUOTE>
<H5>Getting Data into and out of Procedures</H5>
<BLOCKQUOTE>
Each piece of data is passed into your procedures using an <A HREF="vbs0.htm#defArg">argument</A>. Arguments serve as placeholders for the data you want to pass into your procedure. You can name your arguments anything that is valid as a variable name. When you create a procedure using either the <B>Sub</B> statement or the <B>Function</B> statement, parentheses must be included after the name of the procedure. Any arguments are placed inside these parentheses, separated by commas. For example, in the following example, <FONT FACE="Courier New" SIZE=2>fDegrees</FONT> is a placeholder for the value being passed into the Celsius function for conversion:
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Function Celsius(fDegrees)
Celsius = (fDegrees - 32) * 5 / 9
End Function
</FONT></PRE></BLOCKQUOTE>
To get data out of a procedure, you must use a <B>Function</B>. Remember, a <B>Function</B> procedure can return a value; a <B>Sub</B> procedure can't.
</BLOCKQUOTE>
<H5>Using <B>Sub</B> and <B>Function</B> Procedures in Code</H5>
<BLOCKQUOTE>
A <B>Function</B> in your code must always be used on the right side of a variable assignment or in an expression. For example:<P>
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Temp = Celsius(fDegrees)</FONT></PRE></BLOCKQUOTE>
or
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> MsgBox "The Celsius temperature is " & Celsius(fDegrees) & " degrees."
</FONT>
</PRE></BLOCKQUOTE>
To call a <B>Sub</B> procedure from another procedure, you can just type the name of the procedure along with values for any required arguments, each separated by a comma. The <A HREF="vbs568.htm"><B>Call</B></A> statement is not required, but if you do use it, you must enclose any arguments in parentheses.<P>
The following example shows two calls to the <FONT FACE="Courier New" SIZE=2>MyProc</FONT> procedure. One uses the <B>Call</B> statement in the code; the other doesn't. Both do exactly the same thing.
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Call MyProc(firstarg, secondarg)
MyProc firstarg, secondarg
</FONT></PRE></BLOCKQUOTE>
Notice that the parentheses are omitted in the call when the <B>Call </B>statement isn't used.<P>
</BLOCKQUOTE>
<hr noshade size=1>
<p align=center><em><a href="../../common/colegal.htm">© 1997 by Microsoft Corporation. All rights reserved.</a></em></p>
</FONT>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -