Section 6.6 Writing new functions
So far, we have only been using the functions that come with Python, but it is also possible to add new functions. A function definition specifies the name of a new function and the sequence of statements that execute when the function is called. Once we define a function, we can reuse the function over and over throughout our program.
Here is an example of a function that takes two input numbers and gives back the average:
def average(a, b):
result = (a + b) / 2
return result
def
is a keyword that indicates that this is a function definition. The name of the function is average
. The rules for function names are the same as for variable names: letters, numbers and some punctuation marks are legal, but the first character can’t be a number. You can’t use a keyword as the name of a function, and you should avoid having a variable and a function with the same name.
In the parentheses, you specify parameters. Think of these as placeholders for the information that the user will provide when they call the function. This function has two parameters, which we name a
and b
.
The first line of the function definition is called the header; the rest is called the body. The header has to end with a colon and the body has to be indented. By convention, the indentation is always four spaces. The body can contain any number of statements.
The last line of the function body has the Python keyword return
. This is followed by an expression that gives the value to be returned to the function’s caller.
Checkpoint 6.6.2.
Q-2: What is the first line of a function definition called? What is every line after the first line called?
body; header
Incorrect! The order is off. Where does a header typically go? Try again.
title; body
Incorrect! Body is correct, but title is not. Try again.
header; body
Correct! The header is the first line of a function definition and the rest is the body.
initialization; body
Incorrect! We initialize variables, but functions are defined. Try again.
If you type a function definition in the Python shell, the interpreter prints ellipses (…) to let you know that the definition isn’t complete:
>>> def average(a, b):
... result = (a + b) / 2
... return result
...
To end the function, you have to enter an empty line. This is not necessary in a script, but it is a good idea to put an empty line after a function to increase the readability of your code.
The value of average
is a function object, which has type “function”.
The syntax for calling the new function is the same as for built-in functions. You give the name of the function, and, in parentheses, the arguments that will fill in the placeholder parameters.
In the preceding program, the value 3.5 (the first argument) “fills in” the first parameter a
, and 8.9 (the second argument) gets copied into the second parameter b
.
Checkpoint 6.6.3.
Construct a block of code that:
creates a function called “triangle_area” that takes the base and height of a triangle as its arguments and returns the triangle’s area
calls that function with arguments 4.5 and 7.3
prints the result of the function call
Watch your indentation!
def triangle_area(base, height):
---
def triangle_area(base, height) #paired
---
function triangle_area(base, height): #paired
---
result = 0.5 * base * height
return result
---
0.5 * base * height
return #paired
---
area = triangle_area(4.5, 7.3)
---
area = triangle_area 4.5 & 7.3 #paired
---
print("Triangle area is", area)