I like class functions a lot but they tend to make equations messy in code. Free functions with a return value are much more intuitive in my opinion. For instance to calculate 3x^2 + 2:

Free function with no return value:

square(x);
multiply(x, 3);
add(x, 2);
return x;

Class function:

x.square();
x.multiply(3);
x.add(2);
return x;

Or, if the class function returns itself:

return x.square().multiply(3).add(2);

Free function with return value:

return 3 * x * x + 2; // or return 3x**2 + 2 if ** is supported

Of course this is a trivial example considering the free function is in fact a language operator. Slightly more complicated: calculate normalize(cross(v1, v2)):

Free function with no return value:

cross(v1, v2); // modifies v1
normalize(v1);
return v1;

Class function:

return v1.cross(v2).normalize(); // not bad

Free function with return value:

return normalize(cross(v1, v2));

I don't know about you guys but I prefer the last option. Of course, as always,

*it depends*. All those different methods have they strengths and weaknesses, and sometimes there is no clear "best" option. As for performance, unless you are bottlenecking on arithmetic or memory copy (which is

*highly* unlikely), I wouldn't worry about it.

**Edited by Bacterius, 27 May 2012 - 08:21 PM.**

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

- *Pessimal Algorithms and Simplexity Analysis*