Recursion in C Functions | C Language Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to Norwich technologies so this is seanwes so today we are going to discuss so one important concept in functions so that is a recursive function so what is a recursive function so very simple definition function calling itself is called a recursive function so what is that function calling itself is called recursive function simply calling a function from the definition of a same function calling a function from the definition of a same function is called recursion we will see one example recursion recursion function calling itself function calling itself is called a recursive function here so one simple example I am writing in the main function one statement printf started next statement I want to call a main method explicitly and of course first time the main method executes by operating system and second time we are calling the main method main this is calling and next here we are writing one more statement printf function here it is end start mid says and n misses two statements we have written in between start and end so we are calling the main function how it executes so generally to execute functions in the application right inside the RAM a memory will be allocated the memory is called stack memory this is not only in a C language in any programming language stack memory is common any function or any block executes inside the stack memory only to execute a method or block some amount of memory will be allocated inside the stack that is called frame that is called what frame a particular amount of memory will be allocated for every method execution see first time whenever we start executing this program operating system invokes main method so two main method some memory will be allocated so here the main frame will be created so amount of memory will be allocated to execute all the three statements defined inside the main method so first statement execution printf we are printing the method just printf a message start start message it will print here and what is the second statement we are calling main method explicitly we are calling main method explicitly sir for this method where is the frame yes one more frame will be created to execute this main method another frame will be created so first control move to here and inside this main frame again all the three statements we should execute all the three statements means a printf again the first statement is the start and what is the second statement we are calling main method but where is the frame again the frame will be created main frame will be created and here and here also same a first statement start it will print and next second statement we are calling main method and one more frame will be created main so in this program every time the control is moving in a forward direction only once all the three statements execution completed then the frame will be deleted and the memory will be assigned to allocated to some other execution of method but here every time the control is moving in a forward direction only for the control there is no chance to come back and complete the execution of a previous frame so it will go like that somewhere that memory will be full here it is giving one error runtime error it will give runtime error out of memory your program will terminate abnormally so because sufficient memory is not there right sufficient memory is not there in a stack to come to new the execution of your program so that is a problem so while working with the recursive functions so we should bit care right while writing the logic because in which order the control is going in a forward direction in the same order the control should come back and where it starts the execution there only it should complete the execution in that format only we should write the programs so for that so one best example I will explain in a recursive function that is factorial of a given number using a recursive function how C so one example so factorial of a given number first program I will write and then as I will execute with the help of method spaces so was the program main in the main method I am declaring one variable N and we are asking printf enter n value and we are reading scanf % is d into address of n we are calling a fact method and we are passing in fact is a method which is used to find the factorial of a given number fact of n so we need to write the body for that fact method fact we are passing in n is of type word integer so here we are collecting into same variable or different variable that is your interest right here it is we are collecting into N only and next here it is one variable we are declaring re s result suppose consider if n value equals to 0 so what is the factorial of a 0 it is a 1 result is a one result equals to one one will be assigned to result or else and this is the formula n into fact of n minus 1 re s equals to n into fact of n minus 1 this is a formula to find using recursion and finally after execution of offense it will return the result returned the result only one statement is belongs to if block are only one statement is belongs to else block means no need to write in a braces that is why if you are confusing just you can write simply this one open brace and close brace right and of course for a single statement that is not required because a compiler directly consider the statement which is followed by if block and the statement which is followed by else block is considered as a if and else block statements only one statement which is followed by the block and it is returning the result result is of type word integer type so return type is a integer and whatever the value will return so that will be collected into variable same variable we are taking no problem re s and we are printing printf here it is the result percent is d and here it is re svr printing so this is finding the factorial of a given number using recursion sir where is the recursive calling simple fact method we are calling from the definition of a same function fact fact function calling from same same location okay how it executes hauser method spaces will be created how the control will go in a forward direction and the control how it will come back see here so first memory will be allocated to main frame starting frame main frame we are declaring the N value n value and one more variable result and we are reading n and next step it is calling a fact method and what we are passing suppose n value we are taking four fact of 4 the result will not be assigned first fact of 4 method should complete so method space will be allocated to fact of n value 4 then the fact method execution fact method execution so how the tracked method executes observe first if n is equals to 0 if n value 4 equals to 0 condition false so if block will not execute else block executes in the else block n into fact of n minus 1 executes right side data n value is 4 into fact of n minus 1 that is 3 but 2 multiplied to perform this multiplied operation first we should know what is the value of fact of 3 so first we should find out the factor of 3 again fact N equals to 3 and this is the method space factor of 3 method space here if 3 equals to 0 condition false if block will not execute else block and here it is 3 into fact of n minus 1 that is 2 again we should find what is the value of a fact of 2 here it is this is fact of 2 fact of 2 method space same story if 2 equals to 0 condition false if block will not execute in the else block 2 into fact of 1 and here it is fact of one method space will be created if 1 equals to 0 again condition false if block will not execute in the else block one into fact of zero it is a last method space in this execution fact of zero fact of zero now observe if zero equals to zero yes if condition is a true now else block will not execute in all the previous method spaces if block is terminating and else block is executing but now if block will execute and else block will terminate what is the if block statement we have written just we are assigning one equals to result so result equals to one into the result one will be stored after if-else what is the last statement we are returning the result yes written it will return the result is called one one will written back to here sir why because a fact of zero function executing from here only so result of this fact of zero function it will return back to the same place so 1 into 1 the value will be stored into result and here it is the last statement executes it is also returning one because one is multiplied with one result is a one and it will return back to here now it is multiplied with a two that will be stored in to result it is returning that result what is the return value - so - where to return here to here and again here it is a 3 is multiplied with a - 6 will be stored in to result it is returning that result 6 and 6 it will return back to here 4 is multiplied with a 6 that will be stored into result and it is returning that result 24 that will be finally stored into main method result variable result collected 24 and we are printing that result 24 this is the factorial of a 4 so in a recursive recursive function execution method spaces logic very very important how the control is moving in a forward direction and how the control is coming back every method you should understand and every method you should write right to understand the exact execution flow of a recursive function okay so this is completely about how recursive function executes in a C language so not only in a C language recursive function means the function calling itself is called a recursive function and this is how the execution flow will be the best example of recursive functions is a factorial of a given number and so many examples are there right all these examples you can try for four more examples right just log into the nourish ID channel okay we'll see more videos right in the coming sessions thank you thank you
Info
Channel: Naresh i Technologies
Views: 690,330
Rating: 4.920208 out of 5
Keywords: C Language, Recursion in C, Srinivas, Naresh IT, Hands on C Language Training, C Language Demo, Online C Language Training, C Language Tutorial Videos, C Language Overview, C Language Interview Questions
Id: SUfe1UX1m4M
Channel Id: undefined
Length: 14min 32sec (872 seconds)
Published: Mon Sep 12 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.