Siebel VB Language Reference > VB Language Reference >

Function...End Function Statement


This standard VB construct defines a function procedure.

Syntax

[Static] [Private] Function name([[Optional ]argument
[As type]][, ... ]) [As funcType]
   name = expression
End Function

Placeholder
Description

name

The name of the function

argument

The argument to pass to the function when it is called

type

The data type for the argument

funcType

The data type for the value returned by the function

Returns

The value calculated by the expression; the program line name = expression assigns the return value to the name of the function.

Usage

The purpose of a function is to produce and return a single value of a specified type. Recursion is supported.

The data type of name determines the type of the return value. Use a type character as part of the name, or use the As funcType clause to specify the data type. Otherwise the default data type is variant. When calling the function, you need not specify the type character.

The arguments are specified as a comma-separated list of variable names. The data type of an argument can be specified by using a type character or by using the As clause. Record arguments are declared using an As clause and a type that has previously been defined using the Type statement. Array arguments are indicated by using empty parentheses after the argument. The array dimensions are not specified in the Function statement. Every reference to an array argument within the body of the function must have a consistent number of dimensions.

You specify the return value for the function name using the name = expression assignment, where name is the name of the function and expression evaluates to a return value. If omitted, the value returned is 0 for numeric functions and a null string ("") for string functions and vartype 0 (Empty) is returned for a return type of variant. The function returns to the caller when the End Function statement is reached or when an Exit Function statement is executed.

If you declare an argument as Optional, a procedure can omit its value when calling the function. Only arguments with variant data types can be declared as optional, and optional arguments must appear after the required arguments in the Function statement. The function IsMissing must be used to check whether an optional argument was omitted by the user or not. Named arguments are described under the Call statement heading, but they can be used when the function is used in an expression as well.

The Static keyword specifies that the variables declared within the function retain their values as long as the program is running, regardless of the way the variables are declared.

The Private keyword specifies that the function is not accessible to functions and subprograms from other modules. Only procedures defined in the same module have access to a Private function.

Basic procedures use the call by reference convention. This means that if a procedure assigns a value to an argument, it modifies the variable passed by the caller. Use this feature with great care.

Use Sub to define a procedure with no return value.

CAUTION:  You cannot write your own functions or subprograms directly in the methods and events exposed in Siebel Tools. You can write functions and subprograms in the (general) (declarations) section of a given method script. However, if you want your routines to be available throughout the program, you can use the Application_PreInvokeMethod or an external DLL file as a central place to write them. For details, read Siebel Technical Notes #207 and #217.

If you create more than one function or subprogram in the (general) (declarations) section, be sure that any function or subprogram that may be called by other user-defined functions and subprograms appears before the procedure that calls it. Otherwise, you cannot compile your procedures.

Example

This example declares a function that is later called by the main subprogram. The function performs a calculation on the value sent to it, thereby changing the value of the variable. For other examples, read Declare Statement, and the second example within GoTo Statement.

(general) (declarations)
Option Explicit
Declare Function Calculate(i as Single) As Single

Function Calculate(i As Single)
   i = i * 3 + 2
   Calculate = i
End Function

Sub Button_Click
   Dim x as String
   Dim y As Single
   x = 34
   y = val(x)
   Call Calculate(y)
End Sub

Related Topics

Call Statement
Dim Statement
Global Statement
IsMissing Function
Option Explicit Statement
Static Statement
Sub...End Sub Statement

Siebel VB Language Reference Copyright © 2006, Oracle. All rights reserved.