📄 vbs602.htm
字号:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><HTML><HEAD><TITLE>Sub Statement</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="Language Reference">
<META NAME="Keywords" CONTENT="As,Sub statement,ByRef,ByVal,Public,End Sub,Private,Exit Sub,procedure,sub procedures,procedures sub,arguments passing,creating procedures,procedures creating,recursive procedures,procedures,arguments"><META NAME="Description" CONTENT="Sub Statement"></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>Sub Statement</B></FONT>
</TD>
<TD ALIGN=RIGHT>
<FONT SIZE=2> <A HREF="vbstoc.htm">Language Reference</A> <BR>
<A HREF="vbs16.htm">Version 1</A> <P></FONT>
</TD></TR>
</TABLE>
<FONT SIZE=2><p><A HREF="vbs603.htm">See Also</A></FONT>
<HR noshade SIZE=1>
<H5>Description</H5>
<BLOCKQUOTE>Declares the name, arguments, and code that form the body of a <b>Sub</b> procedure.</BLOCKQUOTE>
<H5>Syntax</H5>
<BLOCKQUOTE>[<B>Public</B> | <B>Private</B>] <b>Sub</b> <i>name</i> [<b>(</b><i>arglist</i><b>)</b>] <BR>
[<i>statements</i>]<BR>
[<b>Exit Sub</b>]<BR>
[<i>statements</i>]<BR>
<b>End Sub</b><P>
The <b>Sub</b> statement syntax has these parts:<P>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR VALIGN=TOP BGCOLOR="#DDDDDD">
<TD><FONT SIZE=2><b>Part</b></FONT></TD>
<TD><FONT SIZE=2><b>Description</b></FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>Public</b></FONT></TD>
<TD><FONT SIZE=2>Indicates that the <b>Sub</b> procedure is accessible to all other procedures in all scripts.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>Private</b></FONT></TD>
<TD><FONT SIZE=2>Indicates that the <b>Sub</b> procedure is accessible only to other procedures in the script where it is declared.</FONT></TD></TR>
<!--TR VALIGN=TOP>
<TD><FONT SIZE=2><b>Static</b></FONT></TD>
<TD><FONT SIZE=2>Indicates that the <b>Sub</b> procedure's local variables are preserved between calls. The <b>Static</b> attribute doesn't affect variables that are declared outside the <b>Sub</b>, even if they are used in the procedure.</FONT></TD></TR-->
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>name</i></FONT></TD>
<TD><FONT SIZE=2>Name of the <b>Sub</b>; follows standard <A HREF="vbs0.htm#defVariable">variable</A> naming conventions.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>arglist</i></FONT></TD>
<TD><FONT SIZE=2>List of variables representing arguments that are passed to the <b>Sub</b> procedure when it is called. Multiple variables are separated by commas.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>statements</i></FONT></TD>
<TD><FONT SIZE=2>Any group of statements to be executed within the body of the <b>Sub</b> procedure.</FONT></TD></TR>
</TABLE>
The <i>arglist</i> argument has the following syntax and parts:<P>
[<b>ByVal</b> | <b>ByVal</b>] <i>varname</i>[<b>( )</b>]<P>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR VALIGN=TOP BGCOLOR="#DDDDDD">
<TD><FONT SIZE=2><b>Part</b></FONT></TD>
<TD><FONT SIZE=2><b>Description</b></FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>ByVal</b></FONT></TD>
<TD><FONT SIZE=2>Indicates that the argument is passed <A HREF="vbs0.htm#defByValue">by value</A>.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><b>ByRef</b></FONT></TD>
<TD><FONT SIZE=2>Indicates that the argument is passed <A HREF="vbs0.htm#defByReference">by reference</A>.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>varname</i></FONT></TD>
<TD><FONT SIZE=2>Name of the variable representing the argument; follows standard variable naming conventions.</FONT></TD></TR></TABLE></BLOCKQUOTE>
<H5>Remarks</H5>
<BLOCKQUOTE>If not explicitly specified using either <B>Public</B> or <B>Private</B>,
<b>Sub</b> procedures are public by default, that is, they are visible to all other procedures in your script. The value of local variables in a <B>Sub</B> procedure is not preserved between calls to the procedure.<P>
All executable code must be contained in <A HREF="vbs0.htm#defProcedure">procedures</A>. You can't define a <b>Sub</b> procedure inside another <b>Sub</b> or <b>Function</b> procedure.<P>
The <b>Exit Sub </b>statement causes an immediate exit from a <b>Sub</b> procedure. Program execution continues with the statement following the statement that called the <b>Sub</b> procedure. Any number of <b>Exit Sub</b> statements can appear anywhere in a <b>Sub</b> procedure.<P>
Like a <b>Function</b> procedure, a <b>Sub</b> procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. However, unlike a <b>Function</b> procedure, which returns a value, a <b>Sub</b> procedure can't be used in an expression.<P>
You call a <b>Sub</b> procedure using the procedure name followed by the argument list. See the <b>Call</b> statement for specific information on how to call <b>Sub</b> procedures.
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 WIDTH=87%><TR><TD COLSPAN=2 VALIGN=BOTTOM><hr noshade size=1></TD></TR>
<TR><TD VALIGN=TOP><FONT SIZE=2><b>Caution</b> <b>Sub</b> procedures can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow. <!--The <b>Static</b> keyword usually is not used with recursive <b>Sub</b> procedures.--></FONT></TD></TR>
<TR><TD COLSPAN=2 VALIGN=TOP><hr noshade size=1></TD></TR></TABLE>
Variables used in <b>Sub</b> procedures fall into two categories: those that are explicitly declared within the procedure and those that are not. Variables that are explicitly declared in a procedure (using <b>Dim</b> or the equivalent) are always local to the procedure. Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure.
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 WIDTH=87%><TR><TD COLSPAN=2 VALIGN=BOTTOM><hr noshade size=1></TD></TR>
<TR><TD VALIGN=TOP><FONT SIZE=2><b>Caution</b> A procedure can use a variable that is not explicitly declared in the procedure, but a naming conflict can occur if anything you have defined at the <A HREF="vbs0.htm#defScriptLevel">script level</A> has the same name. If your procedure refers to an undeclared variable that has the same name as another procedure, <A HREF="vbs0.htm#defConstant">constant</A> or variable, it is assumed that your procedure is referring to that script-level name. Explicitly declare variables to avoid this kind of conflict. You can use an <b>Option Explicit</b> statement to force explicit declaration of variables.</FONT></TD></TR>
<TR><TD COLSPAN=2 VALIGN=TOP><hr noshade size=1></TD></TR></TABLE></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 + -