Thread: Does "return" always terminate the programm?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32

    Does "return" always terminate the programm?

    I have the following code

    Code:
    printf("Start. Give a\n");
    scanf("%d",&a);
    printf("\na is %d\n",a);
    if (a==2)
    {
            return a;
        printf("a is 2\n");
    }
    return 0;
    1.If i put 2 the programm is terminated. Shouldn't it just output the value of a (2)? That happened on C++ I think.

    2.And In my book I've red that with return you can restore the cotrol from a function to the main() function. This means that it works like "break;"

    ------------------------------------------------------------------------------------------------
    My learning process on my blog

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    return always terminates the current function. If it is main, that's all she wrote -- your program is over, and the returned value is given to the operating system to do whatever with it (there's a way in most shells to determine the value of a program, but I forget what it is). If it's a called function, then control is returned to the caller.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George M. View Post
    2.And In my book I've red that with return you can restore the cotrol from a function to the main() function. This means that it works like "break;"
    That would be return. Returning from a function ends it and the execution resumes at the next line after the one that called the function that just returned.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    1.If i put 2 the programm is terminated. Shouldn't it just output the value of a (2)? That happened on C++ I think.
    In both C and C++ the return statement terminates the function. Check your compiler warnings, it will probably complain about the printf() never being executed.

    2.And In my book I've red that with return you can restore the cotrol from a function to the main() function. This means that it works like "break;"
    If the function was directly called by main(), then after return it returns to main().

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    2.And In my book I've red that with return you can restore the cotrol from a function to the main() function. This means that it works like "break;"
    Return will give control back to whichever function... called the function... that called it (I'm a terrible writer). It won't necessarily return to main(). For example.

    Code:
    #include <stdio.h>
    
    int function1();
    int function2();
    
    int main() {
        function1();
        return 0; // ends program
    }
    
    int function1() {
        function2();
        return 0; // returns control to main
    }
    
    int function2() {
        printf("hello word\n");
        return 0; // returns control to function1
    }

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Greece
    Posts
    32
    oh.. now i got it. So if its the main it terminates the programm. thank you!

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    so because I have no idea about this and I'm really new I have to ask for clarity on this.

    Are saying that return causes a function to end. Am I u derstanding that properly?

    And what does the number after return do? I see return almost always followed by a 0. What are the hazards of not using return at the end of a function?

    Thanks

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, return ends a function.
    Functions can also have a return type. You specify what you want the function to return after the "return."
    Such as:
    Code:
    typedef bool int;
    bool foo()
    {
        return true;
    }
    
    int main()
    {
        bool x false;
        x = foo(); // x now becomes true.
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    so can you always pull the return data out of a function later then. can the return value be changed like a variable or will you need functions with return values set in stone and call the functions to alter the return values? what are the benefits of using a return value instead of creating a variable and using that within a function?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by demuro1 View Post
    so can you always pull the return data out of a function later then.
    I don't really get that, but it means that you fetch the return from a function when issuing the call. It's irretrievable later. Either you get it or ignore it.

    can the return value be changed like a variable or will you need functions with return values set in stone and call the functions to alter the return values?
    The point of return values is that a function can decide what to return. It can return anything that can be represented by that specific type you chose when declaring the function. Typically, return values are used for returning error or success.
    The return value is then stored in a variable from the calling function and you can then do whatever you want with it.

    what are the benefits of using a return value instead of creating a variable and using that within a function?
    Pretty much only the syntax:
    (For C)
    Code:
    int x:
    foo(&x);
    Or
    Code:
    int x = foo();
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    return just returns a value to the previous function. If there is no previous function, as in main(), then it returns a value to the OS.

    You can return a literal value as in: return 0; or you can return a variable as in: return x;

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Perhaps you are wanting this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminate Boost::Thread
    By CornyKorn21 in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2008, 12:19 PM
  2. HexDump Programm....
    By Kdar in forum C Programming
    Replies: 4
    Last Post: 10-03-2006, 05:13 AM
  3. programm will not start
    By keeper in forum C++ Programming
    Replies: 11
    Last Post: 07-03-2006, 06:02 AM
  4. how to terminate a target thread
    By swaugh in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2005, 11:13 AM
  5. I am looking for a programm!
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-10-2002, 11:51 AM