Thread: about fuction!

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    19

    about fuction!

    in the main i there is a part that will go to checking()
    i want after that will go back again to main

    Code:
    checking(){
    for(i = 0;i < 26; i++){
    		if (o_brief[n] == (i+1))
    		{
    			gotoxy(x[i], y[i]);
    			printf("  ");
    		}
    	}
    
    	for(i = 0;i < 26; i++){
    		if(c_brief[o_brief[n]-1]==z[i])
    		{
    			gotoxy(65,23);
    			printf("%8ld",z[i]);
    			sleep(2);
    			gotoxy(65,23);
    			printf("          ");
    			gotoxy(d[i], e[i]);
    			printf("*");
    		}
    	}
    }
    
    main(){
    	gotoxy(30,20);
    	printf("Choose your briefcase!");
    	gotoxy(10,23);
    	scanf("%d",&y_brief);
    
    	for(i = 0;i < 26; i++){
    		if(y_brief==1+i)
    		{
    			gotoxy(x[i], y[i]);
    			printf("  ");
    		}
    	}
    
    	gotoxy(29,20);
    	printf("      Round 1        ÚÄÄ¿    ");
    	gotoxy(29,21);
    	printf("                     ³  ³    ");
    	gotoxy(29,22);
    	printf("Open 6 briefcases!   ÀÄÄÙ    ");
    
    	do{
    	gotoxy(51,21);
    	scanf("%d",&o_brief[n]);
    	gotoxy(51,21);
    	printf("  ");
    
            checking();
    
    	resume here
    
    	sum[0]=sum[0]+c_brief[o_brief[n]-1];
    	n++;
    	}while(n<6);
    	r[0]=rand() % ((15 - 5) + 1) + 5;
    	offer[0]=((7020566 - sum[0]) / 20) * (r[0] / 100);
    
    	gotoxy(29,19);
    	printf("    banker offers you         ");
    	gotoxy(29,20);
    	printf("     ÚÄÄÄÄÄÄÄÄÄÄÄÄÄ¿          ");
            gotoxy(29,21);
    	printf("     ³             ³          ");
    	gotoxy(29,22);
    	printf("     ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÙ          ");
    	gotoxy(37,21);
    	printf("%7.0lf",offer[0]);
    	gotoxy(29,23);
    	printf("   [D]eal or [N]o Deal        ");
    
    	Resp=getch();
    	if(Resp==100){
    	closed(g=0);
    	}
    	else
    	briefcases2();
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Functions always return to where they were called from.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Just call main(). But even at that, I would not highly recommend a recursive main() function. Why not have main() call your game function. And have your game function be a callback type of function that has information given to it through main.

    Example:
    Code:
    struct game_variables
    {
      ... // I will leave this up to you
    }
    
    int in_game(struct game_variables *pvars)
    {
      ... // have it return zero to stop execution of the game or non-zero for other scenarios.
    }
    
    int main(void)
    {
      struct game_variables vars;
    
      ... // initialize and do whatever
    
      while(in_game(&vars))
      {
         // you could also store the return of in_game and process the return value if the returns
         // are designed to have any specific significance.
      }
    
      return 0;
    }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't call main, that's undefined behaviour. Some compilers may prepend code to the compiled main, that initialises globals etc, which must run only once.

    llinocoe, the functions you write behave the same as the functions you are already using such as printf, gotoxy, and scanf. You call them and then the next line of code executes. Your functions are no different.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Don't call main, that's undefined behaviour. Some compilers may prepend code to the compiled main, that initialises globals etc, which must run only once.
    For reasons that I cannot fathom, C actually allows main to be called recursively.

    It might be a mistake in the text of the standard, but then C99 talks about "a return from the initial call to the main function" (emphasis mine), in addition to not disallowing a recursive call of main, unlike the C++ standard.

    Consequently, in C, calling main recursively is bad style, but is still permitted.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aside from the "bad style" to call main, you should not design code that is mutually recursive [1] when there is not reasonable exit condition, and absolutely not to just create a loop in the code. If that's what you actually want, either split your function(s) into smaller functions, or otherwise re-arrange the code to work with a while/do-while/for loop style looping. High levels of recursion will run out of stack and crash, if you keep going for long enough.

    [1] Mutual recursion is where two functions call each other, so func1() calls func2() and func2() calls func1() and so on. There are situations when mutual recursion is (nearly) unavoidable, but those are really rather rare.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The two ways I do this is:

    1) Make a do while loop in main() that is large enough to put all your other function calls, inside it.

    something like this:
    Code:
    gameon = 1;
    do   {
    //other game function calls in here
    
       if(briefcases_left < 1 || deal == 1)
          gameon = 0;
    
    }while(gameon == 1);
    Or

    2) have main() call a driver or menu function, and put the same type of large game loop,
    into that function, like the above code fragment.

    Either way, you avoid recursively calling main() - which may be allowed, but is certainly a bad programming practice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Finding the mode, calling fuction
    By CaliJoe in forum C++ Programming
    Replies: 5
    Last Post: 04-09-2009, 08:50 PM
  2. Einstine calculator
    By Mach_ie in forum C Programming
    Replies: 2
    Last Post: 08-30-2003, 09:37 AM
  3. Enistine program
    By Mac_the vamp in forum C Programming
    Replies: 2
    Last Post: 11-11-2002, 10:56 AM
  4. check for number of days as a fuction?
    By scronge1 in forum C++ Programming
    Replies: 4
    Last Post: 06-24-2002, 06:35 AM
  5. just a fuction problem
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 04:01 AM