I tried to debug and understand the following code. The program is correct but I don't understand how the last four lines of outputs prints and how the value of numberOfBottles increased from 0 to 4 during recursion. Does someone has an easier way to explain me this. Thanks in advance.
#include <stdio.h>
void singSongFor(int numberOfBottles)
{
if (numberOfBottles == 0)
{
printf("There are simply no bottles of beer on the wall.\n\n");
}
else
{
printf("%d bottles of beer on the wall. %d bottles of beer.\n", numberOfBottles, numberOfBottles);
int oneFewer = numberOfBottles - 1;
printf("Take one down and pass it around. %d bottles of beer on the wall.\n", oneFewer);
singSongFor(oneFewer); // The function calls itself. This process is called recursion.
// Print message just before the function ends
printf("Put a bottle in the recycling, %d empty bottles in the bin.\n", numberOfBottles);
}
}
int main(int argc, constchar * argv[])
{
// We could sing 99 verses, but 4 is easier to think about
singSongFor(4);
return 0;
}
Call singSongFor with numberOfBottles = 4
Output starting lines for numberOfBottles (4)
| Call singSongFor with numberOfBottles = 3
s Output starting lines for numberOfBottles (3)
a | Call singSongFor with numberOfBottles = 2
m | Output starting lines for numberOfBottles (2)
e | | Call singSongFor with numberOfBottles = 1
| | | Output starting lines for numberOfBottles(1)
v | | | Call singSongFor with numberOfBottles = 0
a | | ↓ Output special message
r | ↓ Output end line with numberOfBottles(1)
s ↓ Output end line with numberOfBottles(2)
↓ Output end line with numberOfBottles (3)
Output end line with numberOfBottles (4)
It is not increasing. When calling funtion, current function execution is paused and will resume after function finishes.
So, for example:
call singSongFor with numberOfBottles = 1. We will call it function1. It executes lines 11-13 outputting
1 bottles of beer on the wall. 1 bottles of beer.
Take one down and pass it around. 0 bottles of beer on the wall.
Then on line 15 it calls singSongFor with numberOfBottles = oneFewer(0). We will call it function2. function1 execution is paused at line 16. function2 executes and outputs
There are simply no bottles of beer on the wall.
function2 finishes and function1 resumes from line 16. In function1numberOfBottles = 1, so line 18 will output
Put a bottle in the recycling, %d empty bottles in the bin.
MiiNiPaa's example explains it pretty well. But maybe this will help visualize it more:
singSongFor(0) outputs this:
There are simply no bottles of beer on the wall.
singSongFor(1) outputs this:
1 bottles of beer on the wall. 1 bottles of beer.
Take one down and pass it around. 0 bottles of beer on the wall.
<<< Whatever singSongFor(0) outputs >>> <- this is the recursive call
Put a bottle in the recycling, 1 empty bottles in the bin.
Since we already know what singSongFor(0) outputs, this means the full output for singSongFor(1) is this:
1 bottles of beer on the wall. 1 bottles of beer.
Take one down and pass it around. 0 bottles of beer on the wall.
There are simply no bottles of beer on the wall.
Put a bottle in the recycling, 1 empty bottles in the bin.
So now... singSongFor(2) outputs this:
2 bottles of beer on the wall. 2 bottles of beer.
Take one down and pass it around. 1 bottles of beer on the wall.
<<< Whatever singSongFor(1) outputs >>> <- this is the recursive call
Put a bottle in the recycling, 2 empty bottles in the bin.
Again... since we know what singSongFor(1) outputs, just substitute that in... making the full output this:
2 bottles of beer on the wall. 2 bottles of beer.
Take one down and pass it around. 1 bottles of beer on the wall.
1 bottles of beer on the wall. 1 bottles of beer.
Take one down and pass it around. 0 bottles of beer on the wall.
There are simply no bottles of beer on the wall.
Put a bottle in the recycling, 1 empty bottles in the bin.
Put a bottle in the recycling, 2 empty bottles in the bin.
Great. I think now I am getting it. Actually where I was getting confused was the function resuming part. So I guess what I understand is that every singSongFor function call the numberOfBottles variable value is just dedicated to that call. It's the same variable used but it's value is different for every singSongFor call. Is that correct?