Returning Data from Procedures and Functions

Conventionally, functions return values to the calling program. However, in Visual FoxPro, you can return values from procedures and functions. When procedures and functions do not include explicit RETURN statements that return a specified value, Visual FoxPro includes an implicit RETURN statement that returns a default value of True (.T.) automatically. Generally, you want to use the RETURN command to return a specified value from functions, for example, as a processing result or to indicate whether the function operation succeeded.

Note

The RETURN command also returns control over code execution to the calling program. For more information, see RETURN Command.

In the following example, suppose you pass a date to the following function. The function returns a date that is 14 days later than the date that was passed:

FUNCTION plus2weeks
   PARAMETERS dDate
   RETURN dDate + 14
ENDFUNC

You can then assign the return value from the function to a variable as shown in the following line of code:

dDeadLine = plus2weeks(DATE())

Assigning Return Values from Procedures and Functions

You can assign values returned from procedures and functions explicitly, for example, to a variable, by using the equal sign (=) operator or pass the return value directly, for example, to another function.

In the following example, suppose the function myFunc returns a specific value. The first line of code assigns the return value from myFunc to a variable named myVar. The second line of code passes the return value from myFunc directly to a Visual FoxPro STR( ) function, whose return value is then displayed to the current active output window using the ? command:

myVar = myFunc( )
? STR( myFunc( ) )

See Also

Tasks

How to: Create Procedures and Functions

How to: Call Procedures and Functions

Other Resources

Working with Procedures and Functions