File tree Expand file tree Collapse file tree
packages/web/docs/core-schemas/programs Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -71,3 +71,30 @@ create {
7171code {
7272 result = add(3, 4);
7373}` ;
74+
75+ export const recursiveCount = `name Counter;
76+
77+ define {
78+ function succ(n: uint256) -> uint256 {
79+ return n + 1;
80+ };
81+ function count(n: uint256, target: uint256) -> uint256 {
82+ if (n < target) {
83+ return count(succ(n), target);
84+ } else {
85+ return n;
86+ }
87+ };
88+ }
89+
90+ storage {
91+ [0] result: uint256;
92+ }
93+
94+ create {
95+ result = 0;
96+ }
97+
98+ code {
99+ result = count(0, 5);
100+ }` ;
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import {
1010 thresholdCheck ,
1111 multipleStorageSlots ,
1212 functionCallAndReturn ,
13+ recursiveCount ,
1314} from " ./tracing-examples" ;
1415
1516# Tracing execution
@@ -95,6 +96,17 @@ trace. Watch for **invoke** contexts on the JUMP into `add` and
9596 source = { functionCallAndReturn }
9697/>
9798
99+ Recursive calls produce nested invoke/return pairs. In this
100+ example, ` count ` calls ` succ ` and then calls itself repeatedly
101+ until ` n ` reaches ` target ` . Each recursive call adds a frame
102+ to the call stack:
103+
104+ <TraceExample
105+ title = " Recursive function calls"
106+ description = " Counts from 0 to 5 using recursive succ and count functions"
107+ source = { recursiveCount }
108+ />
109+
98110As you step through, three phases are visible:
99111
100112### Before the call — setting up arguments
You can’t perform that action at this time.
0 commit comments