returning boolean did I do this right?

This is a discussion on returning boolean did I do this right? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; // Declare a function "bigger" which takes 2 double arguments and returns boolean. int ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    25

    returning boolean did I do this right?

    Code:
    #include <iostream>
    using namespace std;
    
    // Declare a function "bigger" which takes 2 double arguments and returns boolean.
     int bigger( double x, double y );
    int main()
    
    {
    int x, y, rb;
    
    cout << "Enter number" << endl;
    cin >> x;
    
    cout << "Enter number 2" << endl;
    cin >> y;
    
    rb = bigger ( x, y);
    
    
    
    
    cout << " The largest number is " << rb << endl;
    
    system("pause");
    return 0;
    }
    
    bigger ( double x, double y)
    {
    if ( x > y)
    
    	return x;
    		else 
    
    		return y; 
    }
    Last edited by Zerohero11; 11-07-2005 at 10:24 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Your trying to pass integers through a function that is prototyped to pass doubles.

    Also you need to give your function a date type when you make the prototype.

    Lastly, since your returning the value that you need in the function. Then you could put it right into a cout statement.

    Code:
     cout << "The bigger number is " << bigger(x,y) << "." << endl;
    Last edited by SlyMaelstrom; 11-07-2005 at 10:29 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Quote Originally Posted by SlyMaelstrom
    Your trying to pass integers through a function that is prototyped to pass doubles.

    Also you need to give your function a date type when you make the prototype.
    Ok I added the data type to "int" since the variables passed will be integers. I ran the program, and it ran fine. Did I excute the program right from your stand point?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Yes, I would just say since you prototyped doubles, you should pass it doubles. If the user knew it accepted doubles and tried to enter the first number as a double, then you'd get weird results.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    // Declare a function "bigger" which takes 2 double arguments and returns boolean.
    Where is the return value specified for your function, and what is that type?

    int bigger( double x, double y );

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If your assignment wants you to return a boolean, then no, you did not do that right.

    Your function should return bool (or maybe int if your instructor says so, but bool is the proper way to return a boolean). It should determine if the first parameter is bigger than the second, and return true if it is, false if it is not. It should not return the bigger number.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Quote Originally Posted by SlyMaelstrom
    Yes, I would just say since you prototyped doubles, you should pass it doubles. If the user knew it accepted doubles and tried to enter the first number as a double, then you'd get weird results.

    ah ok,

    so...
    Code:
    rb = bigger (double x, double y)
    correct?

    Thanks for your help btw!

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    I'm thinking he was just misunderstanding what booleen was and thinking it's just and either/or case. If this is for an assignment in a class and your teacher wants you to return a true or false, then you need to change some things.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Quote Originally Posted by Daved
    If your assignment wants you to return a boolean, then no, you did not do that right.

    Your function should return bool (or maybe int if your instructor says so, but bool is the proper way to return a boolean). It should determine if the first parameter is bigger than the second, and return true if it is, false if it is not. It should not return the bigger number.
    Ok so then just replace "x,y" with true, false correct?

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Nah, that's not your prototype man. You don't want datatypes there.

    Quote Originally Posted by Zerohero11
    Code:
    #include <iostream>
    using namespace std;
    
    // Declare a function "bigger" which takes 2 double arguments and returns boolean.
    
    double bigger( double x, double y );  // This is your function prototype
    int main()
    
    {
    double x, y, rb;
    
    cout << "Enter number" << endl;
    cin >> x;
    
    cout << "Enter number 2" << endl;
    cin >> y;
    
    rb = bigger ( x, y); // This is your function call
    
    cout << " The largest number is " << rb << endl;
    
    system("pause");
    return 0;
    }
    
    double bigger ( double x, double y) // This is your function definition
    {
    if ( x > y)
    
    	return x;
    		else 
    
    		return y; 
    }
    Sent from my iPadŽ

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Quote Originally Posted by Zerohero11
    Ok so then just replace "x,y" with true, false correct?
    No. You have to declare your function as a bool(or int) and return a bool.

    Code:
    bool isBigger(double x, double y) {
        if (x > y)
            return true;
        else
            return false;
    }
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Quote Originally Posted by SlyMaelstrom
    No. You have to declare your function as a bool(or int) and return a bool.

    Code:
    bool isBigger(double x, double y) {
        if (x > y)
            return true;
        else
            return false;
    }

    Code:
    #include <iostream>
    using namespace std;
    
    // Declare a function "bigger" which takes 2 double arguments and returns boolean.
    bool bigger( double x, double y );
    int main()
    
    {
    int x, y, rb;
    
    cout << "Enter number" << endl;
    cin >> x;
    
    cout << "Enter number 2" << endl;
    cin >> y;
    
    rb = bigger ( x, y);
    
    
    
    
    cout << " The largest number is " << rb << endl;
    
    system("pause");
    return 0;
    }
    
    bool bigger ( double x, double y)
    {
    if ( x > y)
    
    	return	true;
    		else 
    
    		return false; 
    }
    correct? I declared the function as "bool", and they return true/false.

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Except that rb is an integer, change that to a bool.

    ...and your cout statement will not read correctly either.
    Sent from my iPadŽ

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Your function is correct, but you need to change how you call it and what you do with the return value. Hopefully nobody will just give you the answer this time.

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Where is the return value specified for your function, and what is that type?

    int bigger( double x, double y );
    If you can't answer that question, you need to reread the chapter in your book on functions. You may also want to read this:

    Here is a very simple function:
    Code:
    #include <iostream>
    using namespace std;
    
    void showNumber()
    {
       cout<<100<<endl;
    }
    
    int main ()
    {
       showNumber();
    
       return 0;
    }
    Functions must specify a 'return type', and the return type precedes the function name. In the function above, there is no return value, and that is designated by 'void'. The function is 'called' in main() by this line:

    showNumber();

    That says, "please go find a function named showNumber, and execute the lines of code contained therein."

    Here is another example:
    Code:
    #include <iostream>
    using namespace std;
    
    void showNumber(int a)
    {
       cout<<a<<endl;
    }
    
    int main ()
    {
       showNumber(100);
       showNumber(25);
    
       return 0;
    }
    Now, instead of always displaying the same number, you can send the function a specific number, and the function will display whatever number you send it. Once again, the function does not return a value, so its return type is 'void'. You should note that the type of the number you send to the function must match the function 'parameter'. The function parameter above is "int a", which means the function is expecting an int type to be 'sent' to it. You send a value to a function like this:

    showNumber(100);

    To send a value to a function, you just put the value between the parentheses after the function name. The function parameter "int a" creates an integer variable named "a" which stores the value you send to the function--that's why the type of the value you send has to match the function parameter. Then, inside the function you can use the variable "a" by referring to it by name:

    cout<<a<<endl;

    Here is another example:
    Code:
    #include <iostream>
    using namespace std;
    
    int addTen(int a)
    {
       int answer = a + 10;
       return answer;
    }
    
    int main ()
    {
       int result1 = addTen(2);
       int result2 = addTen(5);
    
       cout<<"Here are the results: "<<result1<<" "<<result2<<endl;
       
       return 0;
    }
    This time the function returns a value. The return type preceding the function name must match the type of the value in the 'return statement'. The return statement replaces the function call in main(), e.g. addTen(2), with the return value. So, result1 is assigned 12, and result2 is assigned 15.

    To summarize:
    1) A function must list a return type before its name.
    2) The return type must match the type of the value in the return statement--if there is no return statement, then the type is 'void'.
    3) When you call a function, the value you send it must match the function parameter.
    4) The function call in main() is replaced by the function return value.
    5) Sometimes if a function doesn't have any parameters, like in the first example, "void" will be used for the parameter. For instance you might see the function in the first example written like this:
    Code:
    void showNumber(void)
    {
       cout<<100<<endl;
    }
    (void) is equivalent to ( )

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  2. Passing / Returning a Boolean (Help)
    By EDB in forum C++ Programming
    Replies: 5
    Last Post: 02-17-2008, 01:56 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Working with boolean...
    By CompiledMonkey in forum C Programming
    Replies: 4
    Last Post: 11-03-2003, 09:39 AM
  5. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM

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