When setting default parameters, it is important to note that they will only be used if no arguments are passed in when the function is called. Otherwise, the arguments passed in will override the default values. This feature can be especially useful in real-world scenarios where a function may not receive all the arguments it needs to run properly. By setting default values for the parameters, we can ensure that our function still executes correctly.
We learn how to create a function with default parameters using a simple example called “sayGreeting”. In this example, the function has a single parameter called “name” and a default parameter called “greeting” which is set to “Hello”. This means that if no greeting is passed in when the function is called, it will default to “Hello”. However, if a greeting is passed in, it will override the default value.
To call a function with default parameters, we simply pass in the values for any parameters we want to use, and leave the default ones empty if we want to use the default value. The lesson explains how to properly provide arguments to a function with default parameters, so make sure to understand this before moving on to the next lessons.