# How do I think about recursion.

**URL:** <https://forum.kirupa.com/t/how-do-i-think-about-recursion/655414>\
**Category:** Uncategorized\
**Created:** [December 10, 2022, 2:09pm UTC](https://forum.kirupa.com/t/how-do-i-think-about-recursion/655414 "2022-12-10T14:09:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [December 10, 2022, 2:09pm UTC](https://forum.kirupa.com/t/how-do-i-think-about-recursion/655414/1 "2022-12-10T14:09:13Z")

</div>

I got the idea for problems like factorials or finding sum from 1 to n, where there’s a pattern visible. Like this:

 ![](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/f/f/ffa6a12e86a7213017724942997762897cb8e11c.png)

Source:[https://99x.io/blog/recursion-is-not-hard-here-is-the-right-way-to-think](https://99x.io/blog/recursion-is-not-hard-here-is-the-right-way-to-think)

But I’m confused for things like addition. I know the answer but I am unable to figure out how we came to the answer. Could you guys help me on this?

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [December 10, 2022, 9:56pm UTC](https://forum.kirupa.com/t/how-do-i-think-about-recursion/655414/2 "2022-12-10T21:56:42Z")

</div>

Can you give an example of what you mean for addition? Are you trying to add two numbers using a recursive approach?

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [December 11, 2022, 4:11am UTC](https://forum.kirupa.com/t/how-do-i-think-about-recursion/655414/3 "2022-12-11T04:11:51Z")

</div>

I got for addition of two numbers. Can you describe one for division of two numbers a/b?

For addition, it’s a+b=a-1+1+b  
=(a-1)+(b+1)  
=Sum(a-1,b+1).  
Now I need for division

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [December 11, 2022, 4:19am UTC](https://forum.kirupa.com/t/how-do-i-think-about-recursion/655414/4 "2022-12-11T04:19:00Z")

</div>

I got for division as well. It’s  
(a-b+b)/b  
= (a-b)/b + b/b  
=1+(a-b)/b/

---

<div class="post-metadata">

**Author:** ![steve.mills](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/steve.mills/32/14961_2.png) [@steve.mills](https://forum.kirupa.com/u/steve.mills)\
**Post date:** [December 13, 2022, 3:40am UTC](https://forum.kirupa.com/t/how-do-i-think-about-recursion/655414/5 "2022-12-13T03:40:26Z")

</div>

Hey mate,  
There are multiple way to call functions repeatedly.

You can for example save the result of a function call to a variable and then call it again e.g.

```auto
// call the add function with the previous variable until the last run equals 10
function add (z){ return z + 1 }

let run1 = add(1)
let run2 = add(run1)
let run3 = add(run2)
let run4 = add(run3)
let run5 = add(run4)
let run6 = add(run5)
let run7 = add(run6)
let run8 = add(run7)
let run9 = add(run8)

```

Or call it in a loop e.g.

```auto
function add (z){ return z + 1 }

// while "a" is less 10 make "a" equal to add "a" BUT stop at 10
let a = 1;
while( a < 10 ){ a = add(a) }

```

Or you have a function call itself from inside itself ( **recursion** ) e.g.

```auto
// if x equals 10 return x else call recursion with the value of x again
function recursion (x){
  if(x == 10) return x
  else return recursion(x + 1)
}

recursion(1)

```

The main thing that should concern you is having a break case so you don’t get excessive stacks or an endless loop…
