question about "return"

Started by
13 comments, last by Betrayer_of_Code 20 years, 10 months ago
What exactly does return do, I realize it calls the function that called it (I think). But what exactly does it do. for example:
  
#include <whatever>

int number(int a, int b);

int main()
{
    int c = 10;
    int d = 1;

    number(c, d);

    system("pause");
    return 0;
}

int number(int a, int b)
{
    a += 1;
    b += 1;
    return a - b;
}
  
What does ther return a - b do? Homepage Luck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride. For any total noob out there Here is a like to my tutorials, thry them out
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
Advertisement
It returns the answer to a minus b

James Simmons
MindEngine Development
http://medev.sourceforge.net
but what does RETURN DO!!!


Homepage
Luck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
For any total noob out there Here is a like to my tutorials, thry them out
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
It will, in this case, "return" an integer that is equal to a-b. For example, int x=number(5,4) would set variable x to 1.

"Skepticism.... that great rot of the intellect." - V.H.
Bah, what does HE know?


Albekerky Software


[edited by - sliderag on May 27, 2003 2:33:28 PM]
"Skepticism.... that great rot of the intellect." - V.H.Bah, what does HE know?Albekerky Software
I realized all of this, but what now equals 1. and is there a point in returning a value other then 0 or 1 (TRUE or FLASE)?

Homepage
Luck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
For any total noob out there Here is a like to my tutorials, thry them out
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
"return" does *exactly* what it says it does. it *returns* a value from a function. Every function has a return type, therefore every function must return something, and to return something you have to use the keyword "return", followed by what you want the function to return. For example say I have a function called

float get();

This function MUST return a float. so inside the function I would need a return statement followed by a floating point value ie:

float get() { return 0.13692; }

:::: [ Triple Buffer V2.0 ] ::::
[size=2]aliak.net
Since in your code example you do not have a variable set up to catch the returned value, I''m not sure it has any purpose in your code snippet. If you were to use something like

int i = number(5, 2)

then the value 3 would be "returned" into variable i.

I think the "return 0" at the end of the code informs the system that your program terminated properly.

so return will "return" a value if you have something to return it into.

I hope this made sense.
Thanks,CodeJunkie

return RETURNS a value from a function. If that function returns a - b it should be calledsubtract.the int before subtract means that this function returns an integer value when it is done executing.int subtract(int a,int b){		return (a-b);	}if we called 	subtract(10,3);in a program, the funtion will run and return 7 but you need something to return it to. try: 	int answer;	answer = subtract(10,3);the subtract funtion will return 7, so you assign the return value to the answer integer. you could also do:	answer = subtract(subtract(20,10),subtract(10,7));and the program will subtract the return values. the functions will run, and return 10 and 3, then they will be subtracted. 

return simply "returns" back to the point in the code where the function call was made. When you call a function, the current address of where you are is pushed on the stack, then the code jumps to the function. When the function "returns", it looks at the address that was pushed on the stack, jumps to it (thus returning to where it was before the function call) and clears the stack (not the whole stack, just up to what it was before the call).

You can supply a value when you "return", which provides the calling code with output from the function. Like this:


  int sum(int a, int b){  int sum;  sum = a + b;  return sum;}int main(){  int c;  // call a function  c = sum(5,6);  // sum returns 11, which is assigned to c, so after this call, c will equal 11.  // call the function again  sum(3, 4); // sum returns 7, but since no assignment is made to receive the return value, it simply evaporates into oblivion}  
Brianmiserere nostri Domine miserere nostri
quote:I realized all of this, but what now equals 1. and is there a point in returning a value other then 0 or 1 (TRUE or FLASE)?


Well, suppose you want to find the width of a rectangle, you might write a function like this:

int *RectWidth(RECT *pRect)
{
return(pRect->right-pRect->left);
}

The point of this, if you didn't guess, is basically so you can have a way to do calculations using various inputs.

The point of returning 0 or 1 would be, usually, to test whether a function succeeded or had an error somewhere. Here's an example:

int foo(void)
{
//do something

//something didn't work
return(1);

//do more things....

//all went well
return(0);
}

That way you could assign the return value to an integer and test whether it was 1 or 0 to generate an error message or whatever.


Edit: Forgot to say that in my previous example, x is now equal to 1.

"Skepticism.... that great rot of the intellect." - V.H.
Bah, what does HE know?


Albekerky Software


[edited by - sliderag on May 27, 2003 2:48:21 PM]

[edited by - sliderag on May 27, 2003 2:52:00 PM]
"Skepticism.... that great rot of the intellect." - V.H.Bah, what does HE know?Albekerky Software

This topic is closed to new replies.

Advertisement