Why is cartesianToPolar() void?


#1

The function cartesianToPolar() is void, therefore the function returns no result, with both the desired values being entered directly into addresses. But the function modf() also returns two essential values, with the difference that in this case, one (integerPart) is the direct result of the function, with only one being returned by reference.

So it looks like there are two different ways of achieving the same goal (if I’ve understood correctly). Are there circumstances where you’d favour one way over the other?


#2

Favour simplicity unless the requirements of your code’s environment dictate otherwise.


#3

So in effect, there’s little to choose between the two, but the non-void method is slightly simpler to use as a programmer because you’re writing less code?

Thanks for your help, much appreciated.


#4

In this particular example, the answer is yes.

However, if the example had not been intended for teaching how to pass arguments by reference, it could have been written like this:

[code]struct Polar
{

};
typedef struct Polar Polar;

struct Cartesian
{

}
typedef struct Cartesian Cartesian;

Polar CartesianToPolar (const Cartesian cart)
{
Polar polar;
polar.r = …
polar.angle = …
return polar;
}[/code]


#5

That makes sense to me – thanks!