Thread: Functions

  1. #1
    Unregistered
    Guest

    Exclamation Functions

    I have senn that it is possible to make function return a nummber.
    Like 1 for TRUE or 0 for FALSE. How to do that.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    yes:
    return 0;

    or
    return 1;

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Code:
    // For improving code readability constants are recommended
    typedef enum
    {
        FALSE,
        TRUE
    } boolean;
    
    boolean function ()
    {
        boolean retval;
    
        ....
    
        // Now return the value
        return retval;
    }
    Note that the most recent ANSI C standard has standard boolean-type which you can use when including stdbool.h. I don't know if it is in the ANSI C++ standard.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    you can just go:

    Code:
    BOOL function()
    {
         return TRUE;
         // OR return FALSE;
    }
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I thought bool was in the standard so there is no need for enums, typdefs, #defines, consts or anything else I cant think of. You can usually just do this:

    Code:
    bool someFunction( bool flag )
    {
          return (flag ? true : false);
    }

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    15
    Hope this helps too:

    Code:
    #include <iostream.h>
    
    bool something(int);
    
    void main(void)
    {
    	int testing;
    	bool answer;
    
    	cout << "Enter a number (other than zero) \n";
    	cout << "and i'll tell you if it positive or negative: ";
    	cin >> testing;
    	
    	answer = something(testing);	// 4. you need to capture the return from the something function.
    
    	if (answer)
    	{
    		cout << "Your input was a positive number.\n";
    	}
    	else
    	{
    		cout << "Your input was a negative number.\n";
    	}
    }
    
    bool something(int num)		//  1. you need (bool) here to tell the function it will return one.
    {
    	if (num > 0)
    	{
    		return true;		//  2. this will return true(1) if the input is greater than zero
    	}
    	else
    		return false;		//  3. this will return false(0) if the input is less than zero
    }
    Hope i didn't mess up somewhere. Did it at work.... with no compiler

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    15
    Oh and one more thing. I love this place and I learned a lot here just by reading. You could too if you just ask, but If you are planning on asking, you should at least register your name. I would hate to think of this board like a place that people come to get help but don't even care enough to let us know who we are helping. Sorry, i'm a beginner too just like you and it just doesn't look right when there a whole bunch of "Unregistered" names out there.


    .... Sorry for my blabering ..... i'll go back to work now ...

    Keep up the programming.

  8. #8
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    >void main(void)

    Ack! Call Prelude!
    Use:

    int main (void) {

    //blablabal code blablabla

    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM