({{, (RefLink[$RecursionLimit, paclet:ref/$RecursionLimit])
gives the current limit on the number of levels of recursion that the Wolfram Language can use. }})

OpenersNotesSection(Spacer1)Details

$RecursionLimit=n sets the limit on the number of recursion levels that the Wolfram Language can use to be n.

$RecursionLimit=(RefLink[Infinity, paclet:ref/Infinity]) removes any limit on the number of recursion levels.

(RefLink[$RecursionLimit, paclet:ref/$RecursionLimit]) gives the maximum length of the stack returned by (RefLink[Stack, paclet:ref/Stack])[].

Each time the evaluation of a function requires the nested evaluation of the same or another function, one recursion level is used up.

On most computers, each level of recursion uses a certain amount of stack space. (RefLink[$RecursionLimit, paclet:ref/$RecursionLimit]) allows you to control the amount of stack space that the Wolfram Language can use from within the Wolfram Language. On some computer systems, your whole Wolfram Language session may crash if you allow it to use more stack space than the computer system allows.

(RefLink[MemoryInUse, paclet:ref/MemoryInUse]) and related functions do not count stack space.

OpenersPrimaryExamplesSection(Spacer1)Examples  (6)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

(RefLink[$RecursionLimit, paclet:ref/$RecursionLimit]) limits infinite recursion with circular definitions:

x=x+1

Temporarily reset (RefLink[$RecursionLimit, paclet:ref/$RecursionLimit]):

Block[{$RecursionLimit=1000},x=x+1]

OpenersExampleSection1(Spacer1)Scope  (1)

Use (RefLink[ReleaseHold, paclet:ref/ReleaseHold]) to keep going with the recursion:

x=x+1

ReleaseHold[%]

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (1)

The recursion limit can be set to ∞ to allow any number of recursion steps:

f[0]=0;
f[n_]:=f[n-1]+1

Block[{$RecursionLimit=Infinity},f[10^4]]

OpenersExampleSection3(Spacer1)Properties & Relations  (2)

The recursion limit protects against recursive runaway definitions:

f[x_]:=f[Floor[x/2]]+1

f[10]

The iteration limit protects against runaway definitions:

f[x_]:=f[x+1]

f[1]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[$IterationLimit, paclet:ref/$IterationLimit])  ▪  (RefLink[MemoryConstrained, paclet:ref/MemoryConstrained])  ▪  (RefLink[TimeConstrained, paclet:ref/TimeConstrained])  ▪  (RefLink[MaxRecursion, paclet:ref/MaxRecursion])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Evaluation, paclet:tutorial/Evaluation])}, {▪(RefLinkPlain[Controlling Infinite Evaluation, paclet:tutorial/ControllingInfiniteEvaluation])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Global Computation Settings & Parameters, paclet:guide/GlobalComputationSettingsAndParameters])}, {▪(RefLinkPlain[Evaluation Control, paclet:guide/EvaluationControl])}, {▪(RefLinkPlain[Session Customization, paclet:guide/SessionCustomization])}, {▪(RefLinkPlain[Wolfram System Setup, paclet:guide/WolframSystemSetup])}, {▪(RefLinkPlain[Memory Measurement & Optimization, paclet:guide/MemoryMeasurementAndOptimization])}, {▪(RefLinkPlain[Wolfram System Sessions, paclet:guide/WolframSystemSessions])}})}})