# How to save recursive functions in julia

**URL:** <https://discourse.julialang.org/t/how-to-save-recursive-functions-in-julia/83644>\
**Category:** General Usage\
**Tags:** recursion\
**Created:** [July 1, 2022, 9:21pm UTC](https://discourse.julialang.org/t/how-to-save-recursive-functions-in-julia/83644 "2022-07-01T21:21:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArjunSingh26](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@ArjunSingh26](https://discourse.julialang.org/u/ArjunSingh26)\
**Post date:** [July 1, 2022, 9:21pm UTC](https://discourse.julialang.org/t/how-to-save-recursive-functions-in-julia/83644/1 "2022-07-01T21:21:25Z")

</div>

Hi everyone,

This summer, I’m doing some research involving mathematical particle simulations. Part of my work revolves around a “shape function”, which for the purposes of this post, is simply a function that takes an input n (called the “order” of the function), and returns an output value y. Here is an example of how such a function would work.

y\_test = function(n)  
if n == 0:  
y = 0  
elseif n == 1:  
y = 1  
else:  
y = 1 + y\_test(n-1)  
end  
end

As you can see, this function is recursive. In order to access shape functions of higher order, you must first calculate the function for lower orders. In this example, it is very easy to manually calculate higher-order shape functions. However, in the function I am using in my research, it is much more complicated, and impractical to do it by hand. I am wondering if there was a way that I could store the compiled version of a higher-order function without having to calculate the lower-order versions? For instance, is there a way for me to access y\_test(5) without having to recalculate y\_test(4), y\_test(3), and so on every single time I run y\_test(5)? Perhaps there is a way to run the recursive loop once and from there on have access to y\_test(5) without running the recursion again?

Thanks for you help!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 1, 2022, 9:31pm UTC](https://discourse.julialang.org/t/how-to-save-recursive-functions-in-julia/83644/2 "2022-07-01T21:31:22Z")

</div>

> **[GitHub - willow-ahrens/MemoizedMethods.jl: Methodwise Memoization for Julia](https://github.com/willow-ahrens/MemoizedMethods.jl)**
>
> Methodwise Memoization for Julia. Contribute to willow-ahrens/MemoizedMethods.jl development by creating an account on GitHub.
