# Recursive Function

**URL:** <https://discuss.python.org/t/recursive-function/34690>\
**Category:** Python Help\
**Created:** [September 26, 2023, 4:15am UTC](https://discuss.python.org/t/recursive-function/34690 "2023-09-26T04:15:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [September 26, 2023, 4:15am UTC](https://discuss.python.org/t/recursive-function/34690/1 "2023-09-26T04:15:04Z")

</div>

Hi. Would greatly appreciate any simple explanation on why the function below returns 56:

```auto
def fun(a):
    if a > 30:
        return 3
    else:
        return a + fun(a + 3)

print(fun(25))

```

Thank you so much.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [September 26, 2023, 7:10am UTC](https://discuss.python.org/t/recursive-function/34690/2 "2023-09-26T07:10:04Z")

</div>

There are many ways that people explore code to understand what it does.  
These are two very common ways.

You can run the code in your mind and reason about it line by line.  
Some people find it helpful to explain the code to another person.  
That other person does not need to be a programmer.  
The act of speaking out loud helps you understand code for lots of people, even experienced programmers.

The other common idea is to ask what you want to know as code runs and add print calls so that you can how code works. For example:

```auto
def fun(a):
    print(“start fun a=“, a)
    if a > 30:
        print(“return from if”)
        return 3
    else:
        print(“return from else”)
        return a + fun(a + 3)

print(fun(25))

```

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [September 26, 2023, 7:10am UTC](https://discuss.python.org/t/recursive-function/34690/3 "2023-09-26T07:10:50Z")

</div>

You first: what do you think it should return instead, and why?

---

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [September 26, 2023, 7:50am UTC](https://discuss.python.org/t/recursive-function/34690/4 "2023-09-26T07:50:27Z")

</div>

Thanks for the code showing the steps. Honestly, I am still a little bit confused.  
I thought answer would be 53… It is the " a + fun (a+3)" that confuses me.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [September 26, 2023, 8:00am UTC](https://discuss.python.org/t/recursive-function/34690/5 "2023-09-26T08:00:48Z")

</div>

If you run the version of the code I posted how many times is fun called and with what value of a?

---

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [September 26, 2023, 8:12am UTC](https://discuss.python.org/t/recursive-function/34690/6 "2023-09-26T08:12:14Z")

</div>

fun was called 3x.  
But for example, why is 2nd instance resulting to 28? When it is " a + fun (a+3)? Has an “a +” in front…

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [September 26, 2023, 8:12am UTC](https://discuss.python.org/t/recursive-function/34690/7 "2023-09-26T08:12:30Z")

</div>

> [@Moonlight001](#):
>
> It is the " a + fun (a+3)" that confuses me.

Well, what do you think the `a` part should have as a result? What do you think the `fun(a + 3)` part should have as a result? (Hint: what is `a + 3`? What does it mean if you write `fun()` and put something inside the parentheses?) What should you get if you add those two things together?

---

<div class="post-metadata">

**Author:** ![hansgeunsmeyer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hansgeunsmeyer/32/15188_2.png) [@hansgeunsmeyer](https://discuss.python.org/u/hansgeunsmeyer)\
**Post date:** [September 26, 2023, 3:32pm UTC](https://discuss.python.org/t/recursive-function/34690/8 "2023-09-26T15:32:14Z")

</div>

> [@Moonlight001](#):
>
> Thanks for the code showing the steps. Honestly, I am still a little bit confused.  
> I thought answer would be 53… It is the " a + fun (a+3)" that confuses me.

Does this make it clearer:

```auto
def fun(a):
    if a > 30:
        return 3
    else:
        x = a + 3
        y = fun(x)
        z = a + y
        return z

```

If you got this function from a course or tutorial, then the writer may have a devious sense of humor.  
It’s a pretty funky function, since its max is for fun(0) == fun(3) == 168, so it’s kind of difficult to get any intuition for how its behaving 🙂

---

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [September 27, 2023, 3:36am UTC](https://discuss.python.org/t/recursive-function/34690/9 "2023-09-27T03:36:44Z")

</div>

Yes I get the flow of “a + fun (a+3)”, as in your representation above. Now I know where I got it wrong lol So silly of me that I just focused on "“a + fun (a+3)”, which made me frustrated because the function becomes never ending–fun (28)…fun (31)…fun(33)… But I didn’t really absorbed the condition that above 30, it just returns 3. LOL Thanks for taking the time out for the representation!

---

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [September 27, 2023, 3:37am UTC](https://discuss.python.org/t/recursive-function/34690/10 "2023-09-27T03:37:09Z")

</div>

Yes, I get it now. Thank you!
