Archived

This topic is now archived and is closed to further replies.

Betrayer_of_Code

question about "return"

Recommended Posts

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

Share this post


Link to post
Share on other sites
sliderag    122
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
IFooBar    906
"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 ] ::::

Share this post


Link to post
Share on other sites
Code-Junkie    122
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.

Share this post


Link to post
Share on other sites
Daaark    3553


return RETURNS a value from a function. If that function returns a - b it should be called
subtract.


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.

Share this post


Link to post
Share on other sites
BriTeg    168
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

}

Share this post


Link to post
Share on other sites
sliderag    122
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]

Share this post


Link to post
Share on other sites
randomZ    163
OK, in little steps:

Say we have a simple function named sqr(). It''s purpose is to calculate the square of a value.
We want to use the function in this way:

  
float f = 2.0f;
float squareOfF = sqr(f);


To do this, the function sqr() has to *return* the square of f. Like this:


  
float sqr(float num)
{
return num * num; // num * num = sqr(num) = square of num

}


This could also be written like this:


  
float sqr(float num)
{
float result = num * num;
return result;
}


I hope this has made it a little clearer for you.


My Wonderful Web Site (C++ SDL OpenGL Game Programming)

I am a signature virus. Please add me to your signature so that I may multiply.

Share this post


Link to post
Share on other sites
Pipo DeClown    804
int ReturnInteger() // Function is "int" type
{
int a=0;
return a; // Return an "int"
}

char ReturnCharacter() //Function is "char" type
{
char a='a';
return a; // Return a "char"
}

int* ReturnPointer() // Function is "int*" type
{
int* a=NULL;
return a; // Return an "int*"
}

// Now you can use these functions to get a value:
int b = ReturnInteger(); // 0
char c = ReturnCharacter(); // 'a'
int* d = ReturnPointer(); // NULL // Empty




.lick


[edited by - Pipo DeClown on May 27, 2003 2:46:51 PM]

Share this post


Link to post
Share on other sites
Code-Junkie    122
There are many reasons to return something other than 1 or 0.
You may have a function that figures the area of a rectangle and want to return the value into another variable.
int lenght = 5
int width = 2
int x = getArea( length, width)

your function would return 10 into x.

Make sense?

Share this post


Link to post
Share on other sites
MichaelT    214
Return does this:

what this assembler below does:
function a jumps into function b, returns with a value. a continues.

assembler:

//func a
.
mov eax,14 (we store some values, like this in c b(14,11)
mov ebx,11
call b (or ja, jz etc.. this is just an example)(this calls the function)
cmp eax,1(it continues here after return comparing the return value for 1)
je somewhere (jump somewhere if the return value is 1)
jmp failed (it was not 1, we go here instead)
.

//func b
.
doing things
.
mov eax,0 (put a 0 in eax (our return value)
ret (return, eax is what return holds the value(see above)(again just an example)
(now it has jumped back to func a)
.

Now, this is off the top of my head so don''t flame me about incorrect things. However, this is a rather accurate view of exactly what happens. Note that this is for intel compatible cpus but it is roughly the same on any cpu family.


____________________________________________________________
Try RealityRift at www.planetrift.com
Feel free to comment, object, laugh at or agree to this. I won''t engage in flaming because of what I have said.
I could be wrong or right but the ideas are mine.

Share this post


Link to post
Share on other sites