pandol
11-18-2007, 07:15 PM
Below there is a simple recursion program. My question is why when I Run the program I get "11" times the expression "Hello World" instead of "1"?
I would expect to get:
"My name"
"Hey"
"Hello world"
#include <iostream>
using namespace std;
void doll ( int size )
{
if ( size == 0 ) // No doll can be smaller than 1 atom (10^0==1) so doesn't call itself
{
printf("My name\n");
printf("Hey\n");
}
else
{
doll ( size - 1 ); // Decrements the size variable so the next doll will be smaller.
}
printf("Hello world\n");
}
int main()
{
doll ( 1 ); //Starts off with a large doll (its a logarithmic scale)
}
I would expect to get:
"My name"
"Hey"
"Hello world"
#include <iostream>
using namespace std;
void doll ( int size )
{
if ( size == 0 ) // No doll can be smaller than 1 atom (10^0==1) so doesn't call itself
{
printf("My name\n");
printf("Hey\n");
}
else
{
doll ( size - 1 ); // Decrements the size variable so the next doll will be smaller.
}
printf("Hello world\n");
}
int main()
{
doll ( 1 ); //Starts off with a large doll (its a logarithmic scale)
}