returns

This is a discussion on returns within the C++ Programming forums, part of the General Programming Boards category; I'm learning C++ and am currently looking at classes. The whole time I've been learning I have been kind of ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    7

    Question returns

    I'm learning C++ and am currently looking at classes. The whole time I've been learning I have been kind of lost as far as return values go. Everything else makes perfect sense but I just don't understand what return 0; means or what return NAMEofVARIABLErightHERE does. Can some one give me an explanation of these for an idiot? Oh, and I already know that they go at the end of a function...

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    What a return statement does has nothing to do with classes. In general, a function does something. Sometimes, it returns a result when it is done. In order to return a result, you type return _result_; with _result_ being whatever you want. If a function is supposed to return an integer which is an error code, often it returns 0 when the function succeeded. If the function returns a variable, it is actually returning the value of that variable. This way, the code that called the function gets its result.

    Now, it sounds like you might have more of a question about how functions (and their return statements) are used in classes, but if you do then you'd have to be more specific.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    7
    Well, I just stated that I was learning about classes -- no reason in particular. Thanks. So if I had a function that calculated some problem and the answer was going to be assigned to an already declared variable named 'int john' I would say 'return john'?

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Code:
    int CalcSomeProb()
    {
      int john = 15 - (4 + 1);
      return john;
    }
    
    int main()
    {
      int bob = 3;
      bob = CalcSomeProb(); // Now bob is 10.
    }
    Does that make sense?

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    7
    So is it like after you have written all the code for what that function does the result is what you return so when you call on that function what you returned is basically what you are calling for?

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Most of the time, your functions will be exactly like jlou's example. Your function will calculate some number, and return that "answer" back to main(), or whatever function called that function. (Your non-main functions can call other functions.)

    The C++ standard says that main() should return 0; if the program terminates normally. If something "unexpected" happens, main() should return non-zero. The value returned by main() is returned to the operating system.

    Oh, and I already know that they go at the end of a function...
    Well... most of the time yes. And, your function will end when the return statement is execuited. But, sometimes you'll have a loop in your function, with an if-statement that says if-something, then return. The if-statement and the associated return may not be at the end of the function. Also with if-statements, you can have more than one return statement. Of course, only one return statement be execuited each time the function is called, because once it returns, the "program-flow" is no longer inside the function.

    Note that a function can only return ONE value. If your function has to change more than one variable, you have to use pointers or references.

    NOTE: Classes are generally considered an advanced (or intermediate) topic. Functions are fundamental to C/C++ programming. Everything in C++ is done in with functions. I don't want to tell you how to learn/study, but I recommend that you understand functions "inside and out", including how to use pointers and references, before attempting to learn classes.

    EDIT
    So is it like after you have written all the code for what that function does the result is what you return so when you call on that function what you returned is basically what you are calling for?
    Right!!!!

    return; // "Go back where you came from."
    return x; // "Go back where you came from and take x with you."
    Last edited by DougDbug; 05-04-2004 at 07:21 PM.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    7
    Well, I'm reading this book called "Learn C++ in 24 hours" written by Jesse Liberty. Great book, makes most things very clear. It organizes everything into hours and I'm at hour 7 which covers basic classes... I now have a much better understanding of C++ as a whole since functions ARE a main part of C++. Thanks!

    P.S.
    DougDbug do you think I should learn something else before classes? Here's a little taste of what I already know... Hour 6 was "Program Flow" which had to do with loops. 5 was "Functions" and most everything before that was basic stuff. So... Should I continue reading about classes or try something else?

  8. #8
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    C++ cannot be taught in 24 hours, I would suggest putting that book down and finding another one. And I don't mean the other "learn C++ in 28 days" either. Unless you are in a class that you just want to quickly pass and not retain any of the information for later use, take your time and pick up a book that explains the language and its components in their entirety.

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by skorman00
    C++ cannot be taught in 24 hours, I would suggest putting that book down and finding another one. And I don't mean the other "learn C++ in 28 days" either. Unless you are in a class that you just want to quickly pass and not retain any of the information for later use, take your time and pick up a book that explains the language and its components in their entirety.
    I believe that the book says that C++ cannot be learnt in 24 hours as well. Like any other beginning book on C++, it's a springboard into the language, not a thorough dissertation covering every little detail. In fact the only book that truly covers C++ in its entirety is the ISO standard, and I wouldn't recommend that to a beginner.

    Most people don't need every feature when first learning the language, so you shouldn't be so critical toward books for newcomers. They aren't as easy to write as you might imagine.

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    7
    Well I definitely don't intend to learn it in 24 hours.... I spend at least a day, two, or three on each "hour". And I intend to look into internet resources on each subject so this book isn't the only way I'm learning.

  11. #11
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I agree with you full heartedly Kip, however I have read through the book he is reading. In my humble opinion, its complete garbage.

    Just look at what this fellow originally posted,
    I'm learning C++ and am currently looking at classes. The whole time I've been learning I have been kind of lost as far as return values go
    that isn't a sign of a good book to learn with (no offense to you om4li, it's the book's fault not yours).

    In contrast to what I said before, the learn C++ in 28 days (or some number close to that) wasn't all that bad, but I do think there are better ones out there. They're great to get a fresh start with, but you'll wind up having to get another book and almost relearn some stuff anyway. The C++ Primer is very thorough, easy to read, and even gives excperts when things are not traditionally used anymore. If you were to get a book to try and learn with, I would suggest that one. Keep in mind you wont read everything word for word your first time through, but it will still be there when you take a second pass.

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    7
    But will that book work for a total beginner? The only other programming knowledge I have is some QBasic...


    Edit : Wow, I'm checking that book out at Amazon and for one thing, it is HUGE which is great. Also, I see that they have classes set WAY after it is in the book I have now. Well, I might look into buying that book... Thanks for the recommendation.
    Last edited by om4li; 05-04-2004 at 09:29 PM.

  13. #13
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I think it is great for a beginner when used properly. Like I said, don't be prepared to read every excpert word for word. If a section still doesnt make sense after 2 or 3 passes, come back to it later.

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    100
    Good reference is essential to anything, IMHO. I've just about finished my first semester of C++, and the text that was used (A Structured Approach Using C++) sucked. I have pulled a book or two out of the library (forgot the titles) to fill the spaces in my comprehension. I have also found this site to be very useful.
    I guess my point is snag any decent books you see, bookmark any good web sites that you come across, and when the next assignment falls in your lap, you can go at it loaded for bear.

  15. #15
    Registered User
    Join Date
    May 2004
    Posts
    7
    Skorman, on the Primer, it has an answer book sold separately. Do I need to buy this too? I would hate to buy something I don't need, or not get something totally essential.

    Wow, this topic has drifted far away from what it originally was about...

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

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 09:32 AM
  3. Function returns null value instead of zero
    By drdepoy in forum C Programming
    Replies: 3
    Last Post: 10-23-2005, 04:51 PM
  4. Replies: 2
    Last Post: 04-12-2004, 02:37 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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