Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- The Guide To Clipper - <b>function</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
FUNCTION


Syntax:     FUNCTION <procedure>
               <statements>...
            RETURN <exp>

Purpose:    To declare a user-defined function.

Arguments:  <procedure> is the declared name of the user-defined
            function.  Procedure and user-defined function names can be
            up to 10 characters in length.

            <exp> is the function return value.  All user-defined
            functions must return a value.

Usage:      User-defined functions are the same as procedures with two
            exceptions.  They must begin with the FUNCTION declaration
            and contain a RETURN statement with an argument (in order to
            return a value).

            To call a user-defined function, use the same notation as
            you would when calling Clipper functions:

            function(<parameter list>)

            If you are not concerned with the return value, you can use
            the same notation to place a user-defined function on a line
            by itself.  In this case the return value is ignored.

            Passing parameters: Parameters passed to user-defined
            functions are passed by value with two exceptions.  First,
            if the actual parameter is an array reference, the entire
            array is passed by reference.  Second, if the actual
            parameter is a memory variable preceded by the "at" sign
            (@), it is passed by reference.  (Memory variables passed by
            reference can be changed by the function.)  Note that
            database fields cannot be passed by reference and are always
            passed by value.  If a parameter is an expression, the
            expression is evaluated and the resulting value is passed to
            the function.

Library:    CLIPPER.LIB


----------------------------------- Examples -------------------------------

   The following example uses a user-defined function to center a string
   within an @...SAY statement:

   @ 12, Center("Hi there") SAY "Hi there"
   RETURN

   FUNCTION Center
   PARAMETERS string

   RETURN INT((80 - LEN(string)) / 2)


   In this example, a variable is passed to a user-defined function by
   reference.  Note how changes to the formal parameter in the
   user-defined function change the original variable.

   value = 10
   ? Changvar(value)
   ? value                     && Result: 10

   ? Changvar(@value)
   ? value                     && Result: 20


   FUNCTION Changvar
   PARAMETER var

   var = var * 2

   RETURN (var)


   This example demonstrates the use of a user-defined function
   in a VALID clause in order to validate data entry:

   num = 0
   @ 1, 0 SAY "Enter number: " GET num VALID Valnum(num)
   READ
   RETURN

   FUNCTION Valnum
   PARAMETER number

   RETURN (number > 10 .AND. number < 20)


See Also: PARAMETERS PROCEDURE RETURN

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson