How to declare a function argument as optional?

Discussion in 'C Programming' started by Pushkar Prasad, Feb 7, 2011.

  1. 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
     
    Pushkar Prasad, Feb 7, 2011
    #1
    1. Advertising

  2. Pushkar Prasad <> writes:

    > 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>
    --
    Ben.
     
    Ben Bacarisse, Feb 7, 2011
    #2
    1. Advertising

  3. Pushkar Prasad <> wrote:
    > 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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Feb 7, 2011
    #3
  4. Pushkar Prasad

    Lew Pitcher Guest

    On February 7, 2011 10:19, in comp.lang.c, wrote:

    > Pushkar Prasad <> wrote:
    >> 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.


    Jens,

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

    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, Feb 7, 2011
    #4
  5. Pushkar Prasad

    Lew Pitcher Guest

    On February 7, 2011 09:18, in comp.lang.c, wrote:

    > 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.

    ...

    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, Feb 7, 2011
    #5
  6. Lew Pitcher <> wrote:
    > On February 7, 2011 10:19, in comp.lang.c, wrote:


    > > Pushkar Prasad <> wrote:
    > >> 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.


    > Jens,


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


    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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Feb 7, 2011
    #6
  7. Ben Bacarisse <> writes:
    > Pushkar Prasad <> writes:
    >> 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.


    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) <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"
     
    Keith Thompson, Feb 7, 2011
    #7
  8. Pushkar Prasad

    jacob navia Guest

    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
     
    jacob navia, Feb 7, 2011
    #8
  9. Pushkar Prasad

    David Mathog Guest

    On Feb 7, 6:18 am, Pushkar Prasad <> wrote:

    > 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
     
    David Mathog, Feb 7, 2011
    #9
  10. 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

    On Mon, 7 Feb 2011, Keith Thompson wrote:

    > Ben Bacarisse <> writes:
    >> Pushkar Prasad <> writes:
    >>> 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.

    >
    > 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) <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"
    >
     
    Pushkar Prasad, Feb 7, 2011
    #10
  11. Pushkar Prasad <> writes:
    > 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) <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"
     
    Keith Thompson, Feb 7, 2011
    #11
  12. 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

    On Mon, 7 Feb 2011, Keith Thompson wrote:

    > Pushkar Prasad <> writes:
    >> 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) <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"
    >
     
    Pushkar Prasad, Feb 7, 2011
    #12
  13. Pushkar Prasad

    Dr Nick Guest

    David Mathog <> writes:

    > 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.
    --
    Online waterways route planner | http://canalplan.eu
    Plan trips, see photos, check facilities | http://canalplan.org.uk
     
    Dr Nick, Feb 7, 2011
    #13
  14. "Pushkar Prasad" <> wrote:
    > 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
     
    Joel C. Salomon, Feb 7, 2011
    #14
  15. "Pushkar Prasad" <> wrote:
    > 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
     
    Joel C. Salomon, Feb 7, 2011
    #15
  16. "Pushkar Prasad" <> wrote:
    > 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
     
    Joel C. Salomon, Feb 7, 2011
    #16
  17. Apologies for repeated message.
     
    Joel C. Salomon, Feb 7, 2011
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rudy Moore
    Replies:
    2
    Views:
    441
    Sumit Rajan
    Sep 24, 2004
  2. classicist
    Replies:
    3
    Views:
    332
  3. Reckoner
    Replies:
    11
    Views:
    704
    Steven D'Aprano
    Jan 19, 2009
  4. AikidoGuy
    Replies:
    11
    Views:
    560
    Seebs
    Nov 21, 2011
  5. Philipp Kraus
    Replies:
    1
    Views:
    199
    Victor Bazarov
    Jun 3, 2013
Loading...

Share This Page