1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

How to Write Functions That Accepts Variable Number of Arguments

Discussion in 'C' started by poornaMoksha, Sep 30, 2011.

  1. poornaMoksha

    poornaMoksha New Member

    We all have been using Functions since we stepped into programming. Numerous times we have called a function with some arguments to it. We all know that the basic declaration of a function is :

    <return-type><function-name>(<comma seperated list of arguments>);

    For example :

    int func(int a, char b);

    Most of the times, we would have seen the argument list of a function contains fixed number of arguments. Have we ever thought of a case where number of arguments for a function is not fixed. This is what we are going to learn today. Variable list function arguments.

    A Function with varible list argument is declared as follows :

    int func(int num, ...);​
    To represent variable argument list, the ellipse '...' is used. This ellipse tells the compiler that this function accepts varibale list arguments.

    Now, how it happens :

    We'll need to use some macros (which work much like functions, and you can treat them as such) from the stdarg.h header file to extract the values stored in the variable argument list.
    • va_start - which initializes the list.
    • va_arg - which returns the next argument in the list.
    • va_end - which cleans up the variable argument list.
    To use these macros, we need a variable which is capable of atoring variable sized argument list. 'va_list' type of varibale can do exactly the same. A va_list klind of variable is declared as :

    va_list list​
    The va_start macro accepts two arguments :
    • A va_list type variable
    • The argument that directly preceeds '...' in the function decalaration.
    So, for example, in the following function, to initialize a list with va_start, one would write :

    Code:
    int func ( int x, ... )
    {
        va_list list;
        va_start( list, x );
    }
    Now, to fetch the arguments one by one, the macro va_arg is used. This macro takes two arguments :
    • 'va_list' type varibale (which is already decalared and used with va_start).
    • A type in which we want the first/next argument is to be returned.
    It is used as :

    int ret = va_arg(list,int)​
    The above va_arg operation returns the next argument as an integer type.

    Example



    I think the above explanation is enough to understand the following example :

    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    int add(int num, ...) /* A function which accepts varible number of arguments (atleast 1)*/
    {
        va_list arg; // decalre a list
        int sum = 0;
    
        va_start(arg,num); //Initialize the list
    
        int i = 0;
        for(i = 0;i < num; i++)
        {
            sum += va_arg(arg,int); // Fetch the next argument as integer type and add to 'sum'
        }
        va_end(arg); // de-initialize the list
    
        return sum; // return the sum calculated
    }
    
    int main(void)
    {
        int a = 1,b=2,c=3,d=4;
    
        int sum = 0;
        sum = add(4,a,b,c,d); // Pass 5 arguments
        printf("\n Sum = [%d]\n",sum);
        
        sum = 0;
        sum = add(2,a,b);//pass three args
        printf("\n Sum = [%d]\n",sum);
    
        sum = 0;
        sum = add(3,a,b,c); //pass 4 args
        printf("\n Sum = [%d]\n",sum);
    
        return 0;
    }
    The above function tries to calculate sum of variables. There are three calls to function 'sum', each with different number of arguments. In the definition of function 'sum' we used variable argument technique to calculate the sum.

    Hope the above examples makes the concept clear and usable.

    Conclusion



    To conclude, for some rare functions (like printf() ) for which we cannot fix the number of arguments, this technique of varibale argument list is used. This is an important concept for any language that supports it.

    Stay tuned for more!!!!!
     

Share This Page