Thread: What does "return" do?

  1. #16
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >Who are you refering to in the confused newbie statement?
    Obviously, not to you

    >I did it so he will get the right idea. You shuld have a reason to take up space.
    Do you really believe that a man with this problem will care about these things?

    After all, i told him "the statement "result total;" could be simplier written as "return x + y;" and ommit the use of variable total."

    With the same logic, i could simplier write
    printf( "Their sum is %d", sum( x, y ) );
    and ommit the use of answer.
    But i didn't because this isn't our point.
    Loading.....
    ( Trying to be a good C Programmer )

  2. #17
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >e.g. switcharoo(a, b); //with a and b being main()'s variables
    Or the variables in any other function. A function can call other functions.

    >you send the end result of the function's process to main() by using "return",
    No, you return this value to the function that called your function. In our example it was main. A function might call another function. Not all functions are called from main()

    >But I made a code .... "undeclared identifier
    Post your code here.
    Loading.....
    ( Trying to be a good C Programmer )

  3. #18
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    A variable is only good inside its function.

    When you call switcharoo you arnt send the function varibles, your sending it vaules. You can play with them in a function and not change their vaule.

    If you want to change their values you have to use pointers.



    money, You dont add to code when you dont have to. Making it easier to read is a good enough reason, but were doing basic stuff here. The smaller the better.

  4. #19
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >You dont add to code when you dont have to. Making it easier to read is a good enough reason, but were doing basic stuff here. The smaller the better.

    So, next time remember to write "return x+y;" instead of "return (x+y);" .
    Man, i really don't have anything else to tell you.

    P.S I am leaving now..
    Loading.....
    ( Trying to be a good C Programmer )

  5. #20
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    "Making it easier to read is a good enough reason", jackass.

  6. #21
    Registered User
    Join Date
    May 2003
    Posts
    49
    Ooohh, so after I alter two variables from main()----let's call them a and b----in a function and return the result back in the variable "result" again, I'm sending the updated values for both a and b? If that's the deal, I think I understand it.

  7. #22
    Registered User
    Join Date
    Jul 2003
    Posts
    26

    Wink return

    Just play with it, get experience with it, and get examples and ideas from others...

    "any egotistic reply's to this message will be dealt with firmly"
    "Borland Rocks!"

  8. #23
    Registered User
    Join Date
    Jul 2003
    Posts
    26

    necessary statement

    Ohh, and by the way. I would like to add a very neccesary statement to all the ego-heads in this forum.

    "I think money has problems with getting his opinions confused with the reality others opinions differ, of course I would be confused to if I had my head up my @$$ all the time"
    "Borland Rocks!"

  9. #24
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    nold - You're a moron. Of course I know how to use return(). I also know how to use a microwave. Do I need to post instructions on how to do that to make you believe me?

    money? - You're a moron, but at least you (almost) know what you're talking about.

    martman00 - Good job trying to help

    I got sick of reading through the God-forsaken grammarless flame-bait that is this thread, and just skimmed. So I'm not sure if the original poster understands return yet or not. So I'm going to post an analogy-type thing with an explanation, hoping that it will be of some use. Nold, you better read it too, because you seem to have very little idea about what you speak.

    A function (usually) accepts some values, like this
    Code:
    int function(char value1, float value2)
    The values that function() accepts in my example are value1 and value2. Then, function() will do some things with the values it is given, possibly calling other functions in the process. After it is all done doing it's thing to the values it was given, it is allowed to return one, and only one, value to the function that called it (main, for example) For the example above, that value will be of type int. Normally, what is returned will be either be a value indicating the result of all the hard work done in the function, or a value indicating that the function either failed or succeeded in doing what it was supposed to do. Now, to hopefully clarify this just a little bit more, I'm going to provide some sample code (there was some other good sample code given before, but it was stuck in with a bunch of flaming and other code that wasn't good)

    Code:
    #include <iostream>
    using namespace std;
    
    int add(int a, int b)
    {
       cout<<"in function add();"<<endl;
       return (a+b);  //add a+b and return it as the value of this function
    }
    
    int main()
    {
        int num1 = 7;
        int num2 = 4;
        int result;
    
        cout<<"in main();"<<endl;
       
        cout<<"calling add();"<<endl;
        result=add(num1,num2);
    
        cout<<"back in main();.  Add returned this value:"<<result;
    
        return 0; //return from main exits the program and gives a value to the operating system
    }
    I hope that was of help.
    Away.

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by blackrat364
    I hope that was of help.
    Well it would have been, if you would have taken pause long enough from your calling everyone 'moron', to notice that you were in the C forum and not the C++ forum.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #26
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Hey Tride, let's start from scratch without all the flames and try to answer your question in a single, hopefully correct, post:

    When you define a function, the type of the function is the type of value the function will return, i.e.
    Code:
    int thisFunction(int a, int b)
    {
        int rv;
        rv = a * (3 + b);
        return rv;
    }
    In the above, the function will receive two integers (a and b) which are used within the function to calculate the result rv. Since the function is defined as an int, the line return rv; sends the value of rv (an int) back to the calling routine.

    Therefore,
    Code:
        int c;
        int val;
        ....
        val = thisFunction(2,c);
    calling thisFunction will calculate a value (using 2 and c) which will be returned into the variable val.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #27
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    I don't believe that so much discussion was made because i used another variable, because i 'd prefer the return statement to has only one thing for the first programm of a newbie.
    And what irritates me most, is that fact that although i told to that guy "the statement "result total;" could be simplier written as "return x + y;" and ommit the use of variable total.", you(plural) still come and say your words.

    >I got sick of reading through the God-forsaken grammarless flame-bait
    Oh really?? You will smooth out my job very much if you stop reading - and cure your sickness.

    Also, we want to thank you(singular) very much about your code; your "different" code which would explain the question of that guy. Your code only differs from mine in the variable total ( i said to much about this before. )

    >So, next time remember to write "return x+y;" instead of "return (x+y);"
    Minds like yours could only take this seriously.
    Loading.....
    ( Trying to be a good C Programmer )

  13. #28
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Tride - buy a book on C programming, read the section on functions and try the examples.

  14. #29
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    I think that something which may help the explanation is that ( please correct me if I'm wrong - c isnt my best language) return is a control statement.

    A program can only do one thing at a time.

    When the program starts main is given control by the operating system, its statements are executed sequentially. When main starts a function it gives control to that function. So whilst that function is executing main does nothing. When the function returns control to main (with the return statement) it can continue to process.

    It all goes back to the way a computer works. If there was no scheduler the operating system would do nothing until main returned control to it.

  15. #30
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The flames and pointless bickering in this thread will stop now.

    Save your personal opinions of others to Private Messages.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does "return" always terminate the programm?
    By George M. in forum C Programming
    Replies: 11
    Last Post: 09-08-2008, 02:59 PM
  2. Isn't "return" supposed to exit a function?
    By cunnus88 in forum C++ Programming
    Replies: 7
    Last Post: 01-22-2008, 04:58 AM
  3. passing strings from functions as the "return" part
    By fraktal in forum C Programming
    Replies: 8
    Last Post: 12-13-2005, 01:38 AM
  4. "Return" is ending dialog! I dont want that
    By Drogin in forum Windows Programming
    Replies: 3
    Last Post: 11-01-2005, 01:28 PM
  5. info on "return" order in functions
    By Bigbio2002 in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 04:53 PM