Functions#
Just like in Bash, you can define functions in Python and they're used in the same way and for the same reasons. And defining one is equally as simple:
1 2 3 4 5 6 7 8 |
|
Note
When defining the function in the terminal, I had to press Enter twice.
When using just the name of the function, without the ()
, then we can see we just have a reference to a function - <function hello_world at 0x000001F07B7D9310>
- which means we have a function at memory address 0x000001F07B7D9310
(in RAM.)
When we use the ()
, we call the function referenced at 0x000001F07B7D9310
, and its body is executed.
Functions can, of course, have parameters too, even optional ones!
1 2 3 4 5 6 7 8 9 10 |
|
The person
parameter is required. It cannot be omitted, as we can see in the last call to the function. But what if we want to make it optional? Well, we just need to provide a default value:
1 2 3 4 |
|
And that's it.
We can also return values from functions too, as you might have suspected:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
And at this point, that's all you need to know.