Passing parameter to a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shoonya
    New Member
    • May 2007
    • 160

    Passing parameter to a function

    what does this declaration mean ??

    Code:
    void Foo( int a, int b, ... ){
    ..
    }
    Shoonya
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Google "variable number of arguments in c".

    Comment

    • curiously enough
      New Member
      • Aug 2008
      • 79

      #3
      A function with no return value, and integer variables passed as parameters to the function called a and b in the function.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Are you familiar with the printf() function? Its first argument is a format string that encodes the number and meaning of the following arguments. Notice that you can pass a different number of arguments each time you call printf() without provoking any compiler warnings about violating the function prototype.

        The function prototype for printf follows:
        int printf(const char *fmt, ...);

        The normal job of a function prototype is to inform the compiler of the function's calling protocol so that compile-time checks can be performed to verify that the function was called properly.

        The "..." construct in a prototype informs the compiler not to concern itself with which arguments appear beyond this point. It is up to the programmer to get it right. You're performing without a net!

        This variable argument list feature provides great flexibility; but at the cost of reduced compile-time checking and increased complexity if you try to write a such a function.

        Comment

        • shoonya
          New Member
          • May 2007
          • 160

          #5
          hi donbock,

          Thanks a lot

          Shoonya

          Comment

          • Flugeldorph
            New Member
            • Oct 2008
            • 11

            #6
            Actually, this methodology is not difficult to use, and is extrenely powerful. An example is the best way to show how it works. The following routine is used as a graceful exit from a program when things don't go right. It is used like printf in that you can send a variable amount of data to the function

            1st the code:
            make sure that you include <stdarg.h>

            Code:
            #include <stdarg.h>
            
            void SysAbort(char *String, int NumArgs, ...)
            {
               va_list       ap;
               va_start(ap, NumArgs);
            
               vfprintf(stderr, String, ap);
            
               va_end(ap);
            
               // Check if n\memory needs to be cleaned up
               MyCleanupRoutine();
               exit(1);
            }
            This can be called as follows

            Code:
            SysAbort("Unable to open file %s from path %s", 2, FileName, Path);
            The '2' in the call tells the SysAbort function that there are two arguments following, the results would (assuming FileName = "InFile.txt " and Path = ""C:\Data\NewDa ta" look like:

            'Unable to open InFile.txt from path C:\Data\NewData '

            The same as printf.

            Comment

            Working...