Skip to content

Global Procedures

Declaring Global Procedures

A procedure declaration associates an identifier with a block of code as a procedure. That procedure can then be called within your program by a Send statement. The syntax for a procedure declaration is:

Procedure {procedure-name} Global [{type1} {param1} {type2} {param2}]
[{variable-declarations}]
{statement 1}
{statement 2}{statement n}
End_Procedure

Where:

  • {procedure-name} is the identifier associated with the procedure. You cannot use a DataFlex reserved word as a procedure name.
  • {type1} is the data type of parameter {param1}.
  • {type2} is the data type of {param2}, etc.
  • {variable-declarations} are the local variables that you can declare for the procedure.
  • {statement 1-n} are the statements that are executed whenever the procedure is called.

You can define zero or more parameters for each procedure. Each parameter behaves like a local variable that is initialized by the statement that calls the procedure. The data types of each parameter can be any of the DataFlex simple types (Integer, Number, Real, etc.) as well as struct types and arrays.

By reference parameters can also be declared. In this case, the parameter's name must be preceded by the keyword ByRef. When a parameter is passed by reference, the procedure has direct read/write access to the passed variable. For more information, refer to Parameter Passing.

Here are a few examples of procedure declarations:

Procedure DoShowCharacters Global String sMessage
    // this procedure takes the string sMessage and outputs
    // each character separately with intervening commas.
    Integer iPos
    Integer iLength
    Move (Length(sMessage)) To iLength
    For iPos From 1 To (iLength - 1)
        Show (Mid(sMessage, 1, iPos)) ","
    Loop
    Showln (Mid(sMessage, 1, iLength))
End_Procedure

Procedure PrintValues Global Integer[] values
    ...
End_Procedure

// Assuming a struct type tCustomer has been declared somewhere else
Procedure PrintCustomer Global tCustomer cust
    ...
End_Procedure

Calling Global Procedures

Once a procedure has been declared, you can execute it in your program with a Send statement. The syntax of the Send statement is:

Send {procedure-name} [{param1} {param2}{paramN}]

Where:

  • {procedure-name} is the name of the previously declared procedure that you are executing.
  • {param1 .. N} are the parameters that you are passing to the called procedure. The number and order of parameters that you pass must match the list of parameters in the procedure's declaration. The parameters you pass can be either constants, variables, or expressions, so long as they are of comparable type to the matching parameter in the procedure declaration.

Examples

Send DoWriteCharacters "Some Character String"
Send DoWritePaddedString ("Message = " + sMessage) sPadCharacter

Parameter Passing and By Reference Parameters

The parameters of a procedure or function can be passed by reference, as well as by value.

When a parameter is passed by reference, the called procedure or function has direct read/write access to the passed variable. Changes made to the reference parameter inside the routine are reflected in the passed variable outside the method.

For more information, refer to Parameter Passing.