James Kanze
11/16/2008 12:39:00 AM
On Nov 15, 9:48 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> Carl Forsman wrote:
> > Then I can make code more clean to have all function to void
> > return type and if I need a return value from a function, I
> > just pass in the pointer that I want the function to return
> > (e.g. *SYSTEMTIME time)
> You can do this, but I doubt many would agree with your
> definition of clean.
> Doing what you describe is most often used for function
> templates where the parameter types are used to resolve the
> argument types.
Was most often used. In modern C++, there are other solutions,
and I think most people would prefer to use a return value, and
specify the return type explicitly, i.e. f< int >(), rather than
f( int& ). (It's also possible to design the function to return
a proxy in which the return type is chosen automatically.)
The most frequent use I've seen for using a reference to return
a value is when the function should return more than one value.
Others occasional uses are when the returned value is a
modification of an object which is also used as an in argument,
and when the return type is very expensive to copy, e.g. an
std::list<> with a couple of hundred thousand elements.
> Elsewhere, it just leads to clutter. You have to use
> variables whenever you call a function, whether you want to
> retain the result or not. so rather than simply writing
> int n = doSomething();
> you end up with
> int n;
> doSomething( n );
> manageable for simple variables, but useless for constants or
> objects such as std::auto_ptr that require initialisation.
The real problem is when the return type is something that can
be used in further expressions. Which is clearer:
(-b + sqrt( b*b - 4*a*c))/(2*a)
or even
div( add( neg( b ),
sqrt( sub( mul( b, b ), mul( mul( 4, a ), c ) ) ) ),
mul( 2, a ) )
, compared to
BigNumber bSquared ;
mul( &bSquared, b, b ) ;
BigNumber aTimesC ;
mul( &aTimesC, a, c ) ;
BigNumber aTimesCTimes4 ;
mul( &aTimesCTimes4, 4, aTimesC ) ;
BigNumber sum ;
add( &sum, bSquared, aTimesCTimes4 ) ;
BigNumber delta ;
sqrt( &delta, sum ) ;
// ...
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34