How to declare a function argument as optional?

P

Pushkar Prasad

Hello All

Is there a way to ensure that one of the function argument is set as
optional. So that it is not mandatory for the caller to pass
that parameter and yet if necessary can be used?

I do remember that it is possible, but I can't seem to find that
information. Any guidance will be of great help.

Thanks & Regards
Pushkar Prasad


Thanks & Regards
Pushkar Prasad
 
B

Ben Bacarisse

Pushkar Prasad said:
Is there a way to ensure that one of the function argument is set as
optional. So that it is not mandatory for the caller to pass
that parameter and yet if necessary can be used?

Yes, in a way there is. A function with variable arguments (declared
and defined with ... in the parameter list) has, in some sense, optional
arguments but that may not be what you mean.

C++ function can have truly optional arguments with default values, but
C does not have that mechanism.

<snip>
 
J

Jens Thoms Toerring

Pushkar Prasad said:
Is there a way to ensure that one of the function argument is set as
optional. So that it is not mandatory for the caller to pass
that parameter and yet if necessary can be used?

No, there isn't something like that. You have to pass exactly
as many arguments as the function expects.
I do remember that it is possible, but I can't seem to find that
information. Any guidance will be of great help.

Perhaps you're remembering something from C++? There you can
have optional arguments with default values when not passed
to the function.
Regards, Jens
 
L

Lew Pitcher

No, there isn't something like that. You have to pass exactly
as many arguments as the function expects.

Jens,

How many (and of what type) arguments does the standard I/O printf()
function expect?
 
L

Lew Pitcher

Hello All

Is there a way to ensure that one of the function argument is set as
optional. So that it is not mandatory for the caller to pass
that parameter and yet if necessary can be used?

Short answer: no, there is no way to ensure that one formal argument is
optional.

Counter answer: if your function is defined as a variadic function (the last
parameter is an ellipsis), then C provides a logic mechanism to access
those parameters passed (or not) in the place of the ellipsis:

ISO/IEC 9899:1999 (E) ©ISO/IEC

7.15 Variable arguments <stdarg.h>

1 The header <stdarg.h> declares a type and deï¬nes four macros, for
advancing through a list of arguments whose number and types are not
known to the called function when it is translated.
2 A function may be called with a variable number of arguments of varying
types. As described in 6.9.1, its parameter list contains one or more
parameters. The rightmost parameter plays a special role in the access
mechanism, and will be designated parmN in this description.
3 The type declared is
va_list
which is an object type suitable for holding information needed by the
macros va_start, va_arg, va_end, and va_copy. If access to the varying
arguments is desired, the called function shall declare an object
(referred to as ap in this subclause) having type va_list. The object ap
may be passed as an argument to another function; if that function
invokes the va_arg macro with parameter ap, the value of ap in the
calling function is indeterminate and shall be passed to the va_end
macro prior to any further reference to ap.212)


7.15.1 Variable argument list access macros

1 The va_start, va_arg, and va_copy macros described in this subclause
shall be implemented as macros, not functions. It is unspeciï¬ed whether
va_end is a macro or an identiï¬er declared with external linkage. If a
macro deï¬nition is suppressed in order to access an actual function, or
a program deï¬nes an external identiï¬er with the name va_end, the
behavior is undeï¬ned. Each invocation of the va_start or va_copy macros
shall be matched by a corresponding invocation of the va_end macro in
the same function.

...
 
J

Jens Thoms Toerring

Lew Pitcher said:
No, there isn't something like that. You have to pass exactly
as many arguments as the function expects.

How many (and of what type) arguments does the standard I/O printf()
function expect? [/QUOTE]

Well, the problem is that variadic functions are quite a bit
different - while you can have an unspecified number of argu-
ments you then need some extra argument as indicator of (at
least) how many arguments the function is receiving. And, as
far as I understood what the OP was looking for, I didn't
consider that as a solution - it means you must actively mark
non-supplied arguments as not passed instead of "...function
argument is set as optional...". My guess was that he was
looking for something like

int foo( int mandatory, double optional = 0.0 );

which you need C++ for. But, of course, I may have misinter-
preted what the OP was asking for.

Regards, Jens
 
K

Keith Thompson

Ben Bacarisse said:
Yes, in a way there is. A function with variable arguments (declared
and defined with ... in the parameter list) has, in some sense, optional
arguments but that may not be what you mean.

C++ function can have truly optional arguments with default values, but
C does not have that mechanism.

And the problem with the "..." mechanism is that there's no checking
that you've provided the correct number and type of arguments. You
probably want something like:

void func(int x, [optional] int y);

which can be called as func(10), or func(10, 20), but not as
func(10, 20, 30) or func(10, "hello").

If you declare it as:

void func(int x, ...);

then all of those calls are legal; there's no way to enforce your
requirements at compile time.

Depending on what you're trying to do, your best bet might be to write
two different functions with different names:

void func1(int x); /* calls func2(x, 0) */
void func2(int x, int y);

You know when you write a call how may arguments you're passing,
so you can use that knowledge to determine which function you call.
 
J

jacob navia

Le 07/02/11 15:18, Pushkar Prasad a écrit :
Hello All

Is there a way to ensure that one of the function argument is set as
optional. So that it is not mandatory for the caller to pass
that parameter and yet if necessary can be used?

I do remember that it is possible, but I can't seem to find that
information. Any guidance will be of great help.

Thanks & Regards
Pushkar Prasad


Thanks & Regards
Pushkar Prasad

The lcc-win compiler proposes optional arguments within the C language
(not C++)

The syntax is:

int fn(int a,int b, int c = 67,int d = 34);

In this example the last two arguments are optional, and if absent they
will be given the values 67 and 34 respectively.

Another C compiler that supports optional arguments is Pelles' C. It
uses the same syntax.

jacob
 
D

David Mathog

Is there a way to ensure that one of the function argument is set as
optional. So that it is not mandatory for the caller to pass
that parameter and yet if necessary can be used?

Well, this isn't exactly that, but it is pretty common to have an
"ignore this argument" value. For instance (silly example):

void copy_to_morethanone(int value, int *targ1, int *targ2, int *targ3)
{
if(targ1)*targ1=value;
if(targ2)*targ2=value;
if(targ3)*targ3=value;
}

then invoke as:

int a,b,c;
copy_to_morethanone(10,&a,NULL,NULL);
copy_to_morethanone(12,NULL,&b,&c);

/*etc.*/

Here, in essence, the last 3 arguments are all optional, but the
spaces for those parameters must be set to NULL if they are not being
used.

Regards,

David Mathog
 
P

Pushkar Prasad

Thanks everybody for their response. I was misguided by the notation
that I saw in stdio.h and other header files which had Annotations like
_In_opt_. But these are microsoft extensions and simply indicate the
relationship of the arguments to the compiler :). I understand all the
replies that I got and I have choosen following format.

foo(arg1, arg2, [OPT] arg3)

while I am into the function. I will not use arg3 unless I have validated
that a valid argument was passed. I am thinking of using it for the cases
where I am using a pointer so that the caller of my function can pass NULL
for arg3 and it will be easy for me to validate within foo() that the
argument is valid or it was considered optional

Thanks & Regards
Pushkar Prasad

Ben Bacarisse said:
Yes, in a way there is. A function with variable arguments (declared
and defined with ... in the parameter list) has, in some sense, optional
arguments but that may not be what you mean.

C++ function can have truly optional arguments with default values, but
C does not have that mechanism.

And the problem with the "..." mechanism is that there's no checking
that you've provided the correct number and type of arguments. You
probably want something like:

void func(int x, [optional] int y);

which can be called as func(10), or func(10, 20), but not as
func(10, 20, 30) or func(10, "hello").

If you declare it as:

void func(int x, ...);

then all of those calls are legal; there's no way to enforce your
requirements at compile time.

Depending on what you're trying to do, your best bet might be to write
two different functions with different names:

void func1(int x); /* calls func2(x, 0) */
void func2(int x, int y);

You know when you write a call how may arguments you're passing,
so you can use that knowledge to determine which function you call.

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
K

Keith Thompson

Pushkar Prasad said:
Thanks everybody for their response. I was misguided by the notation
that I saw in stdio.h and other header files which had Annotations like
_In_opt_. But these are microsoft extensions and simply indicate the
relationship of the arguments to the compiler :). I understand all the
replies that I got and I have choosen following format.

foo(arg1, arg2, [OPT] arg3)

while I am into the function. I will not use arg3 unless I have validated
that a valid argument was passed. I am thinking of using it for the cases
where I am using a pointer so that the caller of my function can pass NULL
for arg3 and it will be easy for me to validate within foo() that the
argument is valid or it was considered optional
[...]

I don't know what you mean when you say you have chosen that format.

If you've understood the replies, you should understand that

foo(arg1, arg2, [OPT] arg3)

is a syntax error (unless some compiler supports it as an extension).

(And please don't send me e-mail copies of Usenet followups. I'll read
it here.)
 
P

Pushkar Prasad

I used [OPT] to indicate that my third argument will be optional
This is not what I am using in my code. I am passing arg3 as NULL
and checking for it within foo() before I use that argument


Thanks & Regards
Pushkar Prasad

Pushkar Prasad said:
Thanks everybody for their response. I was misguided by the notation
that I saw in stdio.h and other header files which had Annotations like
_In_opt_. But these are microsoft extensions and simply indicate the
relationship of the arguments to the compiler :). I understand all the
replies that I got and I have choosen following format.

foo(arg1, arg2, [OPT] arg3)

while I am into the function. I will not use arg3 unless I have validated
that a valid argument was passed. I am thinking of using it for the cases
where I am using a pointer so that the caller of my function can pass NULL
for arg3 and it will be easy for me to validate within foo() that the
argument is valid or it was considered optional
[...]

I don't know what you mean when you say you have chosen that format.

If you've understood the replies, you should understand that

foo(arg1, arg2, [OPT] arg3)

is a syntax error (unless some compiler supports it as an extension).

(And please don't send me e-mail copies of Usenet followups. I'll read
it here.)

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
D

Dr Nick

David Mathog said:
Well, this isn't exactly that, but it is pretty common to have an
"ignore this argument" value. For instance (silly example):

void copy_to_morethanone(int value, int *targ1, int *targ2, int *targ3)
{
if(targ1)*targ1=value;
if(targ2)*targ2=value;
if(targ3)*targ3=value;
}

If ever there was an argument for judicious use of whitespace this is
it! I don't really mind one-line ifs for this sort of thing as it lets
you lay it out tidily, but for a few seconds I was convinced that there
was multiplication going on there.
 
J

Joel C. Salomon

Pushkar Prasad said:
Is there a way to ensure that one of the function argument is set as
optional. So that it is not mandatory for the caller to pass
that parameter and yet if necessary can be used?

Jens Gustedt's P99 library has a feature to allow this. It uses a trick with
C99's variadic macros that can count the number of arguments passed.

See <http://p99.gforge.inria.fr/p99-html/group__default__arguments.html> for
all the gory details. A simpler version will look like this:

// mimic int foo(int a, int b = 42):
int foo(int a, int b) {.}
#define _overload_foo_1(a) foo(a, 42)
#define _overload_foo_2(a, b) foo(a, b)
#define foo(...) _overload_foo(P99_NARG(__VA_ARGS__), __VA_ARGS__)
#define _overload_foo(n, ...) \
P99_PASTE(_overload_foo_, n) (__VA_ARGS__)

Effectively, this wraps foo() with a macro that
.. counts the number n of arguments passed;
.. constructs a call to _overload_foo_n; and
.. defines _overload_foo_n for n=1 & n=2 such that the overloading is achieved.

-Joel
 
J

Joel C. Salomon

Pushkar Prasad said:
Is there a way to ensure that one of the function argument is set as optional. So that it is not mandatory for the caller to pass
that parameter and yet if necessary can be used?

Jens Gustedt’s P99 library has a feature to allow this. It uses a trick with
C99's variadic macros that can count the number of arguments passed.

See <http://p99.gforge.inria.fr/p99-html/group__default__arguments.html> for
all the gory details. A simpler version will look like this:

// mimic int foo(int a, int b = 42):
int foo(int a, int b) {…}
#define _overload_foo_1(a) foo(a, 42)
#define _overload_foo_2(a, b) foo(a, b)
#define foo(...) _overload_foo(P99_NARG(__VA_ARGS__), __VA_ARGS__)
#define _overload_foo(n, ...) \
P99_PASTE(_overload_foo_, n) (__VA_ARGS__)

Effectively, this wraps foo() with a macro that
• counts the number n of arguments passed;
• constructs a call to _overload_foo_n; and
• defines _overload_foo_n for n=1 & n=2 such that the overloading is achieved.

—Joel
 
J

Joel C. Salomon

Pushkar Prasad said:
Is there a way to ensure that one of the function argument is set as optional. So that it is not mandatory for the caller to pass
that parameter and yet if necessary can be used?

Jens Gustedt’s P99 library has a feature to allow this. It uses a trick with
C99's variadic macros that can count the number of arguments passed.

See <http://p99.gforge.inria.fr/p99-html/group__default__arguments.html> for
all the gory details. A simpler version will look like this:

// mimic int foo(int a, int b = 42):
int foo(int a, int b) {…}
#define _overload_foo_1(a) foo(a, 42)
#define _overload_foo_2(a, b) foo(a, b)
#define foo(...) _overload_foo(P99_NARG(__VA_ARGS__), __VA_ARGS__)
#define _overload_foo(n, ...) \
P99_PASTE(_overload_foo_, n) (__VA_ARGS__)

Effectively, this wraps foo() with a macro that
• counts the number n of arguments passed;
• constructs a call to _overload_foo_n; and
• defines _overload_foo_n for n=1 & n=2 such that the overloading is achieved.

—Joel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top