⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vbs583.htm

📁 VBScript 是一种脚本语言
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><HTML>
<HEAD><TITLE>Function 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,ByVal,ByRef,Public,Private,Function statement,procedure,End Function,Exit Function,arguments passing,creating procedures,function procedures,declaring procedures,procedures creating,procedures function,recursive procedures,procedures declaring,procedures recursive,arguments,procedures"><META NAME="Description" CONTENT="Function 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&#174; Visual Basic&#174; Scripting Edition</FONT><BR>
<FONT SIZE=5 COLOR=#660033><B>Function Statement</B></FONT>

</TD>
<TD ALIGN=RIGHT>
<FONT SIZE=2>&nbsp;<A HREF="vbstoc.htm">Language&nbsp;Reference</A>&nbsp;<BR>
<A HREF="vbs16.htm">Version&nbsp;1</A>&nbsp;<P></FONT>
</TD></TR>
</TABLE> 

<FONT SIZE=2><p><A HREF="vbs584.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>Function</b> procedure.</BLOCKQUOTE>

<H5>Syntax</H5>
<BLOCKQUOTE>
[<B>Public</B> | <B>Private</B>] <b>Function</b> <i>name</i> [<b>(</b><i>arglist</i><b>)</b>]<BR>
&nbsp;&nbsp;&nbsp;&nbsp;[<i>statements</i>]<BR> 
&nbsp;&nbsp;&nbsp;&nbsp;[<i>name</i> <b>=</b> <i>expression</i>]<BR> 
&nbsp;&nbsp;&nbsp;&nbsp;[<b>Exit Function</b>] <BR> 
&nbsp;&nbsp;&nbsp;&nbsp;[<i>statements</i>]<BR> 
&nbsp;&nbsp;&nbsp;&nbsp;[<i>name</i> <b>=</b> <i>expression</i>]<BR>
<b>End Function</b>
<P>The <b>Function</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>Function</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>Function</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>Function</b> procedure's local variables are preserved between calls. The <b>Static</b> attribute doesn't affect variables that are declared outside the <b>Function</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>Function</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>Function</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>Function</b> procedure.</FONT></TD></TR>

<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>expression</i></FONT></TD>
<TD><FONT SIZE=2>Return value of the <b>Function</b>.</FONT></TD></TR></TABLE><P>
The <i>arglist</i> argument has the following syntax and parts:<P>
[<b>ByVal</b> | <b>ByRef</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>Function</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>Function</B> 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>Function</b> procedure inside another <b>Function</b> or <b>Sub </b>procedure.<P>
The <b>Exit Function </b>statement causes an immediate exit from a <b>Function</b> procedure. Program execution continues with the statement following the statement that called the <b>Function</b> procedure. Any number of <b>Exit Function</b> statements can appear anywhere in a <b>Function</b> procedure.<P>Like a <b>Sub</b> procedure, a <b>Function</b> procedure is a separate procedure that can take arguments, perform a series of statements, and change the values of its arguments. However, unlike a <b>Sub</b> procedure, you can use a <b>Function</b> procedure on the right side of an expression in the same way you use any intrinsic function, such as <b>Sqr</b>, <b>Cos</b>, or <b>Chr</b>, when you want to use the value returned by the function.<P>
You call a <b>Function</b> procedure using the function name, followed by the argument list in parentheses, in an expression. See the <b>Call</b> statement for specific information on how to call <b>Function</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>&nbsp;&nbsp;<b>Function</b> procedures can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow. </FONT></TD></TR><TR><TD COLSPAN=2 VALIGN=TOP><hr noshade size=1></TD></TR></TABLE>

To return a value from a function, assign the value to the function name. Any number of such assignments can appear anywhere within the procedure. If no value is assigned to <i>name</i>, the procedure returns a default value: a numeric function returns 0 and a string function returns a zero-length string (&quot;&quot;). A function that returns an object reference returns <A HREF="vbs0.htm#defNothing"><b>Nothing</b></A> if no object reference is assigned to <i>name</i> (using <b>Set</b>) within the <b>Function</b>.<P>
The following example shows how to assign a return value to a function named BinarySearch. In this case, <b>False</b> is assigned to the name to indicate that some value was not found.
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3>
<FONT COLOR= "#FF0000">Function</FONT> BinarySearch(. . .)
    <b>. . .</b>
    ' Value not found. Return a value of False.
    If lower &gt; upper Then
        BinarySearch = False  
        <FONT COLOR= "#FF0000">Exit Function</FONT> 
    End If
    <b>. . .</b>
<FONT COLOR= "#FF0000">End Function</FONT>
</FONT></PRE></BLOCKQUOTE>

Variables used in <b>Function</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>&nbsp;&nbsp;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 script level 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>

<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>&nbsp;&nbsp;VBScript may rearrange arithmetic expressions to increase internal efficiency. Avoid using a <b>Function</b> procedure in an arithmetic expression when the function changes the value of variables in the same expression.</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">&copy; 1997 by Microsoft Corporation. All rights reserved.</a></em></p> 
</FONT></BODY></HTML>

















































































⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -