0Pricing
Mojo Academy · Lesson

Default Argument Values

Make some parameters optional.

Making a Parameter Optional

A default value lets a parameter stand in for itself. If the caller skips it, Mojo quietly uses the value you wrote in the definition.

fn greet(name: String, times: Int = 1):
    for _ in range(times):
        print(name)

Calling With and Without

Now both calls work. Omit times and you get the default of 1; pass it and you override the default for that one call.

greet("Mojo")        # times = 1
greet("Mojo", 3)     # times = 3

All lessons in this course

  1. Positional and Keyword Arguments
  2. Default Argument Values
  3. Returning Multiple Values
  4. Reading Function Signatures
← Back to Mojo Academy