Course Content

  • variablearg

Course Content

Variable Arguments in User Defined Functioncs

Hello everyone, my name is (name) and we are continuing this C programming course (7 sec pause)

 which we had discussed in the previous video recursive function.

We have seen in normal functions, the function definition and the function call, these two are written differently, which means the function definition is written outside of the main function and the function call is written inside the main function.

In this video we will be discussing the functions, the arguments that we pass or the parameters that we see, their numbers, how they should be decided.

We have to do such a thing in which at the start, we don't know how many arguments I will have to write in one function.

So, at that time, how should we make the function, all these things we would be discussing in this video.

So, we are discussing variable arguments topics where we will make variable arguments in a particular function definition.

So, without any delay we will start.

So, a function which takes a variable number of parameters instead of a predefined number of parameters, for them we use function definition with variable number or arguments, which means such functions inside which at the start we don't know how many parameters should be written.

What do we do at that time? We make such a function definition in which the arguments that are there, the number of that is not fixed.

It is variable and can be changed.

These kinds of functions that we make the arguments that we pass we call them as variable arguments in a function.

So, it is clear that if you don't have to keep a fixed number of parameters, if I want to change the parameters according to the situation then at that time, I will make one variable argument function.

So, if we talk about the syntax first of all here the function definition has been given in which the function’s name is FUNC, which means func (pronounce- funk) and ahead of the argument list that you're seeing, the parameter list that you're seeing, in that the first parameter which is there, that is written that it is of integer type.

But ahead of it, you can see that there is a comma and there has been three dots that have been put, which means that we don’t know how many numbers of arguments are going to come ahead.

Here the first argument, which is of integer type.

What does it denote here, it will denote here that how many arguments will come in the future whenever we will call this function? So, the value of the number of arguments, those will be stored in our first argument.

It might seem complex by listening to it but when we will see it practically it will be very easy for us.

We will move ahead, we don't have to do much in our function definition.

We just have to define the first argument in our parameter’s list.

And that would be of integer type because how many arguments should come is getting stored in it.

After this in our function definition, we will be using one or two more predefined functions, we will see those practically, we will not understand it here.

Here we only have to understand that if we want more than one, which means we want variable types of arguments, we will put three dots in our argument list.

Now, we will go ahead from our function definition and go into the main function.

So, here you can see that our FUNC function which was there, in that function call we have passed three variables, which means we have passed three arguments also.

And in the same function call, which means in the same function, we have also given four arguments.

If this is a situation, in which using one function, we have to handle three arguments or we have to handle four arguments.

In that situation we will be using the variable argument’s function.

So, this was our simple syntax, how the flow of a variable argument function is written.

Now we will move ahead and straight away go to our practical part where we will make one simple function in which there will be variable arguments and we will see how these functions are written and made.

Now, we will straight away go into our Visual Studio Code and here we have already written one program here which would be related to argument function.

So, before reading this we will understand what is this program going to be about? This program would be how to calculate the average of a few numbers.

Suppose we have got three numbers and we have to calculate the average of these three numbers.

So, I will use this function and calculate the average of these numbers.

After that if I'm willing or if there is a certain scenario where I have to calculate five numbers on average, with the same function definition I can calculate even the average of those five numbers.

Such kind of situation I will have to create over here.

So, how will I be able to do that, we will see that when we will be making variable argument functions, which means you can put any number of arguments, 3, 5 or 10, etc.

In the start, at the time of function definition.

It doesn't make a difference how many arguments we are putting forth.

We will start first of all with our preprocessor command so, first preprocessor command is # (has) include, STDIO dot H, the standard input output file that is there, what we have imported here.

After that the other preprocessor command is also extremely important over here because this file is STDARG dot H, which means a file with standard argument.

After importing this file, the variable argument functionality which is there, we can use it in our functionality.

To complete the functionality, we also have to use some predefined functions, which are actually defined in this standard argument dot h file.

So that's why we have to import this over here.

6:27

So, this was about our preprocessor command.

Now we will straight away come to our function definition.

If you see in the definition our return type of function is float and average is function’s name as we will be calculating average of the numbers.

And you can see that in the argument’s list, the first argument that is there.

because we will take out the average of the numbers and you can see in the arguments list.

First argument has been defined properly because it is of integer type and its name is NUM, num.

And what will be stored in it going ahead? It will be stored here how many arguments will be handled.

Here in num, there is no value stored because of which it will be clear that our function definition which is there, at that time we are not aware that how many values to handle.

But when the program would be running, at that time the first argument that we would pass with that this would be clear to our compiler or our program that how many arguments we have to handle.

You can see that the first argument that is there- num, inside that we will give one value which will tell us how many numbers of arguments we'll have to handle over here.

And after that in this function definition, we'll have to put the comma and put three dots.

We don't have to write anything else further to that.

With these three dots we get to know, which means the compiler gets to know that this is one variable argument type function.

So, this was about our function definition title.

We will come inside of it and we will understand one by one, what are the things that we have written inside of it.

So, if we talk about the arguments, the argument's number is not fixed at the start.

It can be 5, 10 or even 15.

So, we don't know how much size this kind of storage space we will have to make in which 5 can get stored.

even If 10 are coming,10 can get stored, if 15 are coming then 15 can also get stored.

So, we will not pre define it that I want to keep 15 storage space.

So that if maximum 15 comes only then I will be able to handle it.

If 20 arguments come, I will not be able to handle it.

So, to encounter this situation.

We have introduced this technique in which we make “VA list”, one data type list.

So, we all know what is list in English, list of items, like when we go to a grocery shop, we have to buy few items. In the same way, even here we are making a list which is data type, “VA_list” type.

 And where has this “va_list” data type come from? This “va_list” data type has been imported from the standard argument dot H header file.

So, why are we making this “va_list”? In this we will be storing the arguments which has to handle this particular function? So, whenever we have to make (inaudible) this variable argument, this step, this variable list step is very important to make.

After that we will see that ‘sum variable’ we have made.

And it has even been initialised with 0.0 (zero point zero) Along with that we are made a variable with name I.

Whose data type is integer.

So, till here we are able to understand very easily.

So, now we have made one list and in that our argument are getting stored.

 But till now we are not aware.

We are not aware that in our list how many values would come and along with that we don't even know that which all values will come.

So, when we get to know that or when our user stores some value in this num like 5, 6, 8, 10.

If anyone stores this type of values.

With this it gets clear that there will be 8 variables or 8 arguments that will come or may be 10 or 15 arguments.

After that size of the lists sets accordingly.

And to set that list we use the “va_start” named function, which again comes to us with standard argument dot h header file.

Here, in this particular function we pass va-list, which we had made as it is and we pass in the second argument n u m variable, which means that variable in which means it will be stored in it, how many numbers of arguments are going to be in this.

Like in num 5 is stored.

So, we have to make va-list file of 5, so that is the reason we use “va_start” function.

So, we have made a va-list type of list.

We started that and we have also defined size in it.

(inaudible).

It should be the size of storing 10 arguments.

After this what we will be doing is we will be accessing the arguments one by one.

How we will access those, that is what we will see.

If we have to access one by one, so obviously we will require for loop.

So, exactly the way we used to use array.

This is one type of array.

And like the array as per the index value we were moving ahead one by one.

In the same way even here, in the va-list, in that we will extract the value one by one or access it.

So, for that we are using one loop over here.

Loop is starting with 0 and will go on till 1 less than num.

Right? If num’s value is 5.

It will go on till, 0, 1, 2, 3, it will run till 4.

So, in total 5 times this loop will go on.

And with this i++ (i plus plus), the value will get incremented.

So, what are we going to do here? Inside the sum variable, sum + VAR, we are using one more function here.

And what does this function do? It helps to extract each value from the list we had made.

So, how does this work? We will see that.

Here we have passed the first argument - va list.

It's our list’s name.

And the second argument is integer.

What is the integer telling?

12:39

It is telling us the particular index position over here.

So, what will we do? One by one with the help of the “va_arg” function’s help we will extract the particular argument and store it in the valist.

So, in valist our values or arguments have come, now our average function can handle those arguments.

So, its logic you can see over here, that logic has been used here very easily.

 As soon as you calculate its average.

After calculating the average, the list which we made as a valist, we will also have to close it.

That’s why we use this “VA_end '' named function and we pass valist in it.

If we want to make basically one variable argument function, what all things will be required? We will first require a va-list type list.

Then to the list we have to define what should be the size of the list, that we define with the help of the “VA_start” function.

And after that with the help “va_arg”, which means with the help of variable arguments.

We extract the values one by one.

And our function gets to know how many in total arguments and which all arguments it has to handle.

After all those things, we close that list with the help of “va_end” function.

 And after that we return simply “sum divided by num” because we have to take an average.

So, the total sum should be divided with the number of values or number of arguments.

As soon as we will divide it, we will return it from that function.

It will be returned in which form? It will be returned in float form as its return type is float.

So, here we have understood that if we have to calculate five number average, how we must use it or we have to calculate ten numbers average, how we can use it.

So, if we come here to the main function, you can see that the print-f statement is written and what have we done 2, 3, 4, 5, we have calculated four numbers on average.

If we have to take four numbers on average, in an average function called the first argument which is there, that we would pass by the total number of arguments that we have to put there.

So, the total number of arguments are four, which means there will be four number’s average over here, which we have listed in front of 4, which are 2, 3, 4 and 5.

Similarly, in the below print-f statement we have to take our three numbers average which is 5, 10 or 15.

Here the first argument we have passed 3, with which the compiler will get to know that 3 arguments are going to pass further.

So, these three arguments we have given there by separating with a comma, 5, 10 and 15.

This was a story about our average function where our number of arguments were not fixed.

And it was defined at the run time, meaning at the time when it was being used.

At that time, we defined how many arguments should be there.

And based on that our function reacted and it gave us some output values.

So, this entire process we have understood it.

Now there is a time to run it.

Now we will go again on our terminal and here I will write “gcc variable arg dot c”, which is my file’s name and I will enter and as soon as I enter, my file will get compiled.

And after that I have to again write my file’s name and here my file will run.

Variable Arg is my file’s name and as soon as I press enter, you can see here that there is some error that has come, here we will understand what is the error.

“Variable Arg is not recognised as an internal or external command.” So, that's not a problem.

We will again open one more command prompt, which means terminal.

And we will go into our folder which is a C folder of C programming and we will again run it, which means we will compile this file, “variable arg dot c”.

We will check the spelling once, VARIABLE ARG dot C.

So, this is correct.

In front of GCC we have written the file’s name and we will write it again VARIABLE ARG, after writing this we will press enter.

I don't know why this is happening.

We will check it once.

So, here you can see that our program has run, which means the issue that was coming here was nothing.

It was a Visual Studio Code issue.

So, you can close it and start it again.

If you get this type of an error, you can again type “gcc variable arg dot c”

 whichever is your file name.

After that you have to only type the file’s name without any extension.

As soon as you type it.

You will see your programmes output over here.

Here like we have printed an average of 2, 3, 4 and 5 which has come to 3.5.

In the same way we have calculated the average of 5,10 and 15 which has come out to be 10.00 ( ten point zero zero).

So, here we saw that if we want to calculate the average of four numbers, three numbers, 10 numbers.

Then we can calculate the average of all those numbers because we are using one variable argument function, which basically doesn't define at the start, how many arguments it wants.

but It considers the number of arguments on your run time, which means when your code is running.

And it accesses each argument one by one.

And then it performs whichever operation it has to perform on them.

18:58

So, our variable argument function works in somewhat this way.

 If you think that there is any part of this video or this topic, you might have not understood then you can ask us any query or question related to that topic without any inhibition.

If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.

In the next video we will be seeing about memory management.

So whichever variables we make in C programming or whichever values we store, how to manage those.

This is what we will be basically discussing in the next video.

So, till that time, thank you so much.

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere