Jump to content
  • Advertisement

Archived

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

Betrayer_of_Code

question about "return"

This topic is 5533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
"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
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


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!