Section 1 - Instruction

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?

Section 2 - Instruction

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?

Section 3 - Instruction

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?

Section 4 - Instruction

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?

Section 5 - Instruction

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.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal