I think newbs find it easier to understand a function when you use a conditional to assign the result.
The following boolean function is going to generate a random number out of 20. If it equals 10 then the function results true.
Simba Code:
function isRandomNumber10(): boolean;
begin
end;
The integer variable 'x' is assigned a value out of 20 (between 0..19). If x equals 10 then the function results true.
Simba Code:
function isRandomNumber10(): boolean;
var
x: integer;
begin
x := random(20);
if x = 10 then
result := true;
end;
This is the same as:
Simba Code:
function isRandomNumber1(): boolean;
var
x: integer;
begin
x := random(20);
result := x = 10; // result true if x = 10
end;
Functions don't have to be booleans. Here is an example of a function returning an integer as the result.
Simba Code:
function timesByTen(number: integer): integer;
begin
result := number * 10;
writeLn(toStr(number) + ' multiplied by 10 equals: ' + toStr(result));
end;
This function takes a number, multiplies it by 10, and returns the new number as the result. So the output of this little program:
Simba Code:
program new;
function timesByTen(number: integer): integer;
begin
result := number * 10;
writeLn(toStr(number) + ' multiplied by 10 equals: ' + toStr(result));
end;
begin
timesByTen(10);
end.
would be:
Code:
Compiled successfully in 250 ms.
10 multiplied by 10 equals: 100
Successfully executed.