Mikhail Ramendik <[EMAIL PROTECTED]> said:

> Aaron Stone wrote:
> 
>> Based on your Python comments earlier, I'm assuming that you already
>> program quite a bit, but haven't used C much. My guess is that the only
>> confusing things are *, ** and &. For those, there's a really good cartoon
>> guide that explains them very visually, but I can't remember the name of
>> it :-[
> 
> The current question is not about those, but about optional arguments.
> 
> I.e. I want to be able to define a function named foo(), so that it
> could be called both as foo(a,b) and as foo(a,b,c).
> 
> I'm afraid this was added later than the K&R text was written. 
> 
> I'm nearly sure the feature is there; in fact somebody told me that one
> could even rewrite printf() in C, and that has a theoretically infinite
> number of optional arguments.
> 
> The projected dbmail use is for adding features to db layer functions,
> while preservicg backwards compatibility.

Very bad idea. C does support variadic arguments, but there is absolutely
no type checking whatsoever. Printf() typechecking is a feature of the
compiler, and only if the compiler knows the same printf() that your libc
provides. In fact, for a long time our trace() function was not
typechecked until I found the GCC extension that tells the compiler to
treat a function like printf().

C++ has full support for optional arguments and default values, which it
calls function overloading. Not too much good for us, we're in plain C ;-)

If you need a slightly varied function, the two main options are to either
modify the function and all places that use it (for nearly-trivial changes
that can be specifically unit tested, this is OK) or to create a wrapper
function by the old name that provides the old functionality while calling
the new version of the function with a new name. This is kinda-sorta how
the db_getresult_int, ull, etc. functions are implemented.

Aaron

--

Reply via email to