ProgrammerGuide.Net | Step by Step Programmer Guide

VB-Declaration

Declaration,

Function Procedures
Declares the name, arguments, and code that form the body of a Function procedure.
Syntax: [Public | Private | Friend] [Static] Function name [(arglist)] [As type]
[statements]
[name = expression]
[Exit Function]
[statements]
[name = expression] End Function
The Function statement syntax has these parts:

PartDescription
PublicOptional. Indicates that the Function procedure is accessible to all other procedures in all modules. If used in a module that contains an Option Private, the procedure is notavailable outside the project.
  
PrivateOptional. Indicates that the Function procedure is accessible only to other procedures in the module where it is declared.
  
FriendOptional. Used only in a class module. Indicates that the Function procedure is visible throughout the project, but not visible to a controller of an instance of an object.
  
StaticOptional. Indicates that the Function procedure’s local variables are preserved between calls. The Static attribute doesn’t affect variables that are declared outside the Function,even if they are used in the procedure
  
NameRequired.Name of the Function; follows standard variable naming conventions.
  
arglistOptional. List of variables representing arguments that are passed to the Function procedure when it is called. Multiple variables are separated by commas.
  
typeOptional. Data type of the value returned by the Function procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String, or (except fixed length), Object, Variant, or any user-defined type.
  
statementsOptional. Any group of statements to be executed within the Function procedure.
  
expressionOptional. Return value of the Function.

The arglist argument has the following syntax and parts:
[Optional] [ByVal | ByRef] [ParamArray] varname[( )] [As type] [= defaultvalue]

PartDescription
OptionalOptional. Indicates that an argument is not required. If used, all subsequent arguments in arglist must also be optional and declared using the Optional keyword. Optional can’t be used for any argument if ParamArray is used.
  
ByValOptional. Indicates that the argument is passed by value.
  
ByRefOptional. Indicates that the argument is passed by reference. ByRef is the default in V.B.
  
ParamArrayOptional. Used only as the last argument in arglist to indicate that the final argument is an Optional array of Variant elements. The ParamArray keyword allows you to provide an arbitrary number of arguments. It may not be used with ByVal, ByRef, or Optional.
  
varnameRequired. Name of the variable representing the argument; follows standard variable naming conventions.
  
typeOptional. Data type of the argument passed to the procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported) Date, String (variable length only), Object, Variant, or a specific object type. If the parameter
  
defaultvalueOptional. Any constant or constant expression. Valid for Optional parameters only. If the type is an Object, an explicit default value can only be Nothing.

Remarks
If not explicitly specified using Public, Private, or Friend, Function procedures are public by default. If Static isn’t used, the value of local variables is not preserved between calls. The Friend keyword can only be used in class modules. However, Friend procedures can be accessed by procedures in any module of a project. A Friend procedure does’t appear in the type library of its parent class, nor can a Friend procedure be late bound.

Please click here for related products on Amazon!

Jayashee

Add comment

Want to Check Celebrities News?