Last time, we built functions that take parameters and return results. But sometimes we want to make parameters optional, so users don't always have to provide every piece of information.
Engagement Message
What if some parameters could have backup values?
We can give parameters default values! When we define a function, we assign a value to a parameter using =
. If someone doesn't provide that argument, the function uses our default instead.
Engagement Message
Notice the ="friend"
part?
Now we can call this function in two ways. We can provide our own argument, or skip it and let the function use its default value:
Engagement Message
See how the same function handles both situations?
Functions can mix required parameters (no defaults) with optional ones (with defaults). The rule is: required parameters must come first, then optional ones after.
Engagement Message
Why do you think required parameters must come first?
Default parameters make functions more flexible. Users can provide just the essential information and get sensible defaults for everything else. This is especially useful for complex functions with many settings.
