'void' arguments (Yet Another Feature Proposal)
Stephen Clamage
steve at taumet.com
Thu Jan 24 03:15:48 AEST 1991
risto at tuura.UUCP (Risto Lankinen) writes:
>Suppose there's function...
> SetThisMode( int iMode,int iValue1,int iValue2,int iValue3 );
>also suppose the documentation says that 'when iMode==<xyz>, then none of
>the iValue:s have any effect'. In code you'd then write:
> SetThisMode( <xyz>,0,0,0 ); or SetThisMode( <xyz>,<your-age-in-days>,
> <temperature-in-Kelvins>,<number-of-emptied-Coke-cans-this-week> );
>when you could be doing...
> SetThisMode( <xyz>,(int)any,(int)any,(int)any );
>where the 'any' is my proposal for a keyword (as promised in the header).
>It could be any other, too, for that matter - I actually tried some 'void'
>derivatives, to check out whether there already were a legal way to do so.
This problem is already solved in two different ways in C++.
1. Default parameters. You may specify default values for any (or all)
trailing parameters in the prototype. Any of the rightmost parameters
with default values may be omitted in a call:
SetThisMode( int iMode, int iValue1=0, int iValue2=0, int iValue3=0);
can be called as any of
SetThisMode(fee>);
SetThisMode(fie>, 1);
SetThisMode(foh>, 1, 2);
SetThisMode(fum>, 1, 2, 3);
This does not address your efficiency concern.
2. Overloaded functions. You may define more than one function with the
same name, as long as their calling sequences are different:
SetThisMode( int iMode);
SetThisMode( int iMode, int iValue1);
SetThisMode( int iMode, int iValue1, int iValue2);
SetThisMode( int iMode, int iValue1, int iValue2, int iValue3);
The compiler calls version of the function with the matching calling sequence.
Since only the supplied parameters are passed, you get the most efficient
possible call.
--
Steve Clamage, TauMetric Corp, steve at taumet.com
More information about the Comp.lang.c
mailing list