FormulaCraft

How to write a recursive LAMBDA function in Excel and Google Sheets

Topic:LAMBDA (reusable formulas)
Excel & Google Sheets
=LAMBDA(n,IF(n<=1,1,n*LAMBDA(n,IF(n<=1,1,n*1))(n-1)))(5)

Try it with your data

Edit the grid or formula, then run it through a real spreadsheet engine — no signup.

Sample data — click any cell to edit

Runs server-side · free · no signup

Step by step

  1. 1To create a named recursive LAMBDA in Excel, go to Formulas > Name Manager > New. Name it FACT, and in 'Refers to' enter: =LAMBDA(n,IF(n<=1,1,n*FACT(n-1))). Click OK.
  2. 2In any cell call it with =FACT(5) — the named LAMBDA calls itself recursively until n reaches 1.
  3. 3In Google Sheets, go to Data > Named functions > Add new function. Name it FACT, define the argument n, and enter IF(n<=1,1,n*FACT(n-1)) as the formula body.
  4. 4Inline (without naming), LAMBDA can only self-reference one level deep using nested LAMBDA calls. For true recursion, the named approach is required.
  5. 5Test with small values (n=5 gives 120, n=6 gives 720) and add a guard: IF(n>170,"overflow",IF(n<=1,1,n*FACT(n-1))) to prevent Excel overflow.

Tips

Need it for your exact data?

Describe your columns in plain English and get the precise formula for your sheet, with the right Excel or Sheets syntax.

Frequently asked

Can I use LAMBDA recursion without the Name Manager?

In Excel, true recursion requires a named LAMBDA because the function must reference itself by name. An inline LAMBDA has no name to recurse on. In Google Sheets, you must use Named Functions for the same reason.

What happens if the recursion has no base case?

Excel returns a #VALUE! or #CALC! error after hitting the recalculation limit. Always include an IF base case (e.g., IF(n<=1,1,...)) to terminate recursion.

Are there alternatives to LAMBDA recursion for iterative calculations?

Yes — REDUCE, SCAN, MAKEARRAY, and MAP cover most iterative patterns without true recursion and are generally faster and easier to debug.

More on LAMBDA (reusable formulas)

See all →

Formulas used

Written and reviewed by FormulaCraft Team. Each formula on this page is run through our verification engine before publishing.

Last reviewed: