Visual Basic and Visual FoxPro Procedures and Functions

VBA

Sub, Function, Exit

VFP

Procedure, EndProc
Function, Parameters, Return

Common Syntax Notes

Strictly speaking, a function returns a value and has no other effects whereas a procedure does have some external effect and returns no value. Visual Basic sticks to this convention but Visual FoxPro is less strict.

Visual Basic Syntax Notes

VBA Procedure

Visual Basic procedures are declared in a code module with the name of the procedure followed by the name and type of any parameters it requires:

Public Sub CloseApp()
   CloseAllForms True
   DropTables
   Set gdbs = Nothing
   Application.Quit acExit
Exit Sub

A Visual Basic procedure is called by stating the name of the procedure followed by the names or values of any parameters. In the snippet above, CloseAllForms is a procedure which is being called with a parameter True. There is an older syntax which uses Call and puts the parameter(s) into brackets:

Call CloseAllForms (True)

VBA Function

Visual Basic functions are declared in a code module with the name of the function and the data type it returns together with the names and types of any parameters it requires:

Public Function GetLatestVersion(strID As String) As String
   GetLatestVersion = DMax( "Shipped", "Version", "ID = '" & strID & "'")
End Function

The value returned by the function is simply assigned to a variable with the same name as the function, "GetLatestVersion" in this example. A function is called by assigning its value to a variable or by using it in an expression where a variable of that data type would be allowed:

Me.Caption = "Issues for " & GetLatestVersion(mstrID)

Visual FoxPro Syntax Notes

Although FoxPro supports functions and procedures there is no strict distinction between them. They can exist in a prg file with a Procedure or Function header as appropriate or they can exist as a complete prg file with no header. In that situation the entire file acts as though it were a procedure or function with the same name as the prg file itself.

VFP Procedure

A procedure is declared like this with the parameters listed after a Lparameters or Parameters statement:

Procedure MyProc
  Lparameters tcParm1, tcParm2
  * Procedure code goes here
EndProc

Unlike in Visual Basic, there is no need to define the type of the parameters in FoxPro. A procedure is called like this using Do and With:

Do MyProc With "First parameter", "Second parameter"

VFP Function

A FoxPro function is declared like this:

Function MyFunc
  Lparameters tcParm1, tcParm2
  * Function code goes here
  * It calculates a value for lcAnswer
  Return lcAnswer
EndProc

and called like this:

lcValue = MyFunc("First parameter", "Second parameter")

In practice though you can use the function calling syntax on a procedure. If you examine the return value you'll find that the procedure returns true. There is however no need have an assignment statement. You can call a procedure as though it were a function and just throw the return value away:

MyProc("First parameter", "Second parameter")

VFP Method

A VFP method is just a function which is declared inside a Define Class block.

For  |  Language index  |  If