Procedures and Functions

Procedures are used to specify the set of actions to modify the external (referring to them) program environment. An example of such modification is a new variables value syntax, save data to an external file, and so on. The procedure is called by specifying its name in the program where code specified in the procedure should be executed. Procedure declaration syntax:

Sub <Name>[(<formal parameters>)];
Begin
    
End Sub <Name>;

Functions are used to specify an algorithm for some value calculation. In this case functions are similar to expressions, which also calculate values. A function must return some value. To do this, use the Return statement with this function result parameter. Use the Return statement without parameters in the procedure body to exit the procedure. Function declaration syntax:

Function <Name>[(<formal parameters>)]: <value type>
Begin
    
Return <value>
End Function <Name>;

All constants, variables, procedures and functions described in procedure or function body are local for it.

In addition to local objects and formal parameters, the objects defined in the external procedure or function block are also visible in the body, except for the objects, which have the same identifiers as local objects and procedure or function parameters have.

The use of procedure or function identifier within its body results in procedure or function recursive call.

A procedure or function prior description is absent. Any procedure or function can be called if it is described in the current code block or in any of the enclosing code blocks.

Sub Main;
    
Sub SimpleSub(a, b: Integer; var c: Integer);
    
Begin // Procedure text start
        c := a + b;
    
End Sub SimpleSub; // Procedure text end
Var
    a: Integer; 
// Declare variables for the Main procedure
Begin
    SimpleSub(
13, a); // Call procedure
    Debug.WriteLine(a); // Display result obtained in the a variable
End Sub Main;

Sub Main;
    
Function SimpleFunc(a, b: Integer): Double;
    
Var // Declare variables for function
        c: Double;
    
Begin // Function text start
        c := (a + b) / 2;
        
Return c; // Result returned by function
    End Function SimpleFunc; // Function text end
Var // Declare variables for the Main procedure
    c: Double;
Begin
    c := SimpleFunc(45); // Use function
End Sub Main;

See also:

Fore Language Guide | Formal Parameters | Classes and Objects | Interfaces