very basic question about functions

This is a discussion on very basic question about functions within the C++ Programming forums, part of the General Programming Boards category; hello everyone! im starting to learn programming and c++. im having a simple question about function types. aren't function type ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    3

    very basic question about functions

    hello everyone!

    im starting to learn programming and c++.

    im having a simple question about function types.

    aren't function type chosen to reflect return type?
    so if my function return some
    -text, i should use string?
    -numbers, i should use int?
    what if it return a bit of both?

    and in the example below, the fuction return a bunch of text and numbers, why is it an "int" then??

    thank you
    -a very confused and discouraged beginner

    Code:
    #include <iostream>
    using namespace std;
    
    int bottles_of_beer ()
    {
        int bottle_amount = 99;
        while (bottle_amount > 0)
        {
    
        cout<< bottle_amount << " bottles of beer on the wall, " << bottle_amount << " bottles of beer!\n";
        cout<< "take one down, pass it around! " << --bottle_amount << " bottles of beer on the wall!\n\n";
    }
    }
    int main()
    {
        return bottles_of_beer();
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    im having a simple question about function types.

    aren't function type chosen to reflect return type?
    Functions aren't types. Functions do have a return value, sometimes. What is far more important in function design is what the function is supposed to do. Let's go to your code.

    Code:
    int bottles_of_beer ()
    {
        int bottle_amount = 99;
        while (bottle_amount > 0)
        {
     
        cout<< bottle_amount << " bottles of beer on the wall, " << bottle_amount << " bottles of beer!\n";
        cout<< "take one down, pass it around! " << --bottle_amount << " bottles of beer on the wall!\n\n";
        }
    }
    int main()
    {
        return bottles_of_beer();
    }
    Your bottles_of_beer() function supplies the return variable of main(). This is actually probably the worst option because the return variable of main() means something.
    If main() returns 0, everything is OK.
    If main() returns any other number, it implies an error in program termination.
    So it's actually quite restrictive what you're doing.

    Now the other thing is, if you do return a value, you should plan on using it. A far more simple function like this:
    Code:
    double fahrenheit_to_c (double degree) {
        return 5.0 * (degree - 32.0)  / 9.0 ;
    }
    That has a useful return value, but sometimes there just isn't one. Other than exiting main(), there is no reason for bottles_of_beer to return anything. That is the fix I recommend.

    But say that there was a reason you should return more than one thing, you actually have a lot of options available to you. Non-const references, if you know what those are, allow you to change a thing in a function pretty safely. You could also return a pair, or a tuple, if there were 2 or 3 things you needed to return, respectively. Better still is incorporating these separate values into a user-defined data type.
    Last edited by whiteflags; 12-19-2012 at 10:51 PM. Reason: don't mind my stylistic changes...

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Yeah, return std::string for text. Use either int or double for numbers, depending on what you need. unsigned (aka 'unsigned int') is also useful, as long as you don't decrease it below 0.

    You cannot return two parameters from a function directly, but there are a few ways around this:
    1) Pass the variables to store the result as a reference, and write to them in your function.
    2) Return a structure or class that contains all the data you need to return.
    3) Return an std::pair, or other generic container with the data you need. For example a std::pair<string, int> will hold an string and an integer

    The code you posted is not valid. bottles_of_beer() does not have a return statement, and can reach the end of the function and not know what to do. In this case it will likely return garbage, but technically the behavior is not allowed in c++. A good compiler will give you a warning. (It won't give you an error because it can't prove that the last line of the function will be reached, but it is an error here)
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    hello whiteflags.

    i succesfully understood at least some of what you wrote.
    i think i wasn't too clear about my level of knowledge. I hope to one day understand it all
    But thank you for the very detailed answer!!

    i guess i was confused with what "return" do.
    so it confused the question i had.
    (I shouldn't have added "return" before the function bottles_of_beer() in main.)

    nevertheless, my question still remain.

    why the function bottles_of_beer is a int?
    Code:
    int bottles_of_beer(){...}
    thank you so much

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    It shouldn't be. The code is wrong. It should be void.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    why the function bottles_of_beer is a int?
    In my opinion, it should not return type int but void, because that is C++ speak for "return nothing". I think in the original code the intent was to return bottles after the loop finished. But there is no point to that either, is there, especially since we decided that returning main()'s return value is a bad idea.

    [edit] Ninja'd, really? [/edit]

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    aahhhhhhhhhhhhhh...
    thank you so much. things make alot more sence now.

    i was reading about that void thing a few chapters ago but didn't understand. (i was thinking "what do you mean void? it write all that text and do all those things??")
    sorry, it must have been a really silly question for you two but your answers were very useful to me!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Question about Passing Values between Functions
    By beeson76 in forum C Programming
    Replies: 10
    Last Post: 05-08-2012, 04:38 PM
  2. Basic problem using for loops w/ functions. help :(
    By rensan in forum C Programming
    Replies: 8
    Last Post: 04-22-2012, 11:05 AM
  3. A basic Q about calling templated Functions
    By manasij7479 in forum C++ Programming
    Replies: 8
    Last Post: 06-10-2011, 04:29 PM
  4. Basic use of exec functions
    By Mysrt in forum C++ Programming
    Replies: 0
    Last Post: 10-13-2009, 04:53 PM
  5. handling basic math functions
    By MyDestiny in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2005, 01:12 PM

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