What is Leaf function?

A function that calls no other functions. Its return address is never overwritten, so it need not be saved, and it often needs no stack frame at all.

Field: ISA & Assembly

Japanese name: 葉関数

Why it can be treated specially

In RISC-V and Arm, the instruction that calls a function places the return address in a register. If the called function calls another function in turn, this register is overwritten with the new return address, so the original value must be saved on the stack before the call. A leaf function calls no other function and needs no such save. To return, it simply goes back to the address still in the register.

What else can be omitted

If the registers used for computation are limited to caller-saved registers, which need no saving, there is not a single save or restore. The stack pointer need not be moved either, and no stack frame has to be created. Under the convention used on x86-64 Linux and similar systems, the 128 bytes below the stack pointer may be used as temporary storage without moving the stack pointer. A leaf function can use this area to keep variables on the stack.

Its weight in a program

If function calls are drawn as a tree, the functions at the leaves are numerous and are called often during execution. Many small computations and functions that merely fetch a value are leaf functions. Making the entry and exit of leaf functions light therefore helps overall speed. When a compiler inlines a small function into its caller, the function that used to call it may itself become a new leaf function.

Try it: questions about Leaf function

A function that calls no other functions (a leaf function) often does not need to save ra on the stack. Why?

  1. ABecause leaf functions do not use the ret instruction
  2. BBecause the CPU automatically pushes the return address of leaf functions
  3. CBecause it calls no one, ra is never overwritten and the return address stays intact
Show answer and explanation

Answer: C. Because it calls no one, ra is never overwritten and the return address stays intact

Every jal overwrites ra, so a function that calls others must first save its own return address on the stack. A leaf function makes no calls that write ra (jal ra or jalr ra), so ra stays intact and it can ret without saving it. If it also avoids callee-saved registers, it does not even need a stack frame. x86's call instruction always pushes the return address on the stack, which is a difference from RISC-V.

FAQ

Does a leaf function never use the stack?

It sometimes does, for example when it has a large array as a local variable or more variables than fit in the registers.

Is a recursive function that calls itself a leaf function?

No. Even though the function called is itself, it is still a call, so the return address must be saved.

Books on ISA & Assembly Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary