Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Recursion with main()

Question posted by: Sonia (Guest) on July 19th, 2005 04:57 PM
quick Question. Can main() function be called recursively. Anything wrong
with calling it recursively ?

Thanks


Artie Gold's Avatar
Artie Gold
Guest
n/a Posts
July 19th, 2005
04:57 PM
#2

Re: Recursion with main()
Sonia wrote:[color=blue]
> quick Question. Can main() function be called recursively. Anything wrong
> with calling it recursively ?
>[/color]
Recursive `main()' is *not* legal in standard C++.

<OT>It *is* legal in C, but *rarely* a Good Idea.</OT>

HTH,
--ag




--
Artie Gold -- Austin, Texas


John Harrison's Avatar
John Harrison
Guest
n/a Posts
July 19th, 2005
04:57 PM
#3

Re: Recursion with main()

"Sonia" <smach02@hotmail.com> wrote in message
news:UyO4b.4575$UF.2204@bignews2.bellsouth.net...[color=blue]
> quick Question. Can main() function be called recursively. Anything wrong
> with calling it recursively ?
>
> Thanks
>[/color]

It is specifically forbidden in C++ (for some reason that's never been
adequately explained to me). But there is nothing wrong with this

int pseudo_main()
{
...
pseudo_main();
}

int main()
{
return pseudo_main();
}

john



Kevin Goodsell's Avatar
Kevin Goodsell
Guest
n/a Posts
July 19th, 2005
04:58 PM
#4

Re: Recursion with main()
John Harrison wrote:
[color=blue]
> "Sonia" <smach02@hotmail.com> wrote in message
> news:UyO4b.4575$UF.2204@bignews2.bellsouth.net...
>[color=green]
>>quick Question. Can main() function be called recursively. Anything wrong
>>with calling it recursively ?
>>
>>Thanks
>>[/color]
>
>
> It is specifically forbidden in C++ (for some reason that's never been
> adequately explained to me).[/color]

Well, I don't know if this is adequate or even precisely accurate, but
my understanding is that implementations can stick all kinds of
initializations stuff in at the beginning of main(), and if that stuff
were executed more than once the results could be bad.

Of course, I don't see any good reason that an implementation could not
do precisely what you did in your reply. Perhaps this point makes this
explanation inadequate.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


 
Not the answer you were looking for? Post your question . . .
190,062 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors