Recursion of main()

This is a discussion on Recursion of main() within the C++ Programming forums, part of the General Programming Boards category; Is it not possible to recursion in the main function? Is the only alternative a loop? any help is appreciated....

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    111

    Recursion of main()

    Is it not possible to recursion in the main function? Is the only alternative a loop? any help is appreciated.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    It's possible to use recursion on any function you want, including main().
    If you understand what you're doing, you're not learning anything.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is it not possible to recursion in the main function?
    Yes, a recursive main is not legal in C++. It's legal in C, but typically reserved for spectacularly awful code.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    you can of course do this
    Code:
    int rmain(int argc, char *argv[]) {
    // recursive whatever goes here
    }
    int main(int argc, char *argv[]) {return rmain(argc,argv);}
    I am really not sure why you would want to do this though. You can also kind of "call main" by using a system call to launch your program again, presumably with new parameters. This borders on being either far to clever for your own good, or having a really basic misunderstanding of what's going on.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    ok thanks for the heads up. I just want the program tp restart, and I though using recursion to jump back to the beginning would work.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I just want the program tp restart, and I though using recursion to jump back to the beginning would work.
    Good recursion solves a similar, smaller problem each time. If you want to do the same thing over and over, use a loop.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Ok, and instead of starting another thread, may i ask somethingthat is different. I am used to using "getchar();" for waiting for input, but I have seen cin.get(); used. is cin.get any more (or less) proper to use then getchar()?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is cin.get any more (or less) proper to use then getchar()?
    cin.get is the "C++" way of doing things and getchar is the "C" way. If you otherwise use C++ iostreams then you would be wise to use cin.get instead of getchar. There can be subtle sync issues when mixing C and C++ I/O.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    ok thanks.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Quote Originally Posted by Prelude
    >Is it not possible to recursion in the main function?
    Yes, a recursive main is not legal in C++. It's legal in C, but typically reserved for spectacularly awful code.
    Huh. Good to know!
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    I prefer to use the word "clever" instead of "awful", but you get the general idea.

    It's preferable to use a loop instead of recursive functions for many reasons. One is the overhead associated with function calls. Another is that if you run the recursive loop many times (we're talking at least a few hundred), functions which use a lot of variables will slowly eat up a computer's stack space. There's also the overhead when you roll back up the stack, when you're done with the recursive loop.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 10-15-2008, 03:38 AM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. Replies: 6
    Last Post: 09-23-2008, 07:27 PM
  4. differences between int main and void
    By louis_mine in forum C Programming
    Replies: 6
    Last Post: 09-25-2004, 06:49 AM
  5. Defining main function!
    By alvifarooq in forum C++ Programming
    Replies: 8
    Last Post: 09-19-2004, 02:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21