Results 1 to 21 of 21

Thread: return

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    return

    I am reading this book and it is using return to do certain things like putting return x * y (x-1); at the bottom of the program. While a function prototype is at the top outside all braces.

    But it does not explain what return is actually doing? And I know that return 0 terminates the program but why?
    Last edited by aewarnick; Jan 2nd, 2003 at 11:03 AM.

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    return does actually what it says it does
    it returns a value.

    lets try it by an example, let's say you have a function that is used to increment a number you pass it by 5.
    Code:
    int Inc(int iNr){
    return (iNr + 5);
    }
    The function expects an integer as a parameter, and returns an integer too.

    so if you call this function like
    Code:
    int iTmp = 5; //This is the value we're gonna send to the function
    iTmp = Inc(iTmp); //This will pass 5 to the function
    iTmp will be 10 now, since we passed 5, and the function added 5 to it.

    I hope you understand it now?

    By the way, with returning from the Main function, you can specify error codes. Like if you return 0 it means no error occurred, and if you return 1 you can specify that something went wrong. This is used if your app is being launched from another app, and the numbers you return can mean anything, it's just the way you implement it.
    usually you just return 0
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If your program returns other than 0, most environments will assume it failed to do what was necessary. For example:
    Code:
    $ myprog --arg=val && somecommand --dependent=myprog
    If myprog fails, then somecommand cannot operate correctly. If you return 0 from myprog, the shell will interpret that as non-zero, i.e. success.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Oops I messed things up, sorry I rarely make console apps, my bad...
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Jop, did you erase your post?

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The DOS choice app used return values from the app too. You used it in batch files. You call it and it waits for the user to type one of several characters. Then it returns the 0-based index of this character in the parameter list. You could then test this value using the ERRORLEVEL.

    if ERRORLEVEL 3 goto three
    if ERRORLEVEL 2 goto two
    ...

    retrun actually means "return from the function". You can simply write
    return;
    if the function has no return type. This will exit the function.
    If the function has a return type you have to specify what should be returned:
    return 0;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    I think that I understand return now. Return is only useful if another function or statement calls it, right? Except for the return 0 to end the program.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    return isn't called. return is a statement.

    return is useful in many places. It ends the function you're in and gives control back to the function that called it. It can optionally return a value.

    The return 0; at the end of your app gives control back to the function that called your main function. This is some windows-internal function which will then end the program.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Originally posted by CornedBee
    This is some windows-internal function
    Not a Windows function but a CRT function, mainCRTStartup, assuming you use the MSVCRT.

    Check out: crt\src\crt0.c
    It's one big function that's actually four functions all in one.
    The function (of the four) that will be compiled depends on the preprocessor macros defined.
    Code:
    #ifdef _WINMAIN_
    
    #ifdef WPRFLAG
    int wWinMainCRTStartup(
    #else  /* WPRFLAG */
    int WinMainCRTStartup(
    #endif  /* WPRFLAG */
    
    #else  /* _WINMAIN_ */
    
    #ifdef WPRFLAG
    int wmainCRTStartup(
    #else  /* WPRFLAG */
    int mainCRTStartup(
    #endif  /* WPRFLAG */
    
    #endif  /* _WINMAIN_ */
    
            void
            )
    
    {
    This is the function Windows calls on startup (the entry point).
    Looking further down...
    Code:
    mainret = main(__argc, __argv, _environ);
    is the actual call to main, and
    Code:
    exit(mainret);
    is the call that terminates the process after main returns.

  10. #10
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Return codes and && save you a hell of a lot of grief in UNIX command lines.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Originally posted by Yonatan
    Not a Windows function but a CRT function, mainCRTStartup, assuming you use the MSVCRT.
    I know, but it doesn't really matter, does it? Let's not make it overcomplicated. I regretted even mentioning the startup function when I had posted this.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Anything outside main() should never normally be considered, except by compiler/library implementors.

    For example, on here I don't have a mainCRTStartup function...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yep, that's why I regretted it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    "It ends the function you're in and gives control back to the function that called it."

    What if I put return; at the end of a function but no funcion has called it. For example:

    {int main()
    cout<<"fjlakjsdlkfjdjflkajsd";
    int funk (){
    int c; cin>>c; cout<<"c";

    return;}

    return 0;}

  15. #15

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    "It ends the function you're in and gives control back to the function that called it."

    What if I put return; at the end of a function but no funcion has called it. Will it just be ignored and move on down? For example:

    {int main()
    cout<<"fjlakjsdlkfjdjflkajsd";
    int funk (){
    int c; cin>>c; cout<<"c";

    return;}

    return 0;}

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You may not put a function inside another function.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    I can't put any function inside int main()? So, for, while, do/while, and if are not functions? How do I know what is a function and what is not?
    Last edited by aewarnick; Jan 5th, 2003 at 04:34 PM.

  18. #18
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Nor inside any other, no.

    Note that you can put a function declaration inside a function:
    Code:
    int a()
    {
      int b();
      return b() + 17;
    }
    
    int b()
    {
      return 42;
    }
    But this is deprecated, and if it isn't, it should be.

  19. #19

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    So, for, while, do/while, and if are not functions? How do I know what is a function and what is not?

  20. #20
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    A function is defined as a function. I.E.:
    Code:
    type name(args) {
        /* statements */
    }
    A function looks like a special kind of block, which is any code between braces.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  21. #21

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Got it! Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width