Recursion

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is the difference between direct and indirect recursion?

A function fun is called direct recursive if it calls the same function fun. A function fun is called indirect recursive if it calls another function say fun_new and fun_new calls fun directly or indirectly.

What is difference between tailed and non-tailed recursion?

A recursive function is tail recursive when recursive call is the last thing executed by the function. Please refer tail recursion article for details. (https://www.geeksforgeeks.org/tail-recursion/)

Why Stack Overflow error occurs in recursion?

If base case is not reached or not defined, then stack overflow problem may arise. Let us take an example to understand this. int fact(int n) { // wrong base case (it may cause // stack overflow). if (n == 100) return 1; else return n*fact(n-1); } If fact(10) is called, it will call fact(9), fact(8), fact(7) and so on but number will never reach 100. So, the base case is not reached. If the memory is exhausted by these functions on stack, it will cause stack overflow error.

What is base condition in recursion?

In recursive program, the solution to base case is provided and solution of bigger problem is expressed in terms of smaller problems.

What is recursion?

The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function.

How memory is allocated to different function calls in recursion?

When a function is called, the memory is allocated onto the call stack. A recursive function calls itself so the memory is allocated to the top of memory with different copies of the local variables. When the base case is reached, the function returns the value to the function from who called it and memory is de-allocated as the process continues.


Kaugnay na mga set ng pag-aaral

MTLE Pedagogy-Learning Environment

View Set

N431: FEMA -Incident Command System

View Set

Chapter 6: Consumer Choice and Utility Maximization

View Set