Free Pascal with Terry

Procedures Help



A procedure is a subroutiine in Pascal. A simple Procedure can be declared below the main loops var section and above its logic section. A very simple procedure can be declared like so:

Procedure MyProcedureName(); Begin // Valid Pascal Statements go below here ... End;

Any additional variables needed inside the procedure can be declared right below the procedure header and before the “Begin statement, like so:

Procedure MyProcedureName(); Var MyInt: LongInt; MyFlt: Double; ... Begin // Valid Pascal Statements go below here ... End;

So, a Procedure is kind of like a program within a program. You may also have a Type and Const section.

You can pass variables to a procedure by declaring them in between the parenthesis, like so:

Procedure MyProcedureName(MyInt: LongInt);

In this example when calling MyProcedure, there has to be a LongInt from where you call MyProcedureName, but the name of the LongInt need not be the same. So you can use the same procedure on multiple different
variables as long as they are the right type.

You can define any valid type as a variable in the Procedure deceleration, these can be any of the Integer, float or string types, plus any special types you have declared in the “Type” section. Therefore if you have declared a record in the Type section like this:

Type MyRecordType = Record LastName: String[30]; FirstName: String[25]; End;

You can then pass a MyRecordType to a procedure like so:

Procedure MyProcedureName(MyRec: MyRecordType);

You can pass more than one variable at a time to a procedure, like so:

Procedure MyNewProcedure(i: LongInt; MyFloat: Double);

Now whenever you call this procedure, there must be a LongInt and a Double in the order defined inside the parentheses separated by a comma.

All of the above procedure decleration are examples of passing variables by value. Whenever you pass a variable by value, you are in effect passing a copy of that variable to the procedure and any changes made to that
variable will not be seen by the calling routine.

If you want the calling routine to see the change, you must call it by reference. To do that, you must put the word Var in front of the variable, like so:

Procedure MyProcedureName(Var MyInt: LongInt);