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 = 3All lessons in this course
- Positional and Keyword Arguments
- Default Argument Values
- Returning Multiple Values
- Reading Function Signatures