472,958 Members | 2,339 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

inline recursive functions

Dear friends,

If we declare a recursive function as 'inline' then does it actually
convert to an iterative form during compilation ( the executable code
is iterative)?

Is it possible ?

Please reply.

Nov 15 '05 #1
7 6074
Aloo wrote:
Dear friends,
We are? Nice to know. I'll call upon that.
If we declare a recursive function as 'inline' then does it actually
convert to an iterative form during compilation ( the executable code
is iterative)?
The implementation/compiler has only to make the program behave
as if you have recursion. The inline keyword only is a recommendation
for the compiler but does not change the semantics.

Is it possible ?
Certainly. There may be cases where the compiler effectively makes
your program output the solution to a problem you tackled with a
lengthy program. As long as there is no visible difference, the
compiler may do pretty much everything, even that.

BTW: Code generators outputting C code often are in a better
position to recognize and optimize things like that than compilers.

Please reply.


Done.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #2
Michael Mair wrote:
Aloo wrote:
If we declare a recursive function as 'inline' then does it actually
convert to an iterative form during compilation ( the executable code
is iterative)?

The implementation/compiler has only to make the program behave
as if you have recursion. The inline keyword only is a recommendation
for the compiler but does not change the semantics.

Is it possible ?


Certainly. There may be cases where the compiler effectively makes
your program output the solution to a problem you tackled with a
lengthy program. As long as there is no visible difference, the
compiler may do pretty much everything, even that.

BTW: Code generators outputting C code often are in a better
position to recognize and optimize things like that than compilers.


Yes, but then you're presumably talking about code generators generating
from something that isn't C, which renders the issue moot.

I've seen C compilers that do, in fact, optimize certain tail-recursive
functions to iterative ones and can subsequently inline the result.
Doing it in all cases requires global code analysis and is unattractive
or unfeasible, depending on your platform. This is presumably where the
generators come in: if they generate all code, they could slip in such
analysis while they're at it.

This optimization tactic isn't widely seen in compilers because it's too
clever to pay off in practice; actual C code doesn't use tail-recursive
functions often (of the kind that readily admits optimization), if the
inlining is critical a programmer would never rely on a compiler's
ability to unfold tail recursion, and if the inlining is not critical,
who cares?

Compare this with Scheme compilers, which are *required* to optimize
tail recursion in all cases -- but then, this is because recursion is
the bread and butter of functional languages, and because such
optimization is actually feasible.

S.
Nov 15 '05 #3
In article <11**********************@g44g2000cwa.googlegroups .com>,
"Aloo" <an************@rediffmail.com> wrote:
Dear friends,

If we declare a recursive function as 'inline' then does it actually
convert to an iterative form during compilation ( the executable code
is iterative)?


"inline" is a hint to the compiler, telling it that you wish calls to
this function to be as fast as possible, even if this means growing code
size. Everything else is up to the compiler.
Nov 15 '05 #4
Dear Michael ,

The code compiles succesfully, but does the compiler know at compile
time at how many places to insert the code, since the number of times
the code executes depends upon the input at runtime.

Is there any method to Actually debug the executed code and see for
myself whether it is recursive or iterative ?

regards

Aloo

Nov 15 '05 #5
Please quote enough context when replying to a message -- otherwise,
people who could give you a good answer if they had the necessary
context cannot do so.

Aloo wrote:
[inlining of recursive functions into iterations?]
The code compiles succesfully, but does the compiler know at compile
time at how many places to insert the code, since the number of times
the code executes depends upon the input at runtime.
Well, there are three possibilities for the special case of everything
known at compile time:
1) The compiler ignored the "inline". This is easiest, thus the most
probable course.
2) The compiler replaced the body of the function by an equivalent
iteration and had to inline only once.
3) The compiler figured out the necessary recursion depth for each and
every call and inserted appropriately many calls. This is the most
unlikely.

In principle, depending on the semantics of your function, all three
are possible for runtime inputs, too, but the latter becomes even
more unlikely (but could still be applicable if the inlined function
is essentially a contraction and there are limits on the processed
type).
The answer to your question depends completely on your compiler and
the compiler settings.
Is there any method to Actually debug the executed code and see for
myself whether it is recursive or iterative ?


Usually not. Many compilers do not generate the same code in debug
mode. Even if they did, the debugger may then seemingly step over
the call of the inlined function.
Certainty can only be gained from looking at the compiler output.
Most compilers give you a chance to do so.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #6
In article <3s************@individual.net>,
Michael Mair <Mi**********@invalid.invalid> wrote:
Please quote enough context when replying to a message -- otherwise,
people who could give you a good answer if they had the necessary
context cannot do so.

Aloo wrote:
[inlining of recursive functions into iterations?]
The code compiles succesfully, but does the compiler know at compile
time at how many places to insert the code, since the number of times
the code executes depends upon the input at runtime.


Well, there are three possibilities for the special case of everything
known at compile time:
1) The compiler ignored the "inline". This is easiest, thus the most
probable course.
2) The compiler replaced the body of the function by an equivalent
iteration and had to inline only once.
3) The compiler figured out the necessary recursion depth for each and
every call and inserted appropriately many calls. This is the most
unlikely.


Another one: The compiler can generate a function body which inlines up
to a depth of say four, then calls that function body recursively if
necessary. Doing this would eliminate 75% of all the function calls,
while still easy to implement.
Nov 15 '05 #7
Christian Bau wrote:
In article <3s************@individual.net>,
Michael Mair <Mi**********@invalid.invalid> wrote:

Please quote enough context when replying to a message -- otherwise,
people who could give you a good answer if they had the necessary
context cannot do so.

Aloo wrote:
[inlining of recursive functions into iterations?]
The code compiles succesfully, but does the compiler know at compile
time at how many places to insert the code, since the number of times
the code executes depends upon the input at runtime.


Well, there are three possibilities for the special case of everything
known at compile time:
1) The compiler ignored the "inline". This is easiest, thus the most
probable course.
2) The compiler replaced the body of the function by an equivalent
iteration and had to inline only once.
3) The compiler figured out the necessary recursion depth for each and
every call and inserted appropriately many calls. This is the most
unlikely.


Another one: The compiler can generate a function body which inlines up
to a depth of say four, then calls that function body recursively if
necessary. Doing this would eliminate 75% of all the function calls,
while still easy to implement.


Thank you -- did not think of that :-)

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Chris Mantoulidis | last post by:
I am not clear with the use of the keyword inline... I believe you add it do a function when you implement the function inside the header file where the class is stored... But is that all? What...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
7
by: Srini | last post by:
Hello, Rules for inline functions say that they have to be defined in the same compilation unit as their declarations. For class member functions this means that the inline member functions must...
43
by: Patrick Laurent | last post by:
Hello I have a program with many many inlined template functions It is essential for the execution speed that every (or almost every) function marked as inlined, becomes really inlined by the...
3
by: Aloo | last post by:
Let's say I have a function func() { ............ func(); .......... }
4
by: Peter Olcott | last post by:
What is the best way to handle this?
7
by: sam_cit | last post by:
Hi Everyone, I have a function declared as inline and i was wondering as to how it would be expanded by the compiler sample(int x) { if (x>0) sample(--x); printf("%d",x); }
1
by: Sylvain Audi | last post by:
I know that declaring a function as inline does not imply the function will actually be inlined (e.g. recursive functions, etc...). In that case, where would the function be instanciated? My...
5
by: Digital Puer | last post by:
I got this on an interview: Is it possible to write inline recursive functions? I said yes, but there is no guarantee that the compiler will definitely inline your code even if you write...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.