You’re already familiar with the
len() functions from the previous chapters. Python provides several builtin functions like these, but you can also write your own functions. A function is like a mini-program within a program.
To better understand how functions work, let’s create one. Type this program into the file editor and save it as helloFunc.py:
➊ def hello(): ➋ print('Howdy!') print('Howdy!!!') print('Hello there.') ➌ hello() hello() hello()
The first line is a
def statement ➊, which defines a function named
hello(). The code in the block that follows the
def statement ➋ is the body of the function. This code is executed when the function is called, not when the function is first defined.