Thread: How to set default argurments for function?

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    51

    How to set default argurments for function?

    as title

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You don't. C is not C++ and doesn't support that feature. If you really wanted to you could use the preprocessor though:
    Code:
    #define f1(a) f ( (a), 0, 0, 0 );
    #define f2(a, b) f ( (a), (b), 0, 0 );
    #define f3(a, b, c) f ( (a), (b), (c), 0 );
    #define f4(a, b, c, d) f ( (a), (b), (c), (d) );
    
    void f ( int a, int b, int c, int d );
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    why define f1 .....f4?

    Is thats mean when i call the function
    Code:
    f(1); /* it pass in f( 1 , 0, 0, 0); */
    right?

    but why i need to define in this way
    can i do this
    Code:
    #define fa( a ) f( (a), 0 , 0, 0 );
    #define bbx( a, b ) f( (a), (b) , 0, 0);

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    oooh.. i got it... blur

    that mean when i want to pass in one arg

    f1(2);/* ---> f(2, 0, 0,0) */

    then it is no so convenient way...

    one of my friend seen an example that using an external header to archive .. is that possible?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >one of my friend seen an example that using an external header to archive ..
    I have no idea what you're talking about. Can you try to describe it better?
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    6
    I suppose you could neaten up the calling of the function slioghtly by using a variable argument list.

    This uses <stdarg.h>, is that what you meant by external header file??

    The down side is you would have to pass the number of arguments you were using each time. Then save them one after the other to the variables you want, and set the rest as defaults inside the function. You can still only use arguments consecutively in the order you specify. ie

    for

    void f(int num_args, ...);

    you could do
    f(1,2); /*Passing one argument - rest default*/
    but not
    f(1,,2); /*Second argument missing*/

    which is the same as in Prelude's way. It would also be less efficient than Prelude's - take your pick.

    Davey

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C help for network animator
    By fastshadow in forum Tech Board
    Replies: 7
    Last Post: 03-17-2006, 03:44 AM
  2. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  4. Replies: 5
    Last Post: 09-03-2001, 09:45 PM