How can I show mathematically the time complexity of this function is O(N)?
You can use induction on input.
For example, in your foo,
to show that foo(N) uses exactly N comparisons,
Base case: foo(1) uses 1 comparison
Induction hypothesis: foo(N) uses N comparisons
Step case: foo(N+1) does one comparison and then call foo(N), thus in total, does N+1 comparisons.
You can prove similar statement for addition or all operations, and then you can give a time complexity based on those number of operations.