As we discussed in the previous chapter, we need to loop the immutable function recursively. Let's suppose we have the fibonacci() function that is immutable. We then need to refactor it to be a recursive function. The fibonacci_iteration.cpp code implements the fibonacci() function in the iteration way. The code is written as follows:
/* fibonacci_iteration.cpp */
#include <iostream>
using namespace std;
// Function for generating
// Fibonacci sequence using iteration
int fibonacci(int n)
{
if (n == 0)
return 0;
int previous = 0;
int current = 1;
for (int i = 1; i < n; ++i)
{
int next = previous + current;
previous = current;
current = next;
}
return current;
}
auto main() -> int
{
cout << "[fibonacci_iteration.cpp]"...