Thread: function return

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    function return

    If I call a Play(); function from main, and that function returns a 1 for a win a and a 0 for a lose. How can I assign those results to a variable. The main program needs to recognize the result in order to print the proper message to the screen.

    I'm assuming that I need to assign the returns from the Play function to a variable and assign that variable to the function call within main to be able to check the result and display a message.

    result = Play();

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Yes, that should work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by Salem View Post
    Yes, that should work.
    Code:
    result = Play(answer);
    
    
    int Play(int answer)
    {	
    	int numGuesses = 0;	
    	int guess = 0;
    	int result = 0;
    	do
    	{	
    		guess = GetGuess();   
    		
    			CompareGuess(guess, answer);
    				if(guess == answer)
    				{
    					result = 1;
    				}
    					return result;
    		
    					numGuesses = numGuesses +1; 
    	}
    	while( numGuesses < MAXGUESSES);
    		
    	if (guess != answer)
    				{
    					result = 0;
    				}
    					return result; 
    }
    Does this look right??

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Does it work?

    With improved indentation, and some additional notes:

    Code:
    result = Play(answer);
    
    int Play(int answer)
    {   
        int numGuesses = 0; 
        int guess = 0;
        int result = 0;                   // Variable not needed (read on)
        do
        {   
            guess = GetGuess();
            CompareGuess(guess, answer);  // Are you comparing the guess with
                                          //   the answer here?
    
            if(guess == answer)           // ... or here?
            {
                result = 1;               // You can just return 1 here...
            }
    
            return result;                // ...instead of using this variable
             
            numGuesses = numGuesses +1; 
        } while( numGuesses < MAXGUESSES);
             
        if (guess != answer)              // if the number of guesses have already
        {                                 //   been used up with no correct result,
            result = 0;                   //   you don't need this extra check here.
        }                                 //   Just return 0 after the loop.
    
        return result;                    // Then you wouldn't need this line, either.
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > Does this look right??
    There's this marvellous tool called a "compiler" sitting right there in your toolbox.

    Posting "will this work" is a pointless endeavour.

    However, trying things, analysing things and only then posting when you get stuck (along with a more specific question) will get you much further in the long run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Because of line 19, there is no way to ever execute lines 21 & 22.
    Perhaps you means to put the return inside the preceeding curly braces?
    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"

  7. #7
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by iMalc View Post
    Because of line 19, there is no way to ever execute lines 21 & 22.
    Perhaps you means to put the return inside the preceeding curly braces?
    Thanks for the help. Everything works now. I appreciate the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function return
    By xboss in forum C Programming
    Replies: 10
    Last Post: 10-03-2012, 01:38 PM
  2. What would this function return
    By ganesh bala in forum C Programming
    Replies: 2
    Last Post: 01-30-2009, 02:02 AM
  3. Function return value
    By ursusman in forum C Programming
    Replies: 9
    Last Post: 11-28-2007, 07:17 AM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  5. return value in function
    By threahdead in forum C Programming
    Replies: 2
    Last Post: 10-18-2002, 06:01 PM