Walk through a recursive function you have written and explain how to optimize it to prevent stack overflow.