same happen with me today...I thought, just wasted my whole day but this video cleared my whole concept. hence my hard work pays off that I found this video....thankyou so much.
Tail recursion performance is considered better than Non-Tail recursion because tail recursion is the last line executed so there's nothing left to do before leaving the function, this helps the compiler to optimize the execution because the function's stack frame need not be saved
Both are Non Tail Recursive function. In Program 2 the range of int from -2^(31) to +2^(31)-1....When n=2^31 then it exceeds the range of int and again start from -2^(31) which is less than 0..
Most of the people think that the 2nd program is wrong and does not converge into the base case but it actually converges... It ia because int overflow even when the output is 1(the smallest positive number) the recursion can run only upto 32 times because an int is 4 bytes which is 32 bits... At n=1 the first bit is set and when n=2 second bit is set and so on... When it reaches the 32nd bit n is no longer a positive number and the program returns.
Both are non-tail recursion. I executed Program 2 in Visual Studio Code. I took n=3.I got output and it prints from 3,6,12 upto byte limit and again return that from byte limit to 3. Program 2 is not infinte loop. It executes upto the byte limit of the computer and last line is also executed after return.
But in second program indeed value got true and finally if got executed but there is a printf before recursive procedure and after if being executed, there is, that printf statement left to be executed and second thing we didn't put rescursive procedure in else construct which mean still recursive procedure got executed anyway and in program 1 when value got zero recursive again isn't in else construct it still executed value 0;
Part 1 and part 2 both are great examples of non-tail recursion, in fact, 2nd code is very useful if you want to revise the concept of range of "int" along with studying type of recursion, ..... In the same code(2nd homework code) if we change any one of the two printf statements and print 2*n instead of n, then we'll notice that as the size of n doubles each time during recursive calling then as it reaches 1073741824, the recursive function that will be called by fun2(1073741824) will be fun2(-2147483648) [since max value of int is 2147483647] and as -214.... is less than zero therefore the base condition is met and thereafter the second printf statement will start getting executed and the functions will simultaneously pop out from the stack. The output obtained in this case (where we are printing 2*n instead of n clearly shows this)
Yes both the 2 programs are non tail recursive because the recursive call is not the last thing done by function and in both the process we need the previous function record.
No only 1st is non-tail because in 2nd function will never return and last printf will never get evaluated so here only function call is the last thing done so tail recursive. No Sorry last printf will be evaluated as integer overflow will occur and n
1 2 non tail 1) value can be zero 2) value can not be zero as well 1) n=4 then 4/2=2 2%2=0 done. 2) n=1 then pt=1 , 2*1= 2 , pt=2 goes on... value wont be zero any value after zero can never be greater then zero.
Thank you all for these videos, my master. Probably you did intently but in the end , program 2 will get an stack overflow error. That doesnt satisfy the base statement.
The 1st program is a non-tail recursive, but the 2nd one is a tail recursive program. That is because the recursion will enter an infinite recursion making it impossible for the printf to be executed. Therefore, the recursion remains the last thing to be called.
Both of the given questions are the examples of non-tail recursive functions. As we are performing a modulo operation in the printf statement of first program and printing the value of n in the second program.
It is returning void function so it is absolutely fine. And other easy way to understand is fun(n) returning to fun(n-1). And if function is void it doesn't mean that function doesn't return anything it returns void.
A very big THANKS...... You are really doing a very great job ..... I was really very confused about tail and non-tail both ..... Even after asking my lecturer thrice I won't be able to get it properly..... But u made me understand ...... A very big THANKS again.....
I assumed n==5; When fun(2*5) will be called, it will start to print 5 10 20 40.......1342177280 Iteration/recursion is possible until value of n reaches 2147483647(max range for signed int) Since we are using fun(2*n) i.e. when value of n reaches 1342177280 then after 2*n will exceed the range of n i.e. fun(2684354560) , i.e. value of n will become -ve (lec 8). And as stated if(nn i.e. n=1342177280 i.e. fun(2*671088640) and fun will pop out then 671088640 then fun will popp .... ..... n=10 i.e. fun(2*5) Now fun(2*5) will popp out and control will return to main and then main will popp out.
The printf does evaluate. I've tested both programs and found out that both are non-tail recursive. I entered an input of 8, after hitting 1073741824 it went down to -2147483648 and triggered the base case. After the integer overflow, it prints the result in reverse. Please explain if I'm wrong.
Vivek nirma you are right 1.non tail and 2. Tail because printf is not evolut it may be leads to stack over flow so printf function not be evoluted so it is an tail recursive and in program 1. Fun2(0) arises and then printf should be evoluted so it is non tail
In case of tail recursion there is no need to maintain the stack because the function call is the last thing thats happening. That is why tail recursion is preferred.
1st is Non Tail but 2nd there is no base condition therefore it'll be in an ∞ loop but I can guess it as Tail because there there is nothing to do after calling the function in the function
@@AbhishekKumar-kk6qs but when 2*n will exceed the integer range based on computer, then n will become negative( let us assume if int is 2 byte then after 32766 n will become -32768) and it will return, so I guess that 2nd is also non tail recursive
both are non-tail recursive function because 2nd function is dependent upon the value you passed, if it will be greater number then the value multiplied with 2 will doubled and so on therefor 3 or 4 values are only printed by 1st printf function and then value will be negative (you know signed int concept) so 2nd printf will execute,too. for ex you can pass n as 536870900(nearer value of max signed value/4) so you get only 3 values WHICH is printed by 1st printf and 3 values which are printed by 2nd printf(which is same but in reverse position). SO, AT THE END NOTH ARE NON-TAIL RECURSIVE FUNCTION
both are non-tail recursive function because 2nd function is dependent upon the value you passed, if it will be greater number then the value multiplied with 2 will doubled and so on therefor 3 or 4 values are only printed by 1st printf function and then value will be negative (you know signed int concept) so 2nd printf will execute,too. for ex you can pass n as 536870900(nearer value of max signed value/4) so you get only 3 values WHICH is printed by 1st printf and 3 values which are printed by 2nd printf(which is same but in reverse position). SO, AT THE END NOTH ARE NON-TAIL RECURSIVE FUNCTION
I think for negative value it will return back to main() [or the function that called it] Because.. the if condition becomes true so it returns back to the function that called it..
program 1 - not-tail program program 2: this program is very interesting though . if u paas a value than it will stop at that same value because when it exceeds the range of data type it has to come back.
Not stack overflow, integer overflow. Stack overflow is different than integer overflow. Both are non-tail recursive. In the second program, integer will hit its limit and will become negative negative number and it will hit the base case. It is non-tail recursive because it will evaluate the printf statement below.
No, Program 2 is non - tail recursive. Check out output in Visual Studio code. Func will just executes upto the Byte limit and starts returning back and last line is executed.
why was the activation record of fun(0) not stored in the stack in example 1?? The activation record is stored when a function is called right? fun(0) was called.
The stack usage is the same. you didn't show the optimization (by compiler) gained from the fact that it's a tail recursion. Someone can argue about the need to differentiate them since we may assume that explicit/implicit return is the last instruction that triggers the stack pop().
Bro you use the word "evaluate" in a wrong manner. I think sometimes you mean to say "execute" which is different than "evaluating". You don't evaluate a print statement, you execute it. Thanks for the tutorial though :)
@@madhusudansahu5482I do not know how you came to this conclusion. The second function is recursive for sure because it call itself. The recursion is also non-tailed. The n variable become negative because it overflows at 2^31 (integer variable). After that the second printf gets executed. Finally I tested my conclusion with the program: #include #include void fun2(int n) { if (n
program 1 is non tail recursive , but I can't say about program 2 because it will call itself again and again. since n*2 will increase the value passed to the fun2 and it will never go to the end condition i.e. n
Program is 1 is Non Tail Recursion and Program 2 is Tail Recursion because after if condition print execute but it is no need to store then second recursive call not get value of n .
After searching everywhere for a proper explanation to tail recursion. I found this. This is great, thank you
Thank you so much! I have been confused whole day about the concept of tail recursion, you just help me to have a good night sleep~
same happen with me today...I thought, just wasted my whole day but this video cleared my whole concept. hence my hard work pays off that I found this video....thankyou so much.
Same here
Seriously you are doing a great job for all of us . ✌️✌️ Thank you so much 💓💓
Tail recursion performance is considered better than Non-Tail recursion because tail recursion is the last line executed so there's nothing left to do before leaving the function, this helps the compiler to optimize the execution because the function's stack frame need not be saved
this should hv been included in the video.. thanks
Sir ,why I not met such Teacher during my Graduation, Thanks a lot for wonderful teaching.
Bro I am in Graduation.
and I thing I am lucky to find this teacher
i am very happy that u people are helping us in almost all subjects.On behalf of all , my very big thanks to Neso Academy.......
Both of theme are non-tail recursion. 😊
Does the function stop being called when n overflows?
I request neso academy to give answers to the home work problems in the comment section...It will be even better if u explain them...
@Curtis Phoenix no one gives a damn with your instapwn and your second dummy acc...
Absolutely right
Your message has been successfully read and ignored ✓✓
@@laus-thecurious4120
Good
Yeah
9:06 My dear friend let me tell you one thing😘
It's really helpful video, I always confused about recursion function but now everything is cleared about recursion
Hii
you guys doing a great job helping people WHO LOOKING out for knowledge # EDUCATION IS NOT A BUISNESS
100% it shouldn't be
both program 1 & 2 are non-tail recursion functions, But program 2 has no step to go base so it will be infinite.
@@s.oktaydonmez649 adamsın knk iyi akıl ettin
Give signed value brother eg -10
@@s.oktaydonmez649 helal
It has a base cond.
@@s.oktaydonmez649 thank you, I also forgot about this fact
Both are Non Tail Recursive function. In Program 2 the range of int from -2^(31) to +2^(31)-1....When n=2^31 then it exceeds the range of int and again start from -2^(31) which is less than 0..
Are they both Non Tail Recursive because printf is the last thing in the function?
@@nuwaver91 yeah both are non tail recursive function
Your videos really helped me out to understand many things on recursive exactly to the point.
Most of the people think that the 2nd program is wrong and does not converge into the base case but it actually converges...
It ia because int overflow even when the output is 1(the smallest positive number) the recursion can run only upto 32 times because an int is 4 bytes which is 32 bits...
At n=1 the first bit is set and when n=2 second bit is set and so on... When it reaches the 32nd bit n is no longer a positive number and the program returns.
Great comment! I'm glad I scrolled down and found it. Thank you!
Ans=> PR-1: non-tail recursion function
output : 0 (zero)
PR-2:non-tail recursion function
it is infinite loop.
Till now i can say that your LEGENDRY TEACHER
❤
Your voice is impressive and the way u teach is amazing
Amazing lectures i never see this type of explaination aswome
Both are non-tail recursion. I executed Program 2 in Visual Studio Code. I took n=3.I got output and it prints from 3,6,12 upto byte limit and again return that from byte limit to 3. Program 2 is not infinte loop. It executes upto the byte limit of the computer and last line is also executed after return.
Thanks 😊
But in second program indeed value got true and finally if got executed but there is a printf before recursive procedure and after if being executed, there is, that printf statement left to be executed and second thing we didn't put rescursive procedure in else construct which mean still recursive procedure got executed anyway and in program 1 when value got zero recursive again isn't in else construct it still executed value 0;
Thank you so very much for your wonderful lectures, Sir!!
sir I can see your hard work in your videos
Such a great teacher @jaspreet sir
Your style of explaination is excellent ♥️
This is the best explanation of recursion that I’ve ever seen.
A very big thanks to you sir for clearing my doubts on recursion
Part 1 and part 2 both are great examples of non-tail recursion, in fact, 2nd code is very useful if you want to revise the concept of range of "int" along with studying type of recursion, .....
In the same code(2nd homework code) if we change any one of the two printf statements and print 2*n instead of n, then we'll notice that as the size of n doubles each time during recursive calling then as it reaches 1073741824, the recursive function that will be called by fun2(1073741824) will be fun2(-2147483648) [since max value of int is 2147483647] and as -214.... is less than zero therefore the base condition is met and thereafter the second printf statement will start getting executed and the functions will simultaneously pop out from the stack. The output obtained in this case (where we are printing 2*n instead of n clearly shows this)
Well explained thnk you dear
This comment should be pinned.
Thanku🙌
We will get a palindrome r8? In program 2
thanks
cool explanation i thought the program would last forever thank you
Both of them are non - talil recursive function.🙂🙏
This channel shoud be given an award from @UA-cam.
Well explained great job Neso Academy!
Yes both the 2 programs are non tail recursive because the recursive call is not the last thing done by function and in both the process we need the previous function record.
if thats the case there exist no tail recursion at all, tail recursion should be removed from textbooks
Brilliant explanation, stack frames were very well illustrated!
both are non-tail recursive function as recursion calll is not the last thing done by funcion
print f is also be a functon
No only 1st is non-tail because in 2nd function will never return and last printf will never get evaluated so here only function call is the last thing done so tail recursive. No Sorry last printf will be evaluated as integer overflow will occur and n
@@adarshverma013 why last print will never evaluated?
@@omaranas2965 because it is an infinite loop
@@damnnations9308 aha ty bro
Sir, thinks so much for that! I've finally got it!
sir your channel is a boon for us
1 2 non tail
1) value can be zero
2) value can not be zero as well
1) n=4 then 4/2=2 2%2=0 done.
2) n=1 then pt=1 , 2*1= 2 , pt=2 goes on... value wont be zero
any value after zero can never be greater then zero.
Finally got it! You are a hero!
Thanks to your Recursion videos not only did I understand this issue, but also learnt something. So thanks. :D
very beautiful explaination ..great work
Thank you all for these videos, my master. Probably you did intently but in the end , program 2 will get an stack overflow error. That doesnt satisfy the base statement.
The 1st program is a non-tail recursive, but the 2nd one is a tail recursive program. That is because the recursion will enter an infinite recursion making it impossible for the printf to be executed. Therefore, the recursion remains the last thing to be called.
My dear friend you're the best 🙌❤
please also make one vide on void functins and return 0 ,return 1 diffrence
Both of the given questions are the examples of non-tail recursive functions. As we are performing a modulo operation in the printf statement of first program and printing the value of n in the second program.
@0:50, fun function has type void and yet it is returning some value at line 6 of the code.
It is returning void function so it is absolutely fine. And other easy way to understand is fun(n) returning to fun(n-1). And if function is void it doesn't mean that function doesn't return anything it returns void.
A very big THANKS...... You are really doing a very great job ..... I was really very confused about tail and non-tail both ..... Even after asking my lecturer thrice I won't be able to get it properly..... But u made me understand ...... A very big THANKS again.....
I assumed n==5;
When fun(2*5) will be called, it will start to print 5 10 20 40.......1342177280
Iteration/recursion is possible until value of n reaches 2147483647(max range for signed int)
Since we are using fun(2*n)
i.e. when value of n reaches 1342177280 then after 2*n will exceed the range of n
i.e. fun(2684354560)
, i.e. value of n will become -ve (lec 8).
And as stated if(nn i.e.
n=1342177280 i.e. fun(2*671088640) and fun will pop out
then
671088640 then fun will popp
....
.....
n=10 i.e. fun(2*5)
Now fun(2*5) will popp out and control will return to main and then main will popp out.
This is for 2nd program.
Thank you so much sir for these lectures.C is our building block and you r letting us learn all our concepts with so ease.Warm regards!!!
Hey depali would you got placed I m from ur clg
PROGRAM 1 = NON TAIL RECURSIVE
PROGRAM 2 = TAIL RECURSIVE
AS LAST PRINTFF WILL NOT GET EVALAUTE BUT IT LEADS TO STACK OVERFLOW
The printf does evaluate. I've tested both programs and found out that both are non-tail recursive. I entered an input of 8, after hitting 1073741824 it went down to -2147483648 and triggered the base case. After the integer overflow, it prints the result in reverse. Please explain if I'm wrong.
@@tokomnyori6730 u r ri8
@@selvamsaravanan1095 Thanks 😊
Exactly true
Vivek nirma you are right 1.non tail and 2. Tail because printf is not evolut it may be leads to stack over flow so printf function not be evoluted so it is an tail recursive and in program 1. Fun2(0) arises and then printf should be evoluted so it is non tail
Veryy nice explanation thank u sir
In case of tail recursion there is no need to maintain the stack because the function call is the last thing thats happening. That is why tail recursion is preferred.
1st is Non Tail but 2nd there is no base condition therefore it'll be in an ∞ loop but I can guess it as Tail because there there is nothing to do after calling the function in the function
Your answer is absolutely correct but there is base condition given(n
@@AbhishekKumar-kk6qs but when 2*n will exceed the integer range based on computer, then n will become negative( let us assume if int is 2 byte then after 32766 n will become -32768) and it will return, so I guess that 2nd is also non tail recursive
@@kumarparaskaran7598 this is the correct analysis !
both are non-tail recursive function
because 2nd function is dependent upon the value you passed, if it will be greater number then the value multiplied with 2 will doubled and so on therefor 3 or 4 values are only printed by 1st printf function and then value will be negative (you know signed int concept) so 2nd printf will execute,too.
for ex you can pass n as 536870900(nearer value of max signed value/4) so you get only 3 values WHICH is printed by 1st printf and 3 values which are printed by 2nd printf(which is same but in reverse position).
SO, AT THE END NOTH ARE NON-TAIL RECURSIVE FUNCTION
No, Program 2 is non - tail recursive. Check out output in Visual Studio code.
If teacher like you would be there in college then the scenario of student's life would be different...
yep...i agree :")
Best channel to learn data structure....loved it😍
Because of your legendary lectures, now I fell in love with India.
This was a great explanation of the topic that was about to drive me crazy... Thanks and keep it up
both are non-tail recursive function
because 2nd function is dependent upon the value you passed, if it will be greater number then the value multiplied with 2 will doubled and so on therefor 3 or 4 values are only printed by 1st printf function and then value will be negative (you know signed int concept) so 2nd printf will execute,too.
for ex you can pass n as 536870900(nearer value of max signed value/4) so you get only 3 values WHICH is printed by 1st printf and 3 values which are printed by 2nd printf(which is same but in reverse position).
SO, AT THE END NOTH ARE NON-TAIL RECURSIVE FUNCTION
This channel ❤❤
Thanks for this, Sir. It saves a bunch of time of mine.
really good viedio I have ever seen tq neso academy!!!
i really appreciate this effort thank you very much❤❤❤❤
Program 2 : base case not correct;
Will execute till the stack overflow
what if the n is negative value?
@@daverussell4052 for neg it will not overflow
I think for negative value it will return back to main() [or the function that called it]
Because.. the if condition becomes true so it returns back to the function that called it..
Please suggest any book for such questions 🥺🥺🥺
Thank you so much....very useful 👍
program 1 - not-tail program
program 2: this program is very interesting though . if u paas a value than it will stop at that same value because when it exceeds the range of data type it has to come back.
Can u please send me solution of these homework problems
I can't solve this
@@lakshitavij8600 Program 2 is non - tail recursive. Check out output in Visual Studio code.
Program 2 will make function call until Stack overflow condition occur. Hence the Output is Stack overflow Error. And it is Tail recursion
How is it tail?
If the condition changes,It will be non-tail.
@@nandinisunkara4070 Does 2nd printf Executed atleast 1 time ? Ans is No..
Therefore, programs are just like Pf() Recursive()
Not stack overflow, integer overflow. Stack overflow is different than integer overflow. Both are non-tail recursive. In the second program, integer will hit its limit and will become negative negative number and it will hit the base case. It is non-tail recursive because it will evaluate the printf statement below.
@@tokomnyori6730 In which data structure Integer are stored in Computer?
No, Program 2 is non - tail recursive. Check out output in Visual Studio code. Func will just executes upto the Byte limit and starts returning back and last line is executed.
program 2 will fallback ,it will not go into infinite loop
may be the stack has limited memory
we can't use return in a void function like what did you do in the example of the tail recursion
Recursion is my fear point in C programming 🙌🏻 but if(!rocketscience){printf("It can be learned
");
sir can you please make a video on binary recursion??
why was the activation record of fun(0) not stored in the stack in example 1?? The activation record is stored when a function is called right? fun(0) was called.
This activation record thing is cool
great job man
Thank u that what i needed
The stack usage is the same. you didn't show the optimization (by compiler) gained from the fact that it's a tail recursion. Someone can argue about the need to differentiate them since we may assume that explicit/implicit return is the last instruction that triggers the stack pop().
use gcc -O2 to enable tail recursion in C.
Konstantin Rebrov you didn't understand my point and hrnce you're responding to a question I didn't ask
1st is non tail recursive. 2nd is tail recursive
both r non tail recursive functions
Awesome 👌
Sir ur explaining well bt please reveal answers in nxt video so tat beginners can get some idea…it’s ah kind request from us sir..🥺
This was so incredibly helpful!!!
Thanku sir 🙏
Bro you use the word "evaluate" in a wrong manner. I think sometimes you mean to say "execute" which is different than "evaluating". You don't evaluate a print statement, you execute it.
Thanks for the tutorial though :)
We are studying C language not english
Sir please u don't say thanq for watching to us when end of the video 🙏 bcz u r giving life to us ❤️
Both functions are non-tail. They all have printf after the function call.
2nd is not a recursive program
@@madhusudansahu5482I do not know how you came to this conclusion. The second function is recursive for sure because it call itself. The recursion is also non-tailed. The n variable become negative because it overflows at 2^31 (integer variable). After that the second printf gets executed. Finally I tested my conclusion with the program:
#include
#include
void fun2(int n) {
if (n
Can u explain the line of yours "n becomes negative bcz it overflows at 2^31." a bit more. I'm not able to grab this concept clearly.
Thank You SIr
thank you sir .well expalained
Sir plesse give the answer of homework problem in the next video for satisfaction.
Such a good explanation
Thanks for the lecture especially the tail recursion part which was very confusing for me..
Both are non-tail recursive 😊
2:28 Sir VOID return type wala function kuch return nahi karta instead use INT as return type
program 1 is non tail recursive , but I can't say about program 2 because it will call itself again and again. since n*2 will increase the value passed to the fun2 and it will never go to the end condition i.e. n
I guess the integer overflow explains the function getting terminated
No, Program 2 is non - tail recursive. Check out the output in Visual Studio code.
Good video 👍
Thankyou neso academy
Sir please make a video on tower of Hanoi
1 st one is non tail recursive and second one is tail recursive ...❤
How come the second one is tail recursive?
@@brocklesnarufcchamp1 perhaps there is nothing to evaluate after function calling they simply print n
Program is 1 is Non Tail Recursion and Program 2 is Tail Recursion because after if condition print execute but it is no need to store then second recursive call not get value of n .
No, Program 2 is non - tail recursive. Check out output in Visual Studio code
1st one is non-tail recursive program
2nd one is tail recursive but it will run infinite no. of times..output:444444.....
No, Program 2 is non - tail recursive. Check out output in Visual Studio code.
First program is non tail recursive function and the second one is infinite
1 program is non tail recursive.
2 program is tail recursive.
Kindly please tell me correct answer
Sir .
Thank you so much for wonderful seccion.
No, Program 2 is non - tail recursive. Check out output in Visual Studio code.