Question

You might have heard of recursion described as “It’s a function that calls itself”. Describe an...

You might have heard of recursion described as “It’s a function that calls itself”. Describe an example of how this works and compare with an iterative call. What are the advantages of using a recursive approach?  Describe implications for using a recursive approach

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

Definition:

Recursion:
A recursive function calls itself again and again until, some condition is satisfied

Iteration:
An iteration, is repetitive call or execution of a statment,using loop, till condition is satisfied.

Example Factorial function:

Recursion:

int factorial(int n)
{
if(n==0)
   return 1;
else
   return n*factorial(n-1); //call the function till n=0
}


Iterative Method:

int factorial(int n)
{
int result=1;

//runs the loop till i<=n
for(int i=0;i<=n;i++)
result= result*i;

return result;
}

Advantage of Recursive approach:
1) reduce the code size
2) less number of varaibles
3) unnecessary function call is reduced.


implications for using a recursive approach:
1)infinite recursion leads to system crash.
2)infinite recursion takes more memory space and execution time

//for any clarification, please do comments

Add a comment
Know the answer?
Add Answer to:
You might have heard of recursion described as “It’s a function that calls itself”. Describe an...
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
  • 1. Recursion is ususally where a function calls itself; (another example of recursion is where function...

    1. Recursion is ususally where a function calls itself; (another example of recursion is where function A calls function B, and B calls C and C calls A, creating a loop in the calls). Some problems are most naturally solved recursively, such as writing the factorial function, or finding all the perumutations of a sequence, or checking if a string is a palindrome. Since those examples were done in class, here we will give you a toy example, which normally...

  • Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to...

    Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to implement a binary search algorithm to determine whether a given element is a member of a given sequence Each function will have two parameters, aseq, a sorted sequence, and target. isMemberR and isMemberI will return True if target is an element of the sequence, and False otherwise. Your implementations must implement the binary search algorithm described above. When function i sMemberR recursively invokes itself,...

  • Recursion and Doubly Linked Lists Problem #1: Assume you have a doubly linked list of single...

    Recursion and Doubly Linked Lists Problem #1: Assume you have a doubly linked list of single characters. Plan the algorithm to insert character 'a' immediately before each character 'b' that is encountered. 3. What is the simple case: (what case does not require a loop?) How do we get to the next a smaller sub-problem? What information is needed for the function call: 4. If we insert ‘a, PRIOR to the recursive call, what do we need to be careful...

  • C++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

  • Write a java program that demonstrates recursion. This program can be in a java GUI frame...

    Write a java program that demonstrates recursion. This program can be in a java GUI frame or as a console application. On this one it is up to you. (It would probably work better as a console program for this particular program.) The input for this program is a number of boxes. Ask the user for this with a textbox or a prompt on the command line. Demonstrate recursion with a function called LoadTruck() that takes a number of boxes...

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

  • Describe a possible scenario or a scenario that you have read or heard of that you believe is an ...

    Describe a possible scenario or a scenario that you have read or heard of that you believe is an example of astroturfing and describe what elements unique to your scenario make it astroturfing.

  • Use the resource-based view as described in this chapter to describe how information technology might be...

    Use the resource-based view as described in this chapter to describe how information technology might be used to provide and sustain a winning position for each of these businesses: 1. A global advertising agency 2. A local restaurant 3. A mobile applications provider 4. An insurance company 5. A web-based audio book service Some claim that no sustainable competitive advantages are gained from IT other than the capability of the IT organization itself. Do you agree or disagree? Defend your...

  • In this lab, you may use MATLAB to implement the Recursion problems. Do not use a...

    In this lab, you may use MATLAB to implement the Recursion problems. Do not use a for or a while loop to implement a recursive function. 3. Recursion: Sort Game (One of the reasons why we sort) (25 points) Write a recursive function that guesses a number between 0 and x (x being an input number to the function). If you guess too high, it tells you that and continues. If you guess too low, it tells you that and...

  • JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using...

    JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...

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
ADVERTISEMENT