What is difference between recursion and tail recursion?

1 Answer

Answer :

Tail recursion is a compiler optimisation. When the final statement of a function is a recursive call into the same function, the compiler can eliminate that call completely. Consider the (simplified) quicksort algorithm: void qsort (int* array, unsigned lbound, unsigned ubound) { unsigned pivot; if (lbound>=ubound) return; pivot = partition (array, lbound, ubound); qsort (array, lbound, pivot-1); qsort (array, pivot+1, ubound); } This function has two recursive calls, however the second call is a tail recursion because as soon as we return from that instance of the function, we also return from the current instance. As a result, the values of the local variables are no longer relevant to the current instance. That being the case, instead of calling the function a second time and thus crea

Related questions

Description : What is the difference between recursion and iteration?

Last Answer : A: Recursion is usually slower than iteration due to overhead of maintaining stack, whereas, Iteration does not use stack so it's faster than recursion. Recursion uses more memory than ... whereas, Iteration consume less memory. Recursion makes code smaller, whereas, Iteration makes code longer.

Description : What is recursion?

Last Answer : Recursion is when a method calls itself. 

Description : The minimax algorithm computes the minimax decision from the current state. It uses a simple recursive computation of the minimax values of each successor state, directly implementing the defining equations. The ... are backed up through the tree as the recursion unwinds. a) True b) False

Last Answer : a) True

Description : Backtracking is based on ____________ a) Last in first out b) First in first out c) Recursion d) Both Last in first out & Recursion

Last Answer : d) Both Last in first out & Recursion

Description : Define what is recursion?

Last Answer : C language a function may call another function. When a function calls itself, it is referred to as recursive call and the process is known as recursion. C provides very good facilities for recursion.

Description : What is recursion?

Last Answer : A: It is a process in which a function calls itself repeatedly until some base condition is satisfied.

Description : Calculate factorial of a number using recursion.

Last Answer : #include #include int factorial(int no) { if(no==1) return(1); else return(no*factorial(no-1)); } void main() { intfact,no; clrscr(); printf("\n Enter number"); scanf("%d",&no); fact=factorial(no); printf("\n Factorial number=%d",fact); getch(); }

Description : If the value of a number (N) is entered through keyboard. Write a program using recursion to calculate and display factorial of number (N). 

Last Answer : #include<stdio.h> #include<conio.h> int factorial(int N); void main() { int N,fact; clrscr(); printf("Enter number:"); scanf("%d",&N); fact=factorial(N); printf( ... N) { if(N==1) return(1); else return(N*factorial(N-1)); }

Description : Develop a program to find factorial of a number using recursion.

Last Answer : #include<stdio.h> #include<conio.h> int factorial(int num) { if(num==1) { return 1;  } else { return(num*factorial(num-1));  } } void main() { int ... ;num); result=factorial(num); printf("Factorial of %d is %d",num,result); getch(); }

Description : Can you explain the difference between an occurrence form policy and a claims-made policy and when I need a tail policy?

Last Answer : If you need more information on your limits of liability make sure you check your reports for how much your policy has been used in the past year. Also, make sure you speak with the company who holds your insurance policy to get their advice.