Default Parameter Values
If we called the functions from the previous lesson that had formal parameters, without one of those parameters,
we would definitely get an error. Referring back to the previous example, below we forgot to include the argument for type:
What can we do?
We can set default values for certain parameters directly in the function header, as shown below:
DETAILS
In the first call, the last argument is missing, but the program works, with the default value being '
str', so the concatenation
of three strings entered by the user is performed. In the second call, we specified that the conversion type should be for signed integers,
so inside the function, the case where
type is "
int" was chosen.
All parameters can have default values (for example,
0 and '
str'), so we can call the function even without any arguments:
Obviously, the resulting string will be formed by concatenating the three characters "
0", that is "
000".
Note. The type of the three variables holding the input data must be identical or convertible in this case.
If
x is of type
str, and
y and
z are of type
int... we will get an error. So, be careful with the data you are processing!
Proceed to the next page.