Click here to Skip to main content
14,333,836 members
Rate this:
Please Sign up or sign in to vote.
See more:
can we call a function using recursion for main().......


like::::

void add();
int main()
{
statement.....
.
.
.
.

return add();
}
Posted
Comments
   
Where is your recursion?
--SA
CPallini 29-Jan-12 5:00am
   
That's not a recursive call. See Griff's answer for an example of.
Albert Holguin 29-Jan-12 19:33pm
   
Don't think your pseudo-code would even compile... void/int return types...
Rate this:
Please Sign up or sign in to vote.

Solution 1

That isn't showing any sign of recursion: to be recursive, it should directly or indirectly reference itself:

int factorial(int i)
    {
    if (i > 1)
        {
        return i * factorial(i - 1);
        }
    return 1;
    }
   
Comments
   
Exactly, a 5. (By the way, factorial is both most popular recursion example and one of the inappropriate case of recursion use -- no practical sense at all.)

I also answered on using recursion for main in my answer.
--SA
OriginalGriff 29-Jan-12 3:45am
   
:laugh:
I know - but it is a clean, clearly understood example. Most of the other examples need a fair amount of outside knowledge or explanation.
   
Sure, I understand and agree, it is, but I think it's useful to warn OP about it, so I did. I was my only purpose. Maybe OP is not really familiar with recursion, so it's better clarify it to avoid confusion.
--SA
CPallini 29-Jan-12 5:01am
   
My 5.
Rate this:
Please Sign up or sign in to vote.

Solution 2

The function main is just the function, like any other function. I'm pretty much sure you mean the function main is the one used for the entry point of some application. Serving as the entry point is the only thing which makes is special.

I does not prevent using it in recursion. Use it if you really want it. This will be unusual, and some say this is not the best programming style, but I would say, there is nothing too bad about it. If you can find a good use of it, of course. :-)

Please see my question in my comment to the question. Your code or pseudo-code does not show any signs of recursion. What you demonstrated is just forward declaration which has nothing to do with recursion. So it makes me not sure if you know what is it. So, just in case: http://en.wikipedia.org/wiki/Recursion_(computer_science)[^].

—SA
   
v2
Comments
Amir Mahfoozi 29-Jan-12 3:45am
   
+5
   
Thank you, Amir.
--SA
CPallini 29-Jan-12 5:01am
   
My 5 too.
   
Thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100