What does "return" do?

This is a discussion on What does "return" do? within the C Programming forums, part of the General Programming Boards category; I can't quite understand what returning values does. It sounds (to me) like it gives functions a value. Like, returning ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    49

    What does "return" do?

    I can't quite understand what returning values does. It sounds (to me) like it gives functions a value. Like, returning the variable "a", which is an int value, at the end of a function will make the whole function be type int. But I know that's not what it does.

    Can someone give an easy-to-understand explanation of what "return" does and why I need to use it? I've read explanations (in books and such) of what it does before, but they don't make sense to me, and I know I'll need to understand it's use later on.

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Eh, that's sorta what it does.

    Read this. No, really. Actually click on the link and read that

    >>Return is the keyword used to force the function to return a value. Note that it is possible to have a function that returns no value. In that case, the prototype would have a return type of void. <<
    Away.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    49
    But don't you give a function a type when you define (I think this is defining) it?:
    Code:
    int changevalue(int a);
    {
             blah blah
             blah blah blah
    
    etc.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    26

    use of "return"

    Study this for example:

    //A function for multiplying two numbers

    int multiply(int x, int y)
    {
    int answer;
    return answer= (x * y);
    }
    //use function

    multiply(2, 2);
    printf("2 x 2=%i",answer);
    "Borland Rocks!"

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Well, here's a very simple example which i hope will help:
    Code:
    #include <stdio.h>
    
    int sum( int, int ); // the bold int says that function sum will return a value of type int
    
    int main( void )
    {
    int x, y;
    int answer;
    
    printf( "Enter 2 number's to find their sum:\n" );
    scanf( "%d %d", &x, &y );
    
    answer = sum( x, y );
    
    printf( "Their sum is %d", answer );
    return 0;
    }
    
    int sum( int x, int y )
    {
    int total;
    
    total = x + y;
    return total;  // total is of type int
    }
    Function sum takes 2 integers and finds their sum, and assign it to variable total. But main() doesn't have access to this variable, and generally to variables which are defined in other functions. So function main doesn't know their sum yet.
    So, to find what the result is, function sum has to return to main this value. So you put the statement "return total;" to say that this function will return the value of "total" to the function which has called it ( in this case, function sum was called from main ).
    If you didn't put this statement to function sum, main() wouldn't have the potential to determine the result of x plus y, because, simple, it doesn't have acces to whatever happens to function sum.
    But: function sum knows the value of x and y because it got it from main.
    the statement "result total;" could be simplier written as "return x + y;" and ommit the use of variable total.

    Also, the type of the variable ( double, int, char, float etc. ) which will be returned by a function is the first word which is written in the function prototype.
    In this example, because total is of type int, we wrote:
    int sum( int, int );

    I hope i helped...
    Good luck
    Loading.....
    ( Trying to be a good C Programmer )

  6. #6
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    Re: use of "return"

    Originally posted by nold
    Study this for example:
    No. Don't. That will confuse you, and isn't really correct.
    Away.

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    Use some tabs!!!

    It makes it easier to read plus its why there are code tags.

    return answer= (x * y);
    just 'return (x*y);', then you dont need answer at all.

    same thing with total.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    Please give an example then and enlighten us.
    "Borland Rocks!"

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    If you'd rather debate, go to a debating forum...not a C forum
    "Borland Rocks!"

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    Code:
    int sum( int x, int y )
    {
    int total;
    
    total = x + y;
    return total;  // total is of type int
    }
    to

    Code:
    int sum(int x, int y) {
    	return (x+y);
    }
    Total isnt needed, its just wasted space. Im being pretty picky about it, I know. I just pointed it out....

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >Use some tabs!!!

    Sorry for not using tabs...eerr.. how cah i print a tab(when i click the "selescts" the bottons )

    nold,
    I don't know what you are talking about, i don't either know who you refer to.
    Loading.....
    ( Trying to be a good C Programmer )

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >Im being pretty picky about it, I know
    You shouldn't.

    It's just an example at it's simpliest form to explain something very basic to a confused newbye
    Loading.....
    ( Trying to be a good C Programmer )

  13. #13
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    It's just an example at it's simpliest form to explain something very basic to a confused newbye
    I did it so he will get the right idea. You shuld have a reason to take up space.

  14. #14
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    Who are you refering to in the confused newbie statement?
    "Borland Rocks!"

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    49
    Function sum takes 2 integers and finds their sum, and assign it to variable total. But main() doesn't have access to this variable, and generally to variables which are defined in other functions. So function main doesn't know their sum yet.
    So, to find what the result is, function sum has to return to main this value. So you put the statement "return total;" to say that this function will return the value of "total" to the function which has called it ( in this case, function sum was called from main ).
    If you didn't put this statement to function sum, main() wouldn't have the potential to determine the result of x plus y, because, simple, it doesn't have acces to whatever happens to function sum.
    But: function sum knows the value of x and y because it got it from main.
    the statement "result total;" could be simplier written as "return x + y;" and ommit the use of variable total.

    Also, the type of the variable ( double, int, char, float etc. ) which will be returned by a function is the first word which is written in the function prototype.
    In this example, because total is of type int, we wrote:
    int sum( int, int );
    Ok, I think I get it . As I understand it, you call upon a function in main(), use the function on things local to main()

    e.g. switcharoo(a, b); //with a and b being main()'s variables

    you send the end result of the function's process to main() by using "return", and then (in main()) you do whatever you want with the value/variable returned. But I made a code and tried to use the variable "result"----which I created in a function and returned to main()----in main(), but it claimed result was an "undeclared identifier". Either I messed up on the code or I have the wrong idea of what return does.

Page 1 of 3 123 LastLast
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, 03:58 AM
  3. passing strings from functions as the "return" part
    By fraktal in forum C Programming
    Replies: 8
    Last Post: 12-13-2005, 12:38 AM
  4. "Return" is ending dialog! I dont want that
    By Drogin in forum Windows Programming
    Replies: 3
    Last Post: 11-01-2005, 12:28 PM
  5. info on "return" order in functions
    By Bigbio2002 in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 03:53 PM

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