Skip to main content

How to write an initializer function

This page explains how to write an initializer function.

To learn more about initializers, read this.

Initializers are regular functions that set an "initialized" flag (a nullifier) for the contract. A contract can only be initialized once, and contract functions can only be called after the contract has been initialized, much like a constructor. However, if a contract defines no initializers, it can be called at any time. Additionally, you can define as many initializer functions in a contract as you want, both private and public.

Annotate with #[aztec(private)] and #[aztec(initializer)]

Define your initiaizer like so:

fn constructor(){
// function logic here

Initializer with logic

Initializers are commonly used to set an admin, such as this example:

fn constructor(admin: AztecAddress, name: str<31>, symbol: str<31>, decimals: u8) {
assert(!admin.is_zero(), "invalid admin");
Source code: noir-projects/noir-contracts/contracts/token_contract/src/

Here, the initializer is calling a public function. It can also call a private function. Learn more about calling functions from functions here.

To see constructors in action, check out the getting started guide.