In this article we will discuss how to pass variable number of arguments in a function.


Suppose I want to create a function that take variable number of arguments and use them to perform some task.

For example,
Create a function that will return the sum of all the passed arguments i.e.

As you can see in above example in function findAll() we have passes different number of arguments in same program and it used them to show the result.
So, we need to create a function that can accept variable number of arguments and use them.

Now lets see how to implement this functionality

Declaring a function that can accept variable arguments

Here … is “ellipsis”, which represents variable number of arguments.

Imp Points:
First agrument is neccasary in a function that accepts variable list of arguments.
Also variable number of arguments represented by … (ellipsis) should be the last argument in a function.

Reading Variable Arguments inside a function

Create a list of variable arguments using va_list i.e.

Now initialize the list using va_start() i.e.

Here first agrument is the list of type va_list and second argument is argument that is passed just before the “…” in function.
This function va_start() initializes the va_list.

Now to read the first entry from this va_list use va_arg() function i.e.

This function accepts the va_list and type of argument as argument and return the next entry in list.
We will iterate through all the variable arguments passed in the function by calling this function in a loop.

But how do we know, how many elements is passed in this variable list?

For this we will use the first argument passed i.e. noOfElements. User will the count of arguments in this argument i.e.

Now lets iterate over the variable argument list

In the end we need to clean up this variable argument list i.e.

va_list, va_start, va_arg, va_end are defined in header file <stdarg.h>

How this variable argument stuff works internally

Whenever we pass some argument in a function, all the arguments are stored in a stack and if we know the address of starting point of variable argument then we can easily fetch the next value.
Therefore in va_start we need to pass the argument that jus preceds the variable arguments, so that it can fetch the calculate the starting address of variable arg list.
Then in va_arg we need to pass the type of next argument, so that it can fetch that bytes only to create the argument.

Complete example is as follows,


Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.