Beginner Recursion Bullshit

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
AlexW
Posts: 1
Joined: May 20th, 2021, 2:25 pm

Beginner Recursion Bullshit

Post by AlexW » June 3rd, 2021, 4:05 pm

Code: Select all

#include <iostream>

void recurse(int i);

int main(void)
{
	recurse(0);
	std::cin.get();  //Just to keep the display until a key is pressed
	return 0;
}

void recurse(int i)
{
	if (i < 10)
	{
		recurse (i + 1);
		std::cout << i << std::endl;
	}
}
Before I discovered Chili's channel I was reading a 'C' book by Herbert Schildt. The above is his recursion example but I just don't get it. This prints the numbers 9 through to 0.

If you put a breakpoint on recurse(i + 1) and f11 you will see all the recursions build up in the stack as expected but I don't get how they are being consumed.

I see "i" being incremented up to 10 but when it gets to 10 surely it should leave the "if" statement and it does, briefly, but not to go back to main() instead it goes back to the line recurse(i + 1) but instead of increasing "i" from 10 to 11 as one might expect, "i" reduces by 1 to 9 and outputs to the console, then back to recurse (i+1) and instead of incrementing "i", subtracts 1 and outputs the result. Round and round it goes consuming the stacked recursions.

This makes no sense to me at all.

Can anybody shed some clarity on this for a poor, confused old man?

Thanks in advance for any insight.

Alex Whitcombe (alias Frogmella Slob in YouTube comments)

albinopapa
Posts: 4312
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Beginner Recursion Bullshit

Post by albinopapa » June 5th, 2021, 11:04 am

Code: Select all

#include <iostream>

int main(void)
{
	{  // extra scope to simulate function scope
		int i = 0;
		if (i < 3)
		{
			{  // i = 0 scope
				int i = 1;
				if (i < 3)
				{
					{  // i = 1 scope
						int i = 2;
						if (i < 3)
						{
							{  // i = 2 scope
								int i = 3;
								if (i < 3)  // i is not less than 3, skip
								{
									int i = 4;
									std::cout << i << std::endl;
								}
							}
							// back in i=2 scope, this is next line of execution
							std::cout << i << std::endl;
						}
					}
					// back in i=1 scope, this is the next line of execution
					std::cout << i << std::endl;
				}
			}
			// back in i=0 scope, this is the next line of execution
			std::cout << i << std::endl;
		}
	}
	// end of recurse function calls, exit to main
	std::cin.get();  
	return 0;
}
Not sure if this clears things up any, but this is kind of what the compiler ends up doing. Even though each variable is named 'i', they are in different scopes and the compiler will always use the most recent scoped variable. The extra seemingly pointless { } in the code simulates a function call by introducing a new scope.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply