The key to this is that unless b
==1, unknown()
calls unknown()
- this is called recursion.
For brevity, I'm going to call the function f instead of unknown:
Some languages present functions like this in a clearer way with pattern matching; the equivalent in an imaginary pattern matching language might be:
f(x,1) := x
f(x,y) := x + f(x,y-1)
And so...
f(3,4) = 3 + f(3, 4-1)
= 3 + f(3, 3)
= 3 + ( 3 + f(3, 3-1))
= 3 + ( 3 + f(3, 2))
= 3 + ( 3 + ( 3 + f(3, 2 - 1)))
= 3 + ( 3 + ( 3 + f(3, 1)))
= 3 + ( 3 + ( 3 + (3)))
= 12
I guess your homework is to decide what a better name for the function is than "unknown". Once you've decided, note that recursion is not the best way to implement that function unless your language has specific support for a feature called tail recursion optimisation (this might be a topic you want to shelve for later).
Also, others have noted that nested functions are not allowed in C -- even though your particular compiler might handle them. That means that although your program does this:
int function1() {
int function2(int x) {
...
}
int x = function2(3);
}
... a standard C compiler would not allow it. The normal way is:
int function1() {
int x = function2(3);
}
int function2(int x) {
...
}