Defining commonly and frequently used routines and operations as separate procedures and functions can reduce program size and complexity, making code easier to read and maintain. For example, you need to make changes only once in the procedure or function instead of multiple times throughout your program. Procedures and functions make it possible for you to keep commonly used code in a single location so you can call it from different parts of your application.
Conventionally, a procedure contains code that performs an operation, while a function contains code that performs operations and returns a specific value. Most, if not all, native Visual FoxPro functions return values. In Visual FoxPro, both procedures and functions can return values. When you create procedures and functions, you can choose whether to follow the convention of returning a value only from functions.
For example, the following lines of code illustrate the basic form of a procedure and function:
В | Copy Code |
---|---|
PROCEDURE myProcedure * Insert procedure code. ENDPROC FUNCTION myFunction * Insert function code. RETURN myFuncReturnValue ENDFUNC |
In their basic forms, procedures and functions might not seem useful or different from each other. However, procedures and functions can also accept and process data that the calling program passes to them through parameters, which you can include as part of procedure and function definitions and calls. Data that passes to procedures and functions are sometimes referred to as "arguments". Procedures and functions also differ in the way that programs pass data to them by default. For more information, see Parameters in Procedures and Functions.
Functions that you create in Visual FoxPro are sometimes called user-defined functions (UDFs). You can include procedures or functions with other normal executable code in a program (.prg) file or standalone in a separate .prg file.
Note: |
---|
If you include procedures and functions with other code, they must exist at the end of the .prg file following all other normal code. You cannot include normal executable program code following procedures and functions in a .prg file. Only other user-defined procedures, functions, and class definitions can follow the first PROCEDURE or FUNCTION statement in the file. |