Skip to content

Mojo Argument Conventions

When you call a function in Python, the function gets the same object you passed, and it can change it if the object is changeable. Mojo lets each argument say exactly what the function will do with the value: just look at it, change it, or take it over. The keyword that says this is called the argument convention. You’ve already met mut in Mojo Function Arguments; this page covers all six conventions and when to pick each one.

Convention The function gets Can it change the value? Does the caller keep it?
(none) a read-only reference No Yes
mut a mutable reference Yes, and the caller sees the change Yes
var its own value (a copy or the original) Yes, its own copy Yes, unless the caller passes x^
out an empty slot it must fill It creates the value It is the result
ref a reference that’s read-only or mutable, depending on what was passed Only if the caller’s value is mutable Yes
deinit the value, to destroy it Yes No

You’ll use the first three all the time. out, ref and deinit show up mostly inside structs and libraries. The Mojo manual’s ownership page describes each of them.

If you write no keyword, the argument is a read-only reference to the caller’s value. The function sees the original (no copy is made), but can’t change it:

def total(values: List[Int]) -> Int:
var sum = 0
for v in values:
sum += v
return sum
def main():
var prices: List[Int] = [3, 4, 5]
print(total(prices))
print(prices)

Output:

12
[3, 4, 5]

This is the right choice most of the time. It’s cheap even for a huge list, because nothing is copied, and the caller knows its value is safe. If the function tries to change a read-only argument, Mojo stops it at compile time:

def change(x: Int):
x += 1
def main():
change(3)
hello.mojo:2:5: error: expression must be mutable for in-place operator destination
x += 1
^
mojo: error: failed to parse the provided Mojo source module

Put mut before an argument when the function is meant to change the value it was given. The change happens to the caller’s variable, not to a copy:

def add_bonus(mut scores: List[Int], bonus: Int):
for ref s in scores:
s += bonus
def main():
var scores: List[Int] = [70, 85, 90]
add_bonus(scores, 5)
print(scores)

Output:

[75, 90, 95]

(for ref s makes each loop variable a reference into the list, so s += bonus changes the item itself. Without ref, loop variables are read-only.)

A mut argument needs something that can actually change, so you have to pass a variable, not a plain value:

def double(mut n: Int):
n *= 2
def main():
double(5)
hello.mojo:4:12: error: invalid call to 'double': value passed to mutable argument 'n' must be mutable
double(5)
~~~~~~ ^
mojo: error: failed to parse the provided Mojo source module

Mojo has a rule called argument exclusivity: if a function gets a mutable reference to a value, it can’t get any other reference to that same value in the same call. Here’s why that’s useful:

def append_twice(mut s: String, other: String):
s += other
s += other
def main():
var text = String("o")
append_twice(text, text)
print(text)
hello.mojo:7:17: error: aliasing values passed mutably to 's' argument and passed immutably to 'other' argument in 'append_twice' call
append_twice(text, text)
^~~~~ ~~~~
mojo: error: failed to parse the provided Mojo source module

If this were allowed, the first s += other would also change other (they’re the same string), and you’d get "ooo" or "oooo" depending on details you can’t see. Mojo refuses to guess. If you really want to pass the same text twice, pass a copy as the second argument: append_twice(text, text.copy()).

The rule doesn’t apply to small values like Int and Bool, because those are always passed as copies anyway. add(n, n) with def add(mut x: Int, y: Int) compiles fine.

A var argument means “this function gets its own value, which it can change freely”. What the caller passes decides where that value comes from:

def shout(var text: String) -> String:
text += "!"
return text
def main():
var msg = String("hi")
print(shout(msg))
print(msg)
print(shout(msg^))

Output:

hi!
hi
hi!
  • shout(msg): Mojo makes a copy for the function. msg is untouched, as the second line shows.
  • shout(msg^): the caller hands over the original with ^. No copy is made, but msg can’t be used after this line.
  • You can also pass a brand-new value, like shout("hey"). Nobody else owns it, so it goes straight to the function.

The copy in the first case only happens automatically for cheap types like String and Int. A List won’t be copied behind your back:

def total(var items: List[Int]) -> Int:
var s = 0
for x in items:
s += x
return s
def main():
var nums: List[Int] = [1, 2, 3]
print(total(nums))
hello.mojo:9:17: error: value of type 'List[Int]' cannot be implicitly copied, it does not conform to 'ImplicitlyCopyable'
print(total(nums))
^~~~
hello.mojo:9:17: note: consider transferring the value with '^'
hello.mojo:9:17: note: you can copy it explicitly with '.copy()'
mojo: error: failed to parse the provided Mojo source module

You’d write total(nums^) to hand it over or total(nums.copy()) to keep yours. In this case, though, total only reads the list, so the real fix is to drop the var and use the default convention. Use var when the function needs to keep the value (store it in a list or struct) or change it without affecting the caller. Mojo Moving Values covers the ^ side in detail.

An out argument starts empty, and the function must fill it in before it returns. The caller never passes it: it becomes the function’s return value. It’s mostly used in __init__ (every initializer has out self), but you can use it for a named result in any function:

def describe(n: Int, out text: String):
text = String("number ") + String(n)
if n < 0:
text += " (negative)"
def main():
print(describe(-3))
print(describe(4))

Output:

number -3 (negative)
number 4

describe has no -> and no return. Mojo builds the result directly in text, and the caller gets it as the return value. In your own functions, -> String and return are usually clearer; out matters most in initializers, which you’ll see in Mojo Value Lifecycle.

A ref argument is a reference whose mutability isn’t fixed: it’s mutable if the caller had a mutable value, and read-only if not. This lets one function serve both cases. It’s most useful together with a reference return value, like here:

def first_word(ref words: List[String]) -> ref[words[0]] String:
return words[0]
def shout_first(mut words: List[String]):
first_word(words) += "!"
def show_first(words: List[String]):
print(first_word(words))
def main():
var words: List[String] = ["hello", "world"]
shout_first(words)
show_first(words)

Output:

hello!

first_word returns a reference to the first item, not a copy. Inside shout_first, words is mutable, so the reference is mutable and += "!" changes the list. Inside show_first, words is read-only, so the same call gives a read-only reference. Trying first_word(words) += "!" there would be the “must be mutable” error. The [words[0]] part is an origin: it tells Mojo where the reference points. That’s the topic of Mojo References & Origins.

deinit is the convention for a value that’s about to be destroyed. You’ll see it in two places: the destructor def __deinit__(deinit self) and the move initializer def __init__(out self, *, deinit move: Self). Both get their own lessons, Mojo Destructors and Mojo Value Lifecycle. You won’t need it in everyday functions.

Methods use the same keywords on self. You met the first two in Mojo Methods:

You write Meaning
def show(self) read-only, the default
def bump(mut self) the method changes the instance
def __init__(out self, ...) the method creates the instance
def __deinit__(deinit self) the method destroys the instance
def get(ref self) mutability follows the caller
  • Just reading the value? No keyword. This is the answer most of the time.
  • Need to change the caller’s value? mut.
  • Need to keep the value, or change a private copy? var (and the caller can pass x^ to avoid a copy).
  • Returning a reference into an argument? ref, with an origin on the return type.
  • Writing an initializer or destructor? out self and deinit self.

In Python, every argument is passed the same way: the function gets the same object, and whether it can change it depends on the type (lists yes, numbers and strings no). In Mojo, the signature decides, for every type. Reading the def line tells you exactly what a function can do to your values.

This program should print [2, 4, 6] and then 12. It doesn’t compile. Change only the function signatures.

def double_all(values: List[Int]):
for ref v in values:
v *= 2
def sum_all(var values: List[Int]) -> Int:
var s = 0
for v in values:
s += v
return s
def main():
var nums: List[Int] = [1, 2, 3]
double_all(nums)
print(nums)
print(sum_all(nums))

Hint: one function changes the list, the other only reads it.

Show solution
def double_all(mut values: List[Int]):
for ref v in values:
v *= 2
def sum_all(values: List[Int]) -> Int:
var s = 0
for v in values:
s += v
return s
def main():
var nums: List[Int] = [1, 2, 3]
double_all(nums)
print(nums)
print(sum_all(nums))

Output:

[2, 4, 6]
12

double_all changes the caller’s list, so it needs mut. sum_all only reads, so the default convention is right; var asked for its own copy of the list, which Mojo won’t make implicitly.

Write def greet(var name: String) -> String that adds "Hello, " in front of name and returns it. In main, call it once with a variable (and print the variable afterwards to show it didn’t change), and once with a string literal.

Hint: inside the function, name is your own copy, so you can reassign it.

Show solution
def greet(var name: String) -> String:
name = "Hello, " + name
return name
def main():
var who = String("Ana")
print(greet(who))
print(who)
print(greet("Ben"))

Output:

Hello, Ana
Ana
Hello, Ben

The first call gets a copy of who, so who still says Ana. The literal "Ben" becomes a new value that the function owns straight away.

  • No keyword means a read-only reference: no copy, no changes. Use it by default.
  • mut gives a mutable reference; changes reach the caller. You must pass a variable.
  • Argument exclusivity: a value passed as mut can’t also be passed as another argument in the same call.
  • var gives the function its own value: a copy (cheap types only), the original with x^, or a new value.
  • out builds the result in place; every __init__ has out self.
  • ref follows the caller’s mutability, and deinit is for destructors and moves.

Next up: moving values, everything about the ^ operator and when Mojo moves a value for you.

Quick check

1. A function only needs to read a big list. Which convention should its argument use?
2. Why does append_twice(text, text) fail when the first argument is mut?
3. With def shout(var text: String), what does shout(msg) (no ^) do?
4. Where do you always see the out convention?