The idea of caching is common in programming and computer systems. Images loaded in the browser are cached to avoid further requests to the web server to download it in case the user visits the website again in the future. Caching makes programs run faster. The concept can be leveraged in many forms, including in single functions. For example, the following recursive function calculates the factorial of a number:
long factorial(long n) { if (n <= 1) { return 1; } return n * factorial(n - 1);}
The function doesn't remember its previously calculated values, so the following calls lead to five and six recursive calls, respectively:
factorial(5); // calls factorial(4), which calls factorial(3), and so onfactorial(6); // calls factorial(5), ...