question about "return"

Started by
13 comments, last by Betrayer_of_Code 20 years, 10 months ago
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.
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
Advertisement
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]
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?
Thanks,CodeJunkie
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.

No no no no! :)
Thank you all, you were ENCREDIABLY helpful

EDIT: Ok, i cant spell incredibly, I hope the second spelling is closer

[edited by - betrayer_of_code on May 27, 2003 10:56:57 PM]
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.

This topic is closed to new replies.

Advertisement