freepascal - Why i can use function name in pascal as variable name without definition? -


i wondered strange behaviour in free pascal functions, described in docs.

it said, following code compiled/executed successfully:

function test : integer; begin   test := 2; end;  begin   writeln(test()); end. 

but if use function name test in right side of equation, perform recursive loop.

so, pascal functions, 1 side, define variable name test , type of function return value integer. other side, can still call function (make recursive call using name).

why?! goal?

inside function's body there special variable name identical function name. used keep function result.

it introduced in original pascal syntax. later prevent inconveniences variable named result introduced , alliance previous one:

test := 2; := result + 3; // here = 5;  

so, now, test := 2; , result := 2; same.

in case of usage of function name @ right side of equation, interpreted variable, not function calls:

test := test + 1; // increments test value 

but still can call function recursively using brackets:

test := test() + 1; // recursion 

so, have 3 ways return value function (for example):

function test : integer; begin     test := 2; // function result = 2     result := 2; // same previous     exit(2); // sets function result 2 end exits end; 

it method use.


Comments