User Defined Functions

Chris Kelleher

Administrator
Staff member
Does any one have any information on the usage and construct of user defined functions. I don't know if it's just me but I don't seen to be able to find too much in the on-line manuals (v8.3b) on this subject.
 

Chris Kelleher

Administrator
Staff member
From the online help:

<BLOCKQUOTE><font size="1" face="Arial, Verdana">quote:</font><HR>

SYNTAX
-----------------------------------------------------------
FUNCTION function-name [RETURNS] data-type
[(param [, param ]...)]
[FORWARD
| [MAP [TO] actual-name ] IN proc-handle ]

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.
<HR></BLOCKQUOTE>

<BLOCKQUOTE><font size="1" face="Arial, Verdana">code:</font><HR><pre>
/* udf1.p */
/* Demonstrates defining and referencing 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.
[/code]
<BLOCKQUOTE><font size="1" face="Arial, Verdana">quote:</font><HR>
The second example, r-udf2.p, forward declares, references, and defines doubler().
<HR></BLOCKQUOTE>
<BLOCKQUOTE><font size="1" face="Arial, Verdana">code:</font><HR><pre>
/* r-udf2.p */
/* Demonstrates forward-declaring, referencing, and defining
* 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.
RETURN (2 * parm1).
END FUNCTION.
[/code]
<BLOCKQUOTE><font size="1" face="Arial, Verdana">quote:</font><HR>
The third example consists of two procedures, r-udfdef.p and r-udf3.p. The
example illustrates defining a user–defined function in an external procedure.

The first procedure, r-udfdef.p, defines doubler().
<HR></BLOCKQUOTE>
<BLOCKQUOTE><font size="1" face="Arial, Verdana">code:</font><HR><pre>
/* r-udfdef.p
* Defines user-defined function doubler()
*/

FUNCTION doubler RETURNS INTEGER (INPUT parm1 AS INTEGER).
RETURN (2 * parm1).
END FUNCTION.
[/code]
<BLOCKQUOTE><font size="1" face="Arial, Verdana">quote:</font><HR>
The second procedure, r-udf3.p, runs the first procedure persistently, declares
doubler(), references it, and deletes the persistent procedure.
<HR></BLOCKQUOTE>
<BLOCKQUOTE><font size="1" face="Arial, Verdana">code:</font><HR><pre>
/* r-udf3.p
* references an externally-defined user-defined function
*/

/* define items */
DEFINE VARIABLE myhand AS HANDLE.
DEFINE VARIABLE mystr AS CHARACTER FORMAT "x(20)".

/* forward declare doubler() */
FUNCTION doubler RETURNS INTEGER (INPUT parm1 AS INTEGER) IN myhand.

/* run the procedure that 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).
[/code]

HTH.

--
Chris Schreiber
ProgressTalk.com Manager
webmaster@progresstalk.com
 

Chris Kelleher

Administrator
Staff member
Can I return a string of constant length from a function, or must it be a fixed size piece of data such as an integer or a single character?

In other programming languages such as 'C' to return a string you would have to allocate a 'piece' of memory and returned the pointer. This always frightens the hell out of me as it allows for memory leaks and memory wastage if the 'piece' is not salvaged after use.

<BLOCKQUOTE><font size="1" face="Arial, Verdana">code:</font><HR><pre>
FUNCTION next-id RETURNS character.
DEFINE VARIABLE id as CHARACTER FORMAT "x(8)".
id = string(number + 1).
RETURN id.
END FUNCTION.
[/code]
 

Chris Kelleher

Administrator
Staff member
That should work if you modify the definition line to allow you to pass the number, I have asumed it's an integer.

FUNCTION next-id RETURNS character(number AS INTEGER):

Rob.B.
 
Top