Free Pascal with Terry
Functions Help
A Function is another type of subroutiine in Pascal. A simple Function can be declared below the main loops var section and above its logic section. A very simple Function can be declared like so:
Function MyFunctionName(): LongInt;
Var
AnInteger: LongInt;
Begin
// Valid Pascal Statements go below here
...
MyFunctionName := AnInteger;
End;
Note in last step of a function, the function name must be set to the type that the function is declared as (the type after the column is assigned to the function name) . Alternatively, one could issue a return statement
as the last statement with the return variable in parenthesis. The return variable must be of the same type as the function.
Then in the calling routine, a valid variable will be assigned the Integer in this case from the function, like so:
MyIntValue := MyFunctionName();
Any additional variables needed inside the procedure can be declared right below the procedure header and before the “Begin statement, like so:
Function MyProcedureName(): LongInt;
Var
MyInt: LongInt;
MyFlt: Double;
...
Begin
// Valid Pascal Statements go below here
...
End;
Functions need not be declared as just returning Integer types, but can return any valid Pascal types, such as Floats, Strings and any special types you have declared in the “Type” section. A Function header of type
Double:
Function MyFunctionName(): Double;
So, a Function is also 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 variables, like so:
Function MyFunctionName(MyInt: LongInt): LongInt;
In this example when calling MyFunction, there has to be a LongInt from where you call MyFunctionName, but the name of the LongInt need not be the same. So you can use the same function on multiple different variables as
long as they are the right type.
You can define any valid type as a variable in the Function 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 function like so:
Function MyFunctionName(MyRec: MyRecordType): LongInt;
You can pass more than one variable at a time to a function, like so:
Procedure MyNewFunction(i: LongInt; MyFloat: Double): LongInt;
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.
Functions are mostly used to calculate or format something. In the below example is a function that will calculate the area of a circle.
Function AreaOfCircle(Radius: Double): Double;
Var
Area: Double;
Begin
Area := Pi() * Radius * Radius;
AreaOfCircle := Area;
End;
Then the calling routine might have:
MyArea := AreaOfCircle(3.0);
WriteLn('Area of My Circle: ', MyArea:3:6);
And would display:
Area of My Circle: 28.274334