Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
can we call a function using recursion for main().......


like::::

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

return add();
}
Posted
Comments
Sergey Alexandrovich Kryukov 29-Jan-12 3:26am    
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...

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

C++
int factorial(int i)
    {
    if (i > 1)
        {
        return i * factorial(i - 1);
        }
    return 1;
    }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Jan-12 3:37am    
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.
Sergey Alexandrovich Kryukov 29-Jan-12 3:51am    
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.
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
 
Share this answer
 
v2
Comments
Amir Mahfoozi 29-Jan-12 3:45am    
+5
Sergey Alexandrovich Kryukov 29-Jan-12 3:51am    
Thank you, Amir.
--SA
CPallini 29-Jan-12 5:01am    
My 5 too.
Sergey Alexandrovich Kryukov 29-Jan-12 5:09am    
Thank you.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900