FUNCTION

From Recital Documentation Wiki
Jump to: navigation, search

Purpose

Declare a User Defined Function (UDF)


Syntax

FUNCTION <expC>[(<parameter-list>)]

[RETURN <exp> | ENDFUNC]


See Also

ALIAS, DO, DO CASE, DO WHILE, ENDFUNC, FUNCTION_EXISTS(), IF, KEYWORD, LINK, PARAMETERS, PARAMETERS(), PCOUNT(), PRIVATE, PROCEDURE, PUBLIC, RETURN, SET COMPATIBLE, SET PROCEDURE, VARINFO()


Description

A FUNCTION <expC> command is used to declare a User Defined Function (UDF). Recital UDFs can be used not only in programs, but also in the Applications Data Dictionary, the Report Writer and wherever a standard Recital function can be used.

A UDF can have a variable number of parameters passed to it. These are assigned to private variables in the the <parameter-list> declaration or PARAMETERS statement, which if used, must appear following the FUNCTION statement and before any other executable statement. If a PARAMETERS statement is present, then at least one parameter must be specified with the UDF call. The PCOUNT() function can be used to determine how many actual parameters were specified.

The functions in a procedure library file are made known to the Recital process by using the SET PROCEDURE TO command. Functions can be included in program files, as well as in procedure library files. If a function is included in a program file, then it must be defined before it is used.

The FUNCTION command is terminated with an ENDFUNC or RETURN statement. The RETURN statement can optionally have an expression specified. If none is specified then .F. is returned. Other RETURN statements may be included in the syntax following a FUNCTION command, provided that they are properly nested between IF...ENDIF, DO WHILE...ENDDO, or DO CASE...ENDCASE. If both RETURN and ENDFUNC are omitted, the function definition will be terminated when another PROCEDURE or FUNCTION command is reached.

Recital function names can be a maximum of 32 characters in length, and must begin with a letter or underscore, followed by any combination of letters (A-Z), digits (0-9), and underscores (_).

To execute a UDF, name it in an expression, followed by parameters in round brackets. It will behave just like any standard Recital function.

There is no limit to the number of functions that can be declared in the Recital/4GL. The commands LIST PROCEDURE or DISPLAY PROCEDURE will let you see all currently active functions and procedures.


Example

// Function to return product of two numbers
function atimesb
  parameters a, b
  result=a*b
return result
 
? atimesb(5,10)
        50
 
//or
function atimesb(a, b)
  result=a*b
return result
 
? atimesb(5,10)
        50


Products

Recital Server, Recital