Instead of the current loop and counter, return 0 + recursive_step or 1 + recursive_step , where recursive_step is a call to count(...) , where you increased the array pointer and decreased length . Your base register is when length == 0 , after which you simply return 0 .
A good way to understand recursion is to learn how the calculation is performed, step by step. In your example, you will do something like this:
count(5, {3,4,1,2,4,5,6,5,4,5}) 0+count(5, {4,1,2,4,5,6,5,4,5}) 0+0+count(5, {1,2,4,5,6,5,4,5}) 0+0+0+count(5, {2,4,5,6,5,4,5}) 0+0+0+0+count(5, {4,5,6,5,4,5}) 0+0+0+0+0+count(5, {5,6,5,4,5}) 0+0+0+0+0+1+count(5, {6,5,4,5}) 0+0+0+0+0+1+0+count(5, {5,4,5}) 0+0+0+0+0+1+0+1+count(5, {4,5}) 0+0+0+0+0+1+0+1+0+count(5, {5}) 0+0+0+0+0+1+0+1+0+1+count(5,{}) 0+0+0+0+0+1+0+1+0+1+0 <---The last 0 is the base case 3
If you are allowed to change the specification of a function, you can also do something cool called tail recursion. Instead of return 1 + count(...) add the accumulated number to evaluate the counter: int count(int number, int array[], int length, int acc)
And do return count(..., acc+1) or return count(..., acc+0) . Then, some compilers can perform tail call optimization, turning them into a loop in the compiled code. This saves memory compared to regular recursion.