Partager via


How to: Call Procedures and Functions

You can call native Visual FoxPro functions, user-defined functions, and procedures in different ways. In addition, you can call user-defined functions (UDFs) the same way you call procedures. You can call procedures that you create in a program (.prg) file as if they were standalone programs or as functions if your procedures return values you want to use.

Tip

If you include procedures and user-defined functions in a separate program (.prg) file, you can open those files using the SET PROCEDURE command. For example, suppose you have a file called ProcFile.prg. The following line of code opens the file:

SET PROCEDURE TO ProcFile.prg

To call a Visual FoxPro or user-defined function

  • Call the function without storing the return value.

    -OR-

  • Call the function and assign the return value to a variable.

    -OR-

  • Include the function call within another command or function.

Some functions also accept data as arguments that are passed from the calling program. For more information, see Passing Data to Parameters and Parameters in Procedures and Functions.

In the following example, the first line of code calls the DATE( ) function, which returns the current system date, without performing any operations with the return value. The second line of code stores the return value to a variable named dToday. The third line of code includes the function in another command and sends the return value to the currently active output window. The fourth line of code includes the function in another function and passes the return value to the outer function, which returns the day of the week.

DATE( )
dToday = DATE( )
? DATE( )
DOW(DATE( ))

For more information, see Returning Data from Procedures and Functions.

To call a procedure

  • Use the DO command followed by the procedure name.

In the following example, the DO command calls procedures named myProc1 and myProc2. The first line of code calls myProc1 without arguments. The second line of code includes the WITH clause in the DO command to pass a list of arguments to myProc2.

DO myProc1
var1=4
var2=5
DO myProc2 WITH (var1, var2)

For more information, see DO Command.

See Also

Tasks

How to: Create Procedures and Functions

Concepts

User-Defined Procedures and Functions

Other Resources

Working with Procedures and Functions