Skip to content

Commit 96081e0

Browse files
committed
docs: add recursive function call tracing example
Add a recursive count/succ example to the tracing docs, showing how nested invoke/return contexts appear when functions call themselves repeatedly.
1 parent c2f2236 commit 96081e0

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎packages/web/docs/core-schemas/programs/tracing-examples.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,30 @@ create {
7171
code {
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+
}`;

‎packages/web/docs/core-schemas/programs/tracing.mdx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
98110
As you step through, three phases are visible:
99111

100112
### Before the call — setting up arguments

0 commit comments

Comments
 (0)