please dont rip this site

Microsoft® Visual Basic® Scripting Edition
Function Statement
Language Reference |

See Also


Description
Declares the name, arguments, and code that form the body of a Function procedure.

Syntax
[Public | Private] Function name [(arglist)]
    [statements]
    [name = expression]
    [Exit Function]
    [statements]
    [name = expression]
End Function

The Function statement syntax has these parts:

Part Description
Public Indicates that the Function procedure is accessible to all other procedures in all scripts.
Private Indicates that the Function procedure is accessible only to other procedures in the script where it is declared.
name Name of the Function; follows standard variable naming conventions.
arglist List of variables representing arguments that are passed to the Function procedure when it is called. Multiple variables are separated by commas.
statements Any group of statements to be executed within the body of the Function procedure.
expression Return value of the Function.

The arglist argument has the following syntax and parts:

[ByVal | ByRef] varname[( )]

Part Description
ByVal Indicates that the argument is passed by value.
ByRef Indicates that the argument is passed by reference. This is apparently the default?
varname Name of the variable representing the argument; follows standard variable naming conventions.

Remarks

If not explicitly specified using either Public or Private, Function procedures are public by default, that is, they are visible to all other procedures in your script. The value of local variables in a Function is not preserved between calls to the procedure.

All executable code must be contained in procedures. You can't define a Function procedure inside another Function or Sub procedure.

The Exit Function statement causes an immediate exit from a Function procedure. Program execution continues with the statement following the statement that called the Function procedure. Any number of Exit Function statements can appear anywhere in a Function procedure.

Like a Sub procedure, a Function procedure is a separate procedure that can take arguments, perform a series of statements, and change the values of its arguments. However, unlike a Sub procedure, you can use a Function procedure on the right side of an expression in the same way you use any intrinsic function, such as Sqr, Cos, or Chr, when you want to use the value returned by the function.

You call a Function procedure using the function name, followed by the argument list in parentheses, in an expression. See the Call statement for specific information on how to call Function procedures.


Caution  Function procedures can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow.


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 name, the procedure returns a default value: a numeric function returns 0 and a string function returns a zero-length string (""). A function that returns an object reference returns Nothing if no object reference is assigned to name (using Set) within the Function.

The following example shows how to assign a return value to a function named BinarySearch. In this case, False is assigned to the name to indicate that some value was not found.


Function BinarySearch(. . .)
    . . .
    ' Value not found. Return a value of False.
    If lower > upper Then
        BinarySearch = False  
        Exit Function 
    End If
    . . .
End Function

Variables used in Function 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 Dim 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.


Caution  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, constant 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 Option Explicit statement to force explicit declaration of variables.



Caution  VBScript may rearrange arithmetic expressions to increase internal efficiency. Avoid using a Function procedure in an arithmetic expression when the function changes the value of variables in the same expression.



© 1996 by Microsoft Corporation.
file: /Techref/language/asp/vbs/vbscript/377.htm, 10KB, , updated: 2003/9/19 15:21, local time: 2024/11/5 08:43,
TOP NEW HELP FIND: 
52.14.80.90:LOG IN
©2024 PLEASE DON'T RIP! THIS SITE CLOSES OCT 28, 2024 SO LONG AND THANKS FOR ALL THE FISH!

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/Techref/language/asp/vbs/vbscript/377.htm"> Microsoft&reg; Visual Basic&reg; Scripting Edition </A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?