Question

Data Structures Question When writing a recursive function, what are the two cases for which you...

Data Structures Question

When writing a recursive function, what are the two cases for which you must write code?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Recursion:

The recursive function calls to itself and a terminate or base condition is specified which breaks the infinite execution of the recursive call. When the function call to itself then it works like a continue statement and the control goes to the starting of the function with updated argument value.

In a recursive program, the function call to itself and recursion is always applied to functions. In the iterative program, a group of statements or a single statement is executed repeatedly.

The stack data structure is used in recursion for storing the intermediate value when each function call is made but in the iterative program, the stack is not used.

For example:

int calculateFactorial(int n)
{
//base condition
if (n < = 1)
{
return 1;
}
else
{
return n * fact(n-1);
}
}

As we can see in the above recursive function, two cases are mention:

  1. Base condition
  2. Recursive call

Base condition:

A terminate condition or base condition is specified which breaks the infinite execution of the recursive call.

Recursive Call:

The function will call to itself and this is known as a recursive call.

Add a comment
Know the answer?
Add Answer to:
Data Structures Question When writing a recursive function, what are the two cases for which you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT