noob question: what does return mean?

This is a discussion on noob question: what does return mean? within the C++ Programming forums, part of the General Programming Boards category; I am learning c++ by myself and I've been reading the website and c++ for dummies and I have trouble ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    noob question: what does return mean?

    I am learning c++ by myself and I've been reading the website and c++ for dummies and I have trouble understanding the return. What does it mean when a program returns to 0 or 10??

    I am at page ~100 and he is talking about fonctions that returns to thier arguments and I am totally lost.

    I've just started so I dont really know anything yet.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Returning from main() and returning from other functions is a little different.

    When you return something from a non-main() function, that value gets passed to whatever code called the function in the first place. For example:
    Code:
    #include <iostream>
    
    int get_number(void) {
        return 5;
    }
    
    int main() {
        int num = get_number();
    
        std::cout << num << std::endl;
    
        return 0;
    }
    prints 5.

    In other words, when you call a function that returns a value, you can use that value just like a variable.

    When you return a value from main(), the same thing happens, except that the code that receives the return value is not your own. It's whatever started your program. This code, by convention, expects your main() to return 0 on success, and any other value on failure. What is a "success" and what is a "failure" is up to you to decide.

    There's a tutorial on functions here, too, though it's probably much the same as your book. http://www.cprogramming.com/tutorial/lesson4.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Let's see, how about thinking of a function like a coffee machine. You pour the coffee grounds into the machine (the arguments), it makes a whirrrr sound and coffee spits out the other end (the return value). A function takes something, does something with it, and returns something different.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    2
    Thx I finally get it.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Return means generally to go back to where you were when you entered a function. You don't usually return emptyhanded, but bring the result of the function call with you.

    Probably not the best explanation.

    Code:
    I am at page ~100 ... and I am totally lost.
    May-be go back a few pages or chapters and search for additional materials on the Web? For example Wikipedia has a general overview of the topic (not only C++ specific): Functions

    If the book has any exercises, have you been able to successfully solve them this far?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  3. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 01:22 PM
  4. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

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