Computer Science Canada

Continued confussion with C++

Author:  klopyrev [ Wed Mar 07, 2007 1:51 am ]
Post subject:  Continued confussion with C++

code:

// convert rectangular to polar coordinates
polar rect_to_polar(rect xypos)
{
     polar answer;
     
     answer.distance =
            sqrt( xypos.x * xypos.x + xypos.y * xypos. y);
     answer.angle = atan2(xypos.y, xypos.x);
     return answer;
}


Assuming the two structures used in that method are defined, wouldn't that method not work properly since the method returns a local variable that would cease to exist when the method returns?

KL

Author:  wtd [ Wed Mar 07, 2007 2:29 am ]
Post subject:  RE:Continued confussion with C++

No.

The function works fine (assuming the struct types are properly defined). The struct is returned by value, rather than reference.

Author:  klopyrev [ Wed Mar 07, 2007 2:35 am ]
Post subject:  Re: Continued confussion with C++

Thanks for the response. I figured it out on my own after rereading the chapter about functions.

KL


: