# Recursion

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-08-2008
freddyvorhees
Recursion
Problem:
It should print its local variable and recursive call parameter. For each recursive call, display the outputs on a separate line and add a level of indentation. Do your utmost to make the outputs clear, interesting and meaningful.

I havent done yet the "separate line and add a level of indentation".

Code:

```#include <iostream> using namespace std; void factorial( unsigned long ); int main() { factorial(5); } void factorial( unsigned long number ) {                int result;         if ( number <= 1 )                 cout<<endl;         else                                      cout<<number * factorial( number - 1 ); }```
Error:
error C2297: '*' : illegal, right operand has type 'void'
• 09-08-2008
anon
Your function doesn't return anything (void), yet you attempt to multiply it by something.
• 09-08-2008
freddyvorhees
But using void is appropriate, right?
• 09-08-2008
laserlight
Quote:

But using void is appropriate, right?
Maybe, but not until you change the function a little more (think of its parameters).
• 09-08-2008
swgh
Change void to unsigned long and you will see your desired result
• 09-08-2008
dwks
Well, not really. Because there isn't actually a return statement. :) It requires a few more changes than that.
• 09-08-2008
master5001
I would not even make this particular function recursive. You can just use a loop to accomplish the same task without all the extra expense to the stack.
• 09-08-2008
matsp
Quote:

Originally Posted by master5001
I would not even make this particular function recursive. You can just use a loop to accomplish the same task without all the extra expense to the stack.

1. The compiler will probably make it non-recursive since it's plain tail-recursion. At least gcc does.
2. Because recursion is a concept that needs to be understood, and starting with one of the most simple concepts of recursion is a good way to learn how it works. I have written recursive factorial functions in several languages (Pascal, Modula-2, C, C++ [I think], Basic and a couple of assemblers) just for practice.

There are problems that are MUCH simpler to solve with recursion than otherwise - and then there are problems that are just as easy to solve iteratively, and for REAL programs, that's the right solution then.

--
Mats
• 09-08-2008
master5001
Fair enough. I do understand that the teacher is probably just trying to demonstrate how something can be written more clearly with recursion, however I am not too inclined to write something so simplistic as a recursive function even if I went into it knowing for sure the compiler would optimize out the recursive function and replace it with a loop.
• 09-09-2008
matsp
Quote:

Originally Posted by master5001
Fair enough. I do understand that the teacher is probably just trying to demonstrate how something can be written more clearly with recursion, however I am not too inclined to write something so simplistic as a recursive function even if I went into it knowing for sure the compiler would optimize out the recursive function and replace it with a loop.

Agreed. But if you were to explain recursion to a new student, factorial is about the easiest one to use. Fibonacci series is another example, but involves two calls per step, so a bit more complex. Quicksort is an excellent example of something that is much harder to solve without recursion, but also rather complicated.

Linked list and binary trees are often walked using recursion.

--
Mats
• 09-10-2008
freddyvorhees
Cant understand it
Quote:

Your function doesn't return anything (void), yet you attempt to multiply it by something.
I thought void doesn't return anything, so it means its wrong to use void?

Quote:

Maybe, but not until you change the function a little more (think of its parameters).
A little more hint. Please.

Quote:

Change void to unsigned long and you will see your desired result
Tried it but it doesn't work.
• 09-10-2008
matsp
Code:

`cout<<number * factorial( number - 1 )`
uses factorial as one side of the calculation. That is not allowed when factorial is a void function, because "void" can not be multiplied by an integer.

As an aside, if you do "factorial(10)", it will print 10 numbers, so you probably want to print the result AFTER the function, instead of inside it.

--
Mats
• 09-10-2008
freddyvorhees
This is the whole problem:

(Visualizing Recursion) It is interesting to watch recursion "in action." Modify the factorial function of Fig. 6.29 to print its local variable and recursive call parameter. For each recursive call, display the outputs on a separate line and add a level of indentation. Do your utmost to make the outputs clear, interesting and meaningful. Your goal here is to design and implement an output format that helps a person understand recursion better. You may want to add such display capabilities to the many other recursion examples and exercises throughout the text.

Code:

``` 1  // Fig. 6.29: fig06_29.cpp  2  // Testing the recursive factorial function.  3  #include <iostream>  4  using std::cout;  5  using std::endl;  6  7  #include <iomanip>  8  using std::setw;  9 10  unsigned long factorial( unsigned long ); // function prototype 11 12  int main() 13  { 14    // calculate the factorials of 0 through 10 15    for ( int counter = 0; counter <= 10; counter++ ) 16        cout << setw( 2 ) << counter << "! = " << factorial( counter ) 17          << endl; 18 19    return 0; // indicates successful termination 20  } // end main 21 22  // recursive definition of function factorial  23  unsigned long factorial( unsigned long number ) 24  {                                              25    if ( number <= 1 ) // test for base case    26        return 1; // base cases: 0! = 1 and 1! = 1 27    else // recursion step                      28        return number * factorial( number - 1 );  29  } // end function factorial```
Am I doing the right thing if I'm using void?
• 09-10-2008
freddyvorhees
Where should I do the modification in main or in the function?
• 09-10-2008
matsp
Ah, ok, so then you would add the code to show what it is doing inside the factorial function.

You still have to return the result to main tho'.

--
Mats
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last