Time for action – defining functions
Let's define the following simple function:
- Print
Hello
and a given name in the following way:>>> def print_hello(name): ... print('Hello ' + name) ...
Call the function as follows:
>>> print_hello('Ivan') Hello Ivan
- Some functions do not have arguments, or the arguments have default values. Give the function a default argument value as follows:
>>> def print_hello(name='Ivan'): ... print('Hello ' + name) ... >>> print_hello() Hello Ivan
- Usually, we want to return a value. Define a function, which doubles input values as follows:
>>> def double(number): ... return 2 * number ... >>> double(3) 6
What just happened?
We learned how to define functions. Functions can have default argument values and return values.