[comp.lang.c] 'void' arguments

risto@tuura.UUCP (Risto Lankinen) (01/22/91)

Hi!

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.

Why?  Because I feel the code would be more readable with this arrangement,
and because the compiler will have some more freedom to optimize this kind
of function calls (a PUSH 0 would become PUSH <anyreg>, which is smaller
and maybe faster too, or a sequence of them would become SUB SP,<sizesum>
which definitely is more optimal in both size and speed).  I also think
that _pascal functions with variable arguments would become much easier to
implement (with the cost of added stack usage) by declaring arguments for
the 'worst case', and then using only those that are needed.

Terveisin: Risto Lankinen
-- 
Risto Lankinen / product specialist ***************************************
Nokia Data Systems, Technology Dept *  2                              2   *
THIS SPACE INTENTIONALLY LEFT BLANK * 2 -1 is PRIME!  Now working on 2 +1 *
replies: risto@yj.data.nokia.fi     ***************************************

volpe@camelback.crd.ge.com (Christopher R Volpe) (01/23/91)

In article <943@tuura.UUCP>, risto@tuura.UUCP (Risto Lankinen) writes:
|>
|>   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 );
|>

How about making it a variable argument list function and calling
it with 
           SetThisMode(<xyz>);
or
           SetThisMode(<abc>,i,j,k);
???



|>
|>Terveisin: Risto Lankinen
|>-- 
|>Risto Lankinen / product specialist ***************************************
|>Nokia Data Systems, Technology Dept *  2                              2   *
|>THIS SPACE INTENTIONALLY LEFT BLANK * 2 -1 is PRIME!  Now working on 2 +1 *
|>replies: risto@yj.data.nokia.fi     ***************************************
                                   
==================
Chris Volpe
G.E. Corporate R&D
volpecr@crd.ge.com

steve@taumet.com (Stephen Clamage) (01/24/91)

risto@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@taumet.com

gwyn@smoke.brl.mil (Doug Gwyn) (01/24/91)

In article <15955@crdgw1.crd.ge.com> volpe@camelback.crd.ge.com (Christopher R Volpe) writes:
>How about making it a variable argument list function and calling
>it with 
>           SetThisMode(<xyz>);
>or
>           SetThisMode(<abc>,i,j,k);

That is certainly a cleaner approach, and is supported by the current C
standard.  The one possible drawback is that in some implementations use
of a variable-argument function can result in less efficient function
linkage.  (Also, the definition of SetThisMode() becomes more tedious.)
I don't think those drawbacks are too severe, though.  Certainly there
is very little to be gained by introducing another keyword just to
facilitate slightly more efficient linkage in such an uncommon situation.

wolfram@ikki.informatik.rwth-aachen.de (Wolfram Roesler) (02/08/91)

>How about making it a variable argument list function and calling
>it with 
>           SetThisMode(<xyz>);
>or
>           SetThisMode(<abc>,i,j,k);
>???

It's not even necessary to write SetThisMode() to work with a
variable arg list. If it doesnt bother about the last args in case
the first arg is <xyz>, you could simply call it with only one arg.
This will of cource annoy Lint and anybody reading your program,
but it will work.

volpe@camelback.crd.ge.com (Christopher R Volpe) (02/09/91)

In article <wolfram.666027738@ikki>,
wolfram@ikki.informatik.rwth-aachen.de (Wolfram Roesler) writes:

(This reference here was mine, although Wolfram omitted that fact)

|>>How about making it a variable argument list function and calling
|>>it with 
|>>           SetThisMode(<xyz>);
|>>or
|>>           SetThisMode(<abc>,i,j,k);
|>>???
|>
|>It's not even necessary to write SetThisMode() to work with a
|>variable arg list. If it doesnt bother about the last args in case
|>the first arg is <xyz>, you could simply call it with only one arg.
|>This will of cource annoy Lint and anybody reading your program,

Well, I thought we were talking about portable methods written in
*REAL* C. Not only will it annoy lint and anyone reading your
program (as if that weren't enough), it's illegal from an ANSI
point of view and the compiler MUST issue a diagnostic (assuming you
have some sort of prototype in scope) and will most likely not generate
code.             
|>but it will work.

Not if it aborts compilation, it won't.
==================
Chris Volpe
G.E. Corporate R&D
volpecr@crd.ge.com