1. 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. Eh, that's sorta what it does.

>>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. <<

3. 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. use of "return"

Study this for example:

//A function for multiplying two numbers

int multiply(int x, int y)
{
}
//use function

multiply(2, 2);

5. 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;

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;
}```
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

6. Re: use of "return"

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

7. Use some tabs!!!

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

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

same thing with total.

8. Please give an example then and enlighten us.

9. If you'd rather debate, go to a debating forum...not a C forum

10. Code:
```int sum( int x, int y )
{
int total;

total = x + y;
}```
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. >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.

12. >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

13. 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. Who are you refering to in the confused newbie statement?

15. 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.