ECONO Midterm: recursion

5 views2 pages

Document Summary

A recursive function in c++ is a function that calls itself. To avoid having the recursion run indefinitely, you must include a termination condition. To demonstrate recursion, let"s create a program to calculate a number"s factorial. In mathematics, the term factorial refers to the product of all positive integers that are less than or equal to a specific non-negative integer (n). The factorial of n is denoted as n! Recursion is a method of solving a problem where the solution depends on the solutions to smaller instances of the same problem. Let"s define our function: int factorial(int n) { if (n==1) { return 1; else { return n * factorial(n-1); In this case, it"s when n equals one, return 1 (the factorial of one is one). We placed the recursive function call in the else statement, which returns n multiplied by the factorial of n-1.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers

Related Documents