Functions, Procedures and Subs

Purpose

Blocks of code to which paramters or arguments can be passed and which perform one or more specific tasks; in the case of Functions, the result of any calculations can then be returned to the code that called it.

Evaluates an arithmetic expression which is repeatedly used throughout the program, whereby the result of the expression changes depending on the variables passed to it.

Syntax

Function[Var] name [(arglist)] [As type] [Naked]
[statements]
[Exit Func[tion] [If]]
[statements]
[name = expression | Return expression]
EndFunc[tion]

Proc[edure] name [(arglist)] [Naked]
[statements]
[Exit Proc[edure] [If]]
[statements]
EndProc[edure]

Sub name [(arglist)] [Naked]
[statements]
[Exit Sub [If]]
[statements]
EndSub

Description

Born of Procedural or structured programming introduced with the C language, these three types of subroutines are designed to perform specific tasks independent of other subroutines as well as clones of themselves.

Although the structure of all subroutines is similar, each has a specific purpose.

The body of a subroutine is composed of the declaration (Function, FunctionVar, Procedure or Sub) with a name and parameter list, definition of local variables (Local...), the subroutine statements (with a return value for Functions) and a corresponding end marker (EndFunction, EndProcedure or EndSub).

name Required. Name of the subroutine. This follows standard variable naming conventions.
arglist
statements
Optional. List of variables representing arguments that are passed to the subroutine when it is called. Unless otherwise specified, parameters passed to Function and Procedure are Double and those passed to FunctionVar and Sub are Variant. Multiple variables are separated by commas and are entered in the following format:

ByRef varname[()] [As Type] Show

ByVal varname [As Type] Show

Optional varname [As Type][ = defaultvalue] Show

ParamArray varname() Show


statements

Optional. Any group of statements to be executed within the subroutine.
Naked See here.

[Function and FunctionVar only] Show


[FunctionVar and Sub only] Show


The Exit... statements cause an immediate exit from a subroutine and program execution continues with the statement following that which called the subroutine. Any number of Exit... statements can appear anywhere in a subroutine and can be qualified with an optional If expression which determines whether the exit occurs or not.

Example Show

Remarks

Subroutines can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow. For similar reasons, the Static keyword usually isn't used with recursive subroutines.

Always use FunctionVar for VB functions. Once the function works correctly, it is advisable to change it in a normal Function and change the default types as FunctionVar is time consuming due to the use of Variants.

Known Issues Show

{Created by Sjouke Hamstra; Last updated: 26/11/2023 by James Gaite; Other Contributors: Jean-Marie Melanson}