Question: I am new to Python and learning about functions. I came across the following function and at my wits end understanding how it works. It seems to me no matter the value of `b`, the answer should always be six but that isn't the case.

***CODE***

```
def mult(a, b):
    if b == 0:
        return 0
    rest = mult(a, b - 1)
    value = a + rest
    return value
print "3 * 2 = ", mult(3, 2)

```

My understanding of what happens

1. Since `b` is not 0 it proceeds
2. `rest` is assigned the value `3, 1` and it runs the function again
3. Since `b` is 1 and does to equate to 0, it proceeds to `rest`
4. `rest` is assigned the value `3, 0` and it runs the function again
5. Since `b` is now zero it returns the value 0
6. It then proceeds to `value` which has the value 3 + 3 as `a` had the value of 3 and `rest` had the value of 3 i.e. `(3,0)`
7. It returns the value 6

**But** if I assign `mult(3,4)`, it returns the value 12. Following my understanding, that isn't possible. Clearly, I am not understanding the logic flow. What am I doing wrong?

Answer: 
Your logic is right until bullet 5. Then at 6 you skip some steps.
This is a recursive function. It is easier to understand it by drawing the tree of events on paper, but lets resume your logic first:

mult(3,4):

```
 1. a = 3, b = 4
 1. rest = mult(3, 3)
 2. a = 3, b = 3
 2. rest = mult(3, 2)
 3. a = 3, b = 2
 3. rest = mult(3, 1)
 4. a = 3, b = 1
 4. rest = mult(3, 0)
 5. a = 3, b = 0
 5. return 0
 4. value = 3 + 0
 4. return 3
 3. value = 3 + 3
 3. return 6
 2. value = 3 + 6
 2. return 9
 1. value = 3 + 9
 1. return 12

```

In the example above, each number at the beginning of the line represents the step in the recursion. It starts at step 1, goes until step 5 in this case, and then returns 1 by 1 until step 1 again with your final answer.

The function implements the concept of multiplication through sums. For example, 3 \* 4 is the same of adding the number '4' three times.
Here is a way to visually see how the recursion works:

```
COUNTER = 0

def mult(a, b):
    global COUNTER

    COUNTER+=1
    print " "*COUNTER + "Called with", a,b

    if b == 0:
        return 0
    rest = mult(a, b - 1)
    value = a + rest

    COUNTER -= 1
    print " "*COUNTER, "Value:", value 

    return value

print "3 * 4 = "
print mult(3, 4)

```

Output

```
3 * 4 = 
 Called with 3 4
  Called with 3 3
   Called with 3 2
    Called with 3 1
     Called with 3 0
     Value: 3
    Value: 6
   Value: 9
  Value: 12
12

```

You can see how the call stack goes all the way down to the bottom (b==0), and then returns values back up the chain to the top.