[fpc-pascal] inline function vs recursive function
Sven Barth
pascaldragon at googlemail.com
Mon Feb 6 20:58:06 CET 2012
On 06.02.2012 19:51, ik wrote:
> Hello,
>
> Let's say I have a function that some of it's code (or all) can be used
> in more then one location inside the function itself.
> I can make a nested function with the code and set it to inline, make it
> normal, or make a recursion with it.
>
> What's the best practice in your opinion to take, and why ?
Recursion won't help you here, because recursion is something like the
following:
function factorial(aValue: LongInt): LongInt;
begin
if aValue <= 1 then
Result := 1
else
Result := factorial(aValue - 1) * aValue;
end;
Whether you inline the function or not depends on whether it's a complex
function or not. Something like
function foo(aValue: LongInt): LongInt;
begin
Result := aValue * 6 + 5 - SomeParam;
end;
can definitely be declared inline. You just need to keep in mind that
when inlining a function the function call will be replaced by the
function's/procedure's body so the code size will increase (no, you
don't need to pay attention for duplicate variable names).
Regards,
Sven
More information about the fpc-pascal
mailing list