Stack Exchange
Need to know why the code behaves in this way - explanation code in C
#include <stdio.h>

int main() {
    int c;

    c = f(3, 5);
    printf("c = %d\n", c);    
    c = f(4, 2);
    printf("c = %d\n", c);    
    c = f(2, 4);
    printf("c = %d\n", c);    
    c = f(3, 3);
    printf("c = %d\n", c);
}

int f(int d, int e) {
    if (e > 0)
        return f(d, e - 1) + f(d, e - 1);
    else
        return d;    
}

I know the code gives me the product between d (the first number in f) and 2 elevated to the second number in f(function)
The problem is that I don't understand why it gives me such output, I don't see any operators within the code (something like an equation d*2^e).
An a deep explanation would be very appreciated, feel free to recommend any material which can be helpful in learning C.

Recursion appears to be on the reading list.

Where can I find such recursion?

google, bing, yahoo, duckduckgo,....

Wait ,is the recursion what gives me that output?

Why not just work through the calculations with pencil and paper -- i.e. pretend that you are the computer. It shouldn't take more than a few minutes. Understanding will come.

@LucaMuscolo In order to understand recursion, one must first... understand recursion.

This is a simple recursive function, I can write in such way:
         |  f(d, e-1) + f(d, e-1)   if e > 0
f(d,e) = |
         |  d                       otherwise

But I can simply add two equal terms f(d,e-1), and then it becomes:
         |  2* f(d, e-1)    if e > 0
f(d,e) = |
         |  d               otherwise

In order to let you understand, just try to expand the function. Following the rule, I can write:
f(d,0) = d    for all d  // Identify function

That because the definition, since e=0.
Going ahead:
f(d, 1) = 2*f(d, 0) =           2*d

f(d, 2) = 2*f(d, 1) = 2*(2*d) = 4*d

f(d, 3) = 2*f(d, 2) = 2*(4*d) = 8*d

...  // Inductivly...
f(d, k) =  2^k *d     // for k > 0


Function f(d, n) computes d * 2n.  Here is a proof by induction:

f(d, 0) returns d, which is equal to d * 20
f(d, n + 1) returns f(d, n + 1 - 1) + f(d, n + 1 - 1), which simplifies into 2 * f(d, n).  If f(d, n) computes d * 2n, then f(d, n + 1) computes 2 * d * 2n, which is exactly d * 2n+1.

QED: We can conclude by induction that f(d, n) computes d * 2n for all n >= 0.

Thank for replying , but how do I know from just reading the code that it makes the product of d with 2^e?

You read through the code and understand it. Go through the code and take the execution route and then come out with the result.

@LucaMuscolo:  I quoted the code literally in steps 1 and 2.  step 2 corresponds to the if (e > 0) branch, step 1 to the else branch.
