What does this look and work in a stack frame view? I don't completely understand how the returning works. I appreciate the help!
Let's try to build the solution bottom-up.
- If you called the function -
int count_digits(int n, int digit)
as count_digits(4, 4)
what would happen ?
This is the base case of your solution so it is very easy to see what is the return value. Your function would return 1.
- Now, let's add one more digit and call the function like-
count_digits(42, 4)
. What would happen ?
Your function will check the last digit which is 2
and compare with 4
since they are not equal so it will call the function count_digits(4, 4)
and whatever is the returned value, will be returned as the result of count_digits(42, 4)
.
- Now, let's add one more digit and call the function like -
count_digits(424, 4)
. What would happen ?
Your function will check the last digit which is 4
and compare with 4
since they are equal so it will call the function count_digits(42, 4)
and whatever is the returned value, will be returned by adding 1 to it. Since, number of 4
s in 424
is 1 + number of 4
s in 42
. The result of count_digits(42,4)
will be calculated exactly like it was done previously.
The recursive function builds up the solution in a top-down manner. If there are n
digits initially, then your answer is (0 or 1 depending on the last digit) + answer with n-1
digits. And this process repeats recursively. So, your recursive code, reduces the problems by one digit at a time and it depends on the result of the immediate sub-problem.
You can use the C++ tutor at pythontutor.com website for step by step visualization of the stack frame. http://pythontutor.com/cpp.html#mode=edit
You can also try with smaller inputs and add some debug output to help you track and see how recursion works.
Check this stackoverflow answer for understanding what a stack frame is - Explain the concept of a stack frame in a nutshell
Check this stackoverflow answer for understanding recursion -
Understanding recursion
If you would like more help, please let me know in comments.