int square(int x)
This function takes one argument - an integer which is multiplied by itself to give its square.
Now let's look at C's function printf() which is used to print text to the command prompt. We could simply tell it to print a string:
or we could use to print a value stored in a variable:
printf("The value of x is %d",x);
we can do as many variables as we want!
Now if you're like me when I started out with C, you're probably thinking "How does the function know when to stop printing, and what types of variables to expect?" Well we're not going to write out the implementation of printf() as that takes numerous cross-referencing libraries, but we are going to do a function with a variable length argument list that finds the average of a unspecified number of numbers.
The required functions are stored in stdarg.h so we first need to include that:
Now that we have the proper tools, let's look at how a variable argument list is created. There are 4 main "tools" we will be using:
1) va_list identifier
This declares a variable of type va_list. This is sort of like a pointer to your arguments.
2) va_start(identifier, first argument)
This tells the list where we're starting. The obvious location is the first argument. Note that you must have at least one defined parameter when dealing with variable length argument lists (more on this later)
3) va_arg(indentifier, type)
This is what we will be using to acquire the values form our arguments. Note that the identifier used here is the same one used to declare our va_list
This is just to clean up nicely. It tells our program "Ok, we're done with this list of arguments, let's clean up now"
So now that we've defined the necessary tools, let's get to the implementation of the function:
float average(int n,...)
So let's go through it bit by bit:
When declaring a function with a variable length argument list, you MUST have at least one definitive argument. In this case, we are taking that argument to be the number of numbers we would like to average. As we all know, to average numbers, you take the sum of the numbers (float sum), and then divide it by the amount of numbers you have (int n).
So we start off by declaring our va_list and initializing the va_start(). Note that after suing va_start, the pointer now points at the 2nd argument (if available).
Next is our for loop which will loop n times to sum up all the integers. After doing that, we call va_end() to clean up. And then return sum/n, which will be our average!
A sample call of this function could be:
float x = average(3,6,4,5);
In which case, the value of x would be 5 since (4+5+6)/3 = (15/3) = 5
I hope this has been useful! I just thought I would share this piece of information with everyone!