Designing functions with optional parameters
When we define a function, we often have a need for optional parameters. This allows us to write functions which are more flexible, and can be used in more situations.
We can also think of this as a way to create a family of closely-related functions, each with a slightly different collection of parameters – called the signature – but all sharing the same simple name. The idea of many functions sharing the same name can be a bit confusing. Therefore, we'll focus more on the idea of optional parameters.
An example of optional parameters is the int()
function. This has two forms:
int(str)
: For example, the value ofint('355')
has a value of355
. In this case, we didn't provide a value for the optionalbase
parameter; the default value of10
was used.int(str, base)
: For example, the value ofint('0x163', 16)
is355
. In this case, we provided a value for thebase
parameter.
Getting ready
A great many games rely on collections of dice. The casino game of...