View Full Version : A very basic question:Return type


S.Tarafdar
12-27-03, 05:34 AM
Hello Everyone,
My question is about return type:

say, I have this code:


int sum(int x, int y){
int add=x+y;
return add;
}


I can understand that it returns an integer number that can be printed as well.

Now, look at this code:


// Complex is a class
//it has two private members say Im and Re

Complex Complex::Value(float x, float y){
//-----------
//-----------
}


You see now Complex type has two private members and other public method(s).
so, what does actually it mean when a function return such a type?
Can I see what the function returns using cout in main function (like the previous one?)

Thanks

AntonK
12-29-03, 07:41 AM
you'd have to know a bit more assembly to know what its truly doing. I'm far more familiar with RISC assembly so if you're an x86 guy, I appologize. Basically what the computer does is do all its calculations inside a function and place the results into certain registers reserved for return values. If there is not enough space then it will load the values into memory at the top of the stack (using the stack pointer register), then when the program does a return call (which is also usually a jr $ra call) it jumps back to PC+8 the program knows where to go to get the values returned, it knows they're still in the registers or at the top of the stack. This process is very easy when you return an int since an int is simply a single word (a word is the bit length of the architecture...most systems today are 32 bits so its a 32 bit signed integer), it can simply put the whole return value in a single register. Since you are returned more than 1 actual number in your Complex, it uses probably 2 registers. From an assembly standpoint, the private, public, etc has nothing to do with it. Once its been compiled and assembled, the computer doesnt care about any of that.

Hope that helps...feel free to ask me to clarify. And someone feel free to explain a little better in x86 terms.

-AntonK

okinrus
12-29-03, 03:57 PM
int sum(int x, int y){
int add=x+y;
return add;
}

This code will return the result in the eax register.


You see now Complex type has two private members and other public method(s).
so, what does actually it mean when a function return such a type?

It basically means that you can write
Complex c, d;
c = Complex::value(5, 5);
d = Complex::value(6, 5);
if the value is a static function the class.

At the assembly level, I think this can be accomplished by passing in the address of the storage to be filled somehow or leaving the filled in value on the stack. You could try to print out the asm listing from the compiler.


Can I see what the function returns using cout in main function (like the previous one?)

Only if operator<< is defined.