PreviousNextIndex

FUNCTION statement

Defines or declares a prototype for a user-defined function, or declares a Web service operation. The following syntax boxes describe the syntax for each use of the statement, beginning with a user-defined function definition.

Syntax

FUNCTION function-name [ RETURNS ] return-type [ PRIVATE ] 
  [ ( parameter [ , parameter ] ... ) ] : 
  function-body  

Use the following syntax to declare a user-defined function prototype that is defined later in the same procedure or that is defined in another external procedure:

FUNCTION function-name [ RETURNS ] return-type 
  [ ( parameter [ , parameter ] ... ) ] 
  {    FORWARD  
     | [ MAP [ TO ] actual-name ] IN proc-handle  
     | IN SUPER 
  } . 

Use the following syntax to declare a Web service operation. For more information on declaring Web service operations, see OpenEdge Development: Web Services.

FUNCTION operationName [ RETURNS ] return-type  
  [ ( parameter [ , parameter ] ... ) ] 
  IN hPortType . 

function-name
[ RETURNS ] return-type
object-type-name
CLASS
EXTENT [ constant ]
PRIVATE
( parameter [ , parameter ] ... )
function-body
function-logic
catch-block
END [ FUNCTION ]
FORWARD
[ MAP [ TO ] actual-name ] IN proc-handle
IN SUPER
operationName
hPortType
Examples

The first example, r-udf1.p, defines and references the user-defined function doubler(), which accepts an integer and returns the integer multiplied by two:

r-udf1.p
/* r-udf1.p */ 
/* Defines and references a user-defined function */ 
/* Define doubler() */ 
FUNCTION doubler RETURNS INTEGER (INPUT parm1 AS INTEGER): 
  RETURN (2 * parm1).  
END FUNCTION. 
/* Reference doubler() */ 
DISPLAY  "doubler(0)=" doubler(0) SKIP 
  "doubler(1)=" doubler(1) skip 
  "doubler(2)=" doubler(2) skip. 

The second example, r-udf2.p, declares a prototype for, references, and defines doubler( ):

r-udf2.p
/* r-udf2.p */ 
/* Forward-declares, references, and defines a user-defined function */ 
/* Forward declare doubler() */ 
FUNCTION doubler RETURNS INTEGER (INPUT parm1 AS INTEGER) FORWARD. 
/* Reference doubler() */ 
DISPLAY "doubler(0)=" doubler(0). 
DISPLAY "doubler(1)=" doubler(1). 
DISPLAY "doubler(2)=" doubler(2). 
/* Define doubler() */ 
FUNCTION doubler RETURNS INTEGER (INPUT parm1 AS INTEGER): 
  RETURN (2 * parm1). 
END FUNCTION. 

The third example consists of two procedures, r-udf3.p and r-udfdef.p. The example illustrates defining a prototype for user-defined function that is defined in an external procedure.

The procedure, r-udf3.p, declares the prototype for doubler(), runs r-udfdef.p persistently, invokes doubler(), and deletes the persistent procedure:

r-udf3.p
/* r-udf3.p */ 
/* References an externally-defined user-defined function */ 
/* Define items */ 
DEFINE VARIABLE myhand AS HANDLE    NO-UNDO. 
DEFINE VARIABLE mystr  AS CHARACTER NO-UNDO FORMAT "x(20)". 
/* Forward declare doubler() */ 
FUNCTION doubler RETURNS INTEGER (INPUT parm1 AS INTEGER) IN myhand.  
/* Run the procedure that defines doubler() */ 
RUN src\prodoc\langref\r-udfdef.p PERSISTENT SET myhand.  
     
/* Reference doubler() */ 
DISPLAY "doubler(0)=" doubler(0) SKIP 
        "doubler(1)=" doubler(1) SKIP 
        "doubler(17)=" doubler(17) SKIP. 
/* Delete the procedure that defines doubler */ 
DELETE PROCEDURE myhand.  

The second procedure, r-udfdef.p, defines doubler():

r-udfdef.p
/* r-udfdef.p */ 
/* Defines user-defined function doubler() */ 
FUNCTION doubler RETURNS INTEGER (INPUT parm1 AS INTEGER): 
  RETURN (2 * parm1). 
END FUNCTION. 

To start the third example, run r-udf3.p in the Procedure Editor.

In the fourth example, r-fctrl2.p, the user-defined function fact() implements the factorial function, common in probability and statistics, and commonly notated “!(6! = 6 x 5 x 4 x 3 x 2 x 1; 100! = 100 x 99 x 98 x ... x 3 x 2 x 1):

r-fctrl2.p
/* r-fctrl2.p */ 
/* Demonstrates user-defined function fact() */ 
DEFINE VARIABLE inp AS INTEGER LABEL "Input Value". 
FUNCTION fact RETURNS INTEGER (INPUT val AS INTEGER): 
  IF val LT 0 THEN RETURN 0. 
  IF val LE 1 THEN RETURN 1. 
  RETURN val * fact(val - 1). 
END. 
REPEAT: 
  UPDATE inp WITH TITLE "Factorials". 
  DISPLAY fact(inp) LABEL "Factorial". 
END. 

Notes
See also

DYNAMIC-FUNCTION function, METHOD statement, Parameter definition syntax, PROCEDURE statement, RETURN statement


OpenEdge Release 10.2B
Copyright © 2009 Progress Software Corporation
PreviousNextIndex