[fpc-pascal] inline function vs recursive function

ik idokan at gmail.com
Tue Feb 7 11:09:21 CET 2012


On Mon, Feb 6, 2012 at 21:58, Sven Barth <pascaldragon at googlemail.com>wrote:

> 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).
>

ā€ˇThanks Sven, then I think I'll work with the nested function as a "normal"
function rather then inline or recursive one.


>
> Regards,
> Sven
>
>
Ido


>  ______________________________**_________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.**org<fpc-pascal at lists.freepascal.org>
> http://lists.freepascal.org/**mailman/listinfo/fpc-pascal<http://lists.freepascal.org/mailman/listinfo/fpc-pascal>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20120207/d13c742a/attachment.html>


More information about the fpc-pascal mailing list