FUNCTION
Parameter type checking for functions
──────────────────────────────────────────────────────────────────────────────

Syntax

[STATIC] FUNCTION <idFunction>(<idParam> AS|ASREF <idType>, ...)
.
. <executable statements>
.
RETURN[(] <exp> [)] [AS <idType>]

Arguments

<idFunction> is the name of the user-defined function to declare.
Please see the CA-Clipper guides for more information.

<idParam> is the declaration of one or more parameter variables.
Variables specified in this list are declared local.

AS|ASREF indicates whether the parameter is expected to be normal or
passed by reference. AS specifies a normal parameter and ASREF
specifies a parameter that is expected to be passed by reference. If
you specify AS, and the parameter is passed by reference, then no error
will be given, as program behaviour is not effected.

<idType> is the type of the parameter/return value. For the
FUNCTION statement, this is called every time the function is called,
and for the RETURN statement it is called just before the return is
executed.

Description

This allows you to define a function which will, at run time, check the
parameter types that has been passed to it. For example, if you define
a function as:

FUNCTION Foo(nNumber1 AS INT, nNumber2 AS INT)
RETURN ( nNumber + nNumber2 ) AS INT

And you call this function with:

? Foo(1, 1.1)

The MrDebug will pop up with the following message in the Assert/Trace
window:

Param 2 is not type INT

And the program will stop at that point for your inspection. So you can
take a look at the calling routine to see what has caused the problem.

If you then continued the program would stop on the RETURN because the
result of 1 + 1.1 is not an integer value. So you would get another
message in the Assert/Trace window:

Return value is not type INT

Notes

The allowable types for <idType> are:

CHARACTER A character string
STRING A character string (Same as CHARACTER)
NUMERIC A number of any specification
INTEGER A numeric integer (no decimal places)
INT A numeric integer (no decimal places)
FLOAT A floating pointer number
LOGICAL A logical value
DATE A date value
BLOCK A code block
CODEBLOCK A code block
ARRAY An array of any length
OBJECT An object of any class
ANY Any data type
USUAL Any data type (Same as ANY)

If you try to compile something like:

RETURN (NIL) AS STRING

you will get a compile time warning. Well, it's the least we can do...

Example

This example shows a function declaration with the second
parameter passed by reference.

FUNCTION Message(nCounter AS INT, cPrompt AS REF)

nCounter ++
? cPrompt

RETURN (nCounter) AS STRING

When this program runs, it will stop on the return and MrDebug
will pop up because 'nCounter' is not an STRING.

Files: Header file is MrDebug.CH