Larzscript Course · Lesson 11 of 15

Gas-Metered Functions

Larz covers Larzscript's gas metering: gas N declares a real, checkable cost on a function, and the --ledger flag prints the total gas a run actually spent.

gas N โ€” declaring a cost

Add gas N right after the parameters. It doesn't change what the function returns - just what it costs to call, something a host running your code can check:

fn add(a, b) gas 10 {
    return a + b
}
fn mul(a, b) gas 25 {
    return a * b
}

print(add(2, 3))
print(mul(4, 5))
Output
5
20

--ledger โ€” metering what ran

Run the exact same file with larzscript --ledger and it prints the total gas spent - 10 for add, 25 for mul:

fn add(a, b) gas 10 {
    return a + b
}
fn mul(a, b) gas 25 {
    return a * b
}

print(add(2, 3))
print(mul(4, 5))
Output
--- ledger (0) ---
gas used: 35
5
20

Every snippet on this page is run through the real larzscript interpreter, not hand-typed โ€” what you see in the Output blocks is the actual output, for everything shown: a plain run and a --ledger run of the same gas-annotated code.

← Lesson 10Course syllabusGitHub