#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Can anyone give me example of multiple arguments function?


    Can anyone give me example of multiple arguments function?

    Like
    void display(int a,...);
    For Example i would like to display the value of integer type variable by multiple arguments function.
    Eg.


    If i pass
    Function: Output:-
    display(2) --> 2
    display(2,3,4) ---> 2 3 4

    I want a single function that accepts multiple arguments. I know this is possible but dont know how?

    Help Will be appreciated.
    Thank you.
    Regards,
    Ashish
  2. #2
  3. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    Look up "variadic functions." (varargs)
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Solution for Turboc3 Compiler?


    I want solution for Turboc3 Compiler?
    I tried putting void display(int x,...);
    it accepts ... means there would be some solution.?
    Can you post example for turboc?
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Dawei posted a perfectly good answer which works for TurboC 3 as well (I know it worked in TurboC 2.0 for sure). Have you tried googling for those terms + example.

    Also, just so you know, TurboC compiler is outdated and you shouldn't be using it. Tell your teacher I said so.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Are you asking about functions with variable numbers of arguments? Such as (from stdio.h):
    Code:
    /*
     * Formatted Output
     */
    
    int	fprintf (FILE*, const char*, ...);
    int	printf (const char*, ...);
    int	sprintf (char*, const char*, ...);
    int	_snprintf (char*, size_t, const char*, ...);
    int	vfprintf (FILE*, const char*, va_list);
    int	vprintf (const char*, va_list);
    int	vsprintf (char*, const char*, va_list);
    int	_vsnprintf (char*, size_t, const char*, va_list);
    By the way, I doubt that you could get away with the notation for printf.

    Or are you simply talking about functions with more than one argument? Such as (again from stdio.h):
    Code:
    /*
     * Direct Input and Output Functions
     */
    
    size_t	fread (void*, size_t, size_t, FILE*);
    size_t	fwrite (const void*, size_t, size_t, FILE*);
    Though you should have been able to find that in your textbook. And in the documentation for the standard library functions.

    Of course, those are function prototypes which only need to know what datatype each parameter is. When you write the function itself, you will need to give each parameter a name in addition to the datatype.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    I am talking with respect to int printf (const char*, ...);
    Also I was looking for its implementation. If I could find it may be i could easily understand how it works & I could write my display function.
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    click me here is the source to the working guts of printf. You'll easily understand it and be able to write your own display function.

    Comments on this post

    • dwise1_aol agrees : [voice="Homer Simpson"]Mmmm! Jump tables. Sprinkled liberally with macros. [sound="prolonged heavy drooling"]Aghhhh![/sound][/voice]
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo