Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-02

inline function vs recursive function

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ik

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 ?

Thanks,
Ido

Everton Vieira
to ik

You can make a function inside the function.

Like this:

function Test: Integer;
var
  i: Integer
  procedure IncI(aIncI: Integer);
  begin
    i := i +aIncI;
  end;
begin
  i := 1;
  while i <= 100 do
    IncI(i);
  Result := i;
end;

This make an geometric progression in i.

ik wrote 12 lines
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 ? Thanks, Ido _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Sven Barth
to ik: Let's say I have a function that some of it's code (or all) can be used in more then one…
ik wrote 8 lines
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

ik
to Sven Barth
Sven Barth wrote 36 lines
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.

Show quoted text 5 lines
Regards, Sven

Ido

______________________________**_________________ fpc-pascal maillist - http://lists.freepascal.org/**mailman/listinfo/fpc-pascal<http://lists.freepascal.org/mailman/listinfo/fpc-pascal>
Guionardo Furlan
to ik

Inline: http://www.freepascal.org/docs-html/ref/refsu64.html Recursion: http://wiki.freepascal.org/Recursion

2012/2/7 ik :

Show quoted text 61 lines
On Mon, Feb 6, 2012 at 21:58, Sven Barth 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  -   > http://lists.freepascal.org/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Timeo hominem unius libri Cogito ergo sum - Carpe diem []s Guionardo Furlan http://guionardo.blogspot.com http://www.guionardofurlan.com.br