- For those times when you need a function
that may take a varying number of arguments,
c plus plus provides functions with variadic arguments.
This is variadic.cpp
from chapter five of the exercise files.
This feature works exactly the same
in both c and c plus plus.
And I'm going to start
by just running it so you can see what this does.
So I'll build and run here
and you see it prints a little message
and it prints an average
and that average comes from this average function here.
So let's take a look at main
and see how this is called.
Message just says
this is a message and then message.
You'll notice that it has parameters
like printf and then an argument after.
So message actually works like printf
but it also puts out a new line at the end
which I find actually kind of handy.
So we'll get to message in a moment.
First, we're going to look at average.
You'll notice like call average
with number five and then five arguments.
And these five values are then averaged to
give me the 62.68.
And if I look at the function itself
you notice its function signature
returns a double.
The function is named average
and it takes a constant int for the count
and then dot, dot, dot.
This is not an ellipsis,
this is three separate periods.
And so that dot, dot, dot
is what's called the variadic argument.
And that makes us a variadic function.
So in order for a variadic function to work,
you need to bring in c standard arg.
And that has the macros in it
and type depths that make the variadic functions work.
We start with va list which is a type depth
and this is the type of the variadic argument handle.
We can call it a handle
and I always name it ap for argument pointer.
And then we have you know, our other little variables here
and then we have this macro call to va start
and it takes the va list and account.
This is the number of arguments
that are expected in the argument list.
And then we can get each argument with a va arg.
And va arg takes two arguments
and this again is a macro that's found there
in that c standard arg
and this takes the argument pointer and a type
so that we know what type the arguments are.
And then every va start
must be matched with a va end.
Releases all a memory
and it cleans everything up
so you don't have a leaky program
with lots of security bugs.
And so this is, in a nutshell,
how variadic arguments work.
And so my average function
it takes that first argument
which is that const int count
and then it takes five doubles.
And you remember we tell it that it's doubles
as we pull them off the argument stack there.
Now message also uses
a variadic argument here.
There's the three dots
but it works a little bit differently
because what we're doing here is we're simply
passing that argument pointer to another function.
And this case this vprintf which is actually
how printf works.
Vprintf takes these three arguments.
It takes the output stream, the format string
which you know, I have here
which is constant character format string
and a va start object.
And here we have va start and we have va end
but we don't have any
of this va arg pulling the individual arguments.
'Cause we just pass this argument pointer
to vprintf and let it take care of all the other.
And so there we have a couple of examples
of a couple of different uses for variadic arguments.
It's worth noting that some versions
of Microsoft visual c plus plus may raise a warning
that this vfprinf function is deprecated
and they recommend that you use a version with
the underscore s after the name,
feel free to do that.
That's a Microsoft specific thing.
The security enhancements
in their underscore s versions are worthwhile.
They include additional type checks
for parametrized values.
Most modern see runtime libraries
including the clang library used on this Mac
already have those security features added
to the standard version of vfprintf.
So that non-standard version with the underscore s
at the end of it is not really necessary
unless you're on Microsoft visual c plus plus.
So this is how variadic functions work in c plus plus.