How does Python know what to do when we call functions like abs or procedures like alex.forward(50)? Someone had to define them. Defining a new procedure or function associates a name with a sequence of steps.
In Python, we define a new procedure or function with the keyword def. To use def, we also need to specify: a name for the procedure, a list of its inputs, and the instructions the procedure will perform in this format:
The procedure name should obey the same rules as other names. Both syntax rules (no spaces or odd characters) and style conventions (camelCase or snake_case). Like other names, it should be meaningful - it should describe what task it performs.
Here is a definition for the square procedure we tried to use on the last page. The square procedure takes one input (called turtleName). It has 8 instructions in its body. Try running this code sample and then keep reading:
Why didn’t the Run button do anything? It is because the program defined the procedure square, but it never actually told Python to do the square procedure!
Defining a procedure or function tells Python HOW to do a particular job. It does NOT tell Python to do the job. To do the procedure or function, we must call it.
We call a procedure by using its name and then giving it the right number of inputs. Our square function requires one input - the name of the turtle that should make a square - so when we call sqaure we must provide the name of a turtle.