Hello everyone, my name is (name) we are continuing with this C programming course (6 seconds pause, music) in which we had discussed in the last video, error handling.
We have seen that when our compiler encounters an error, it basically stops our compilation process abruptly.
And we don't even understand what exactly was the problem, what was the error? So, all these things we don’t understand and for one user, this experience is extremely bad.
Now it is time to discuss a new topic and this new topic is a recursive function.
Earlier to this we have made a lot of functions in which we have created function definitions, which have called the function, we have declared the function and we have learned a lot of things about the function.
But what exactly is this recursive function? In normal terms we know the meaning of recursive.
The thing that is repeated constantly.
So, can one function be constantly repeated? And what are the conditions where its requirement comes? All these things we will be discussing in this video.
So, without any delay, we will start and we will go on first to the slide where we are discussing what exactly is this recursion.
If you see in the normal terms, if we see in layman terms, what is the meaning of recursion basically? Constantly repeating one thing.
So, as it was happening in the loop that I am repeating one thing constantly.
In the same way.
There is this concept over here but it is slightly different.
How is it different? We will see that.
And if we talk about function, how the recursion happens in the function? It is the process by which a function calls itself repeatedly until some specific condition has been satisfied.
So, we see that when our function becomes a definition, after that we have to call one function.
Only then there is a use of making that function.
While calling that function, as it is we call it only once.
But if I want to call this function constantly until and unless one specific condition is satisfied.
What will I do at that time? I will do such things at that time; what I will do is I will call my function definition in my function call.
So, this must be a little complex to hear.
So, we will see how it can be easily done.
While listening, it might sound a little complex.
But when we practically perform it, it becomes very easy.
We will understand it once again.
“This process is used in place of repetitive computations in which each action is stated in terms of a previous result”.
So, what is it trying to say over here? That we have to repeat one action many times.
But the action will be repeated constantly.
It will be repeated on what basis.
On the basis of the previous action result, it will work on that.
So, all these things have been discussed about our recursive functions.
So, with this we understand that if we want to constantly repeat our function definition, we will repeat our function call constantly.
And how the functional call can be repeated, from that there is one example that function call should be written in function definition.
3:36
We used to write the function call in our main function, we had discussed this in the previous slides but if the same function call, if I write inside the function definition.
So, in one function definition the compiler will come.
So, again when it sees the function call, it will execute the definition again.
we'll see the function call and it will defend it to the candidate who will again execute the definition.
So, in this way, the function keeps running constant.
So, this is called a recursive function.
And what is a way of writing it, that we will be seeing here.
What is its syntax that we'll be discussing here? So, this is a basic function definition.
We all know that while defining a function we write a data type.
Then we write the name of the function.
In front of it we give a list of arguments.
After writing all these things, statements in the function definition start running.
And as and when they run, after that if any such function or statement comes,which will basically be a function call and it should be the call of the same function definition.
At that time the recursion function concept came here.
So, you can see in this syntax or in this example, the function name that was there, i function called of the name of the same function in its definition.
So, this is a definition of my function.
In that I have called the same function, which can see the function name and further they have passed a few arguments.
So, this is a normal syntax, if you are not able to understand it.
There is no problem.
We will understand it practically.
And then we will understand these things very easily.
So, here the basic concept is that the way we used to call our function in our main function, rather than calling it in the main function.
If we call it inside its function definition, we call that as a recursive function.
So, this was about the recursive function’s syntax.
Now after knowing all these things, we will straight away go to our text editor.
And we will understand practically how the recursive function works.
We have come into our text editor and we have written a simple recursive program.
which is starting from some preprocessor commands.
5:52
So, what is this function of the program basically doing? This takes a number from the user and it prints its factorial for him.
We all know that what is the factorial of a number? For example, if I have to get a factorial of four, what will I do? I will start the reverse multiplication of four till the time it doesn't get multiplied by one.
So if you have to get the factorial of four, so I will write four factorial is equal to “4x3x2x1”, till one it will keep on getting multiplied with all the values So, what we will do is we will do 4x3 which is 12, 12x2 is 24 and 24x1 is 24.
So, the 4’s factorial comes as 24.
In the same way if I want to get any number’s factorial, what will I do? I will make one function, which will be repeated constantly.
Till the time it doesn't get any one and as soon as it gets one, it will output the value.
So, in this way the flow goes on.
Now we will understand it with the help of a function So, now we have written a preprocessor command for standard input and output.
After that we have defined one function over here.
Here you can see that and here the function’s name is factorial.
This is of integer type, which means it will return the value of integer.
Along with that if we see here, its argument is N and even that is of integer type.
So, N basically would be such a value whose factorial we have to find.
So, we have to find N’s factorial and we all know that if I want to get zero’s factorial, zeros factorial is one.
So, here there will be two conditions.
If n’s value is zero then what can be done, if n’s value becomes zero.
We have put here an if statement, if n’s value is zero, it will straight away return one and n’s factorial will be one, when its value is zero.
But if its value is not zero, what will happen then? Then it will come in the else part, else part is extremely important over here.
So, what does the else part say? You have to simply return n multiplied by again we have called the same function over here.
So, you can see that inside the factorial function definition, factorial function itself was called.
So, what does it mean by calling a factorial function? We will understand what exactly has happened here.
Suppose you have given four instead of N.
So, four have come here as an argument.
So, it will check if the statement is four is equal to zero but, this will not be true so it will not return here and it will come into the else part.
What will it return in the else part? It will return four multiplied by factorial of n minus one [n * factorial (n-1)] and n was four, so factorial of three will become, which means four multiplied by factorial of three.
So, as soon as the factorial of three will be called, the compiler will start this definition again.
Now, here in n instead of four, three has come and it will again start running the statements one by one.
So, it will check whether three is equal to zero, it's not and then it will again come here and what is happening here, three will come and three multiplied by factorial of three minus one, which is two.
9:27
So, first if we would see from outside, four multiplied by factorial of three it was.
And what did it do to count factorial of three, it did three into factorial of two and how did it count factorial of two, two multiplied by factorial of one.
So, in this way the loop is basically, till the time one is not encountered, or zero is not encountered.
So, this is the flow of ours to calculate the factorial, will go on.
You all know that, like I have to calculate the factorial of four.
How will I write it? “4 multiplied by 3 factorial”.
If I want to count a factorial of five, I will write “5 multiplied by 4 factorial”.
So, here in the same way, I have to get of 4, then 4 multiplied by factorial of 3.
And if I want to get the factorial of three.
Then “3 multiplied by factorial of 2”.
So, in this way in factorial one by one the value reduces and it keeps factorial counting.
Until the time its value doesn’t become zero.
As soon as its value becomes zero it returns one.
And here the function that we have here, the value that has come after multiplying by one.
That remains as it is and we get our factorial.
So, this was about the recursive function where you have to call the function in the same function definition.
We call that a recursive function.
10:52
So, this was about our function definition.
Now, if we move ahead to our main function, here we have defined the two variables.
One variable is called num and second is called facto, in num we will take that number which we have to get the factorial of.
And in facto those values would come which are the factorial of num.
So, here you can see that from scanf we have taken a number, which is stored in NUM, in num.
And then we have put here a function call, factorial num.
Here if we suppose that in num, four was given.
So, factorial 4 will be called.
And it will come in our function definition.
Then if in n, four comes.
Then slowly it would be 4 multiplied by factorial of 3.
Then what it will do to count factorial of 3 multiplied by factorial of 2.
In this way it will keep on going till the time one value is not encountered.
As soon as this process gets completed.
Factorial num’s value will be generated.
That will be stored in what? In facto variable.
As soon as it gets stored in the facto variable, we will print it with the printf statement, “factorial of num is is equal to facto”.
So, in this way we will take the factorial of our number, now what will we do to run this? We will again go to our terminal, we will click on this option and what will I do here is GCC, first of all I will clear the entire terminal by typing CLS, now I have cleared the terminal.
Now what I will do is I will write GCC, after that I will write recursion and I will put dot c which is my file’s name and I will press enter.
Here my compiler will compile it.
Now after compiling I will have to run it, simply I will write my files name over here.
As soon as I write my file’s name over here.
You can see that it will compile me and run and it will take a number input from me.
So, I will type here four.
12:59
After four, I will have to press enter.
And you can see that it has printed and given me that, “factorial of 4 is 24”.
So how did it get calculated? “4 multiplied by factorial of 3” and what was done to calculate factorial of three, “3 multiplied by factorial of 2” and how do we calculate the factorial of two? “2 multiplied by factorial of 1”.
And how factorial of one was counted? “1 multiplied by factorial of 0”.
And if a factorial of zero comes then it will return one.
So, it will get multiplied by one and the factorial of 4 which is 24, it will get printed.
So, if in this way we have to remove factorials or we want to print any Fibonacci (pronounce- fibonaaki) sequence or different types of sequences that we see even in mathematics, that we have to get printed.
What can we use there? Our recursive functions.
So, this functionality of recursive function is used in two different situations and different scenarios.
Here we have taken a basic example of finding the factorial.
And we saw that with the help of a recursive function, we can calculate this number’s factorial.
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.
The next topic would be variable argument.
This concept we will be discussing in the next video till that time, thank you so much.
Share a personalized message with your friends.