01_02_function-basics-lesson-notes-optional-download_User-Defined Functions - Function Basics
01_02_function-basics-lesson-notes-optional-download_User-Defined Functions - Function Basics
Functions
User-Defined Functions
Function Basics
Function Syntax
You have seen and used built-in functions like the length function
(len(my_list)). This unit deals with user-defined function. Functions are
composed of two parts, the header and the body.
The function header contains the def keyword which signals the
definition of a function. Next is the name of the function. Function
names follow the same rules as variable names; letters, numbers, and
underscores. Function names cannot start with a number. Parentheses
are required, and any parameters go between them. Finally, the header
ends with a colon.
Function Header
The function body is the list of actions the function performs. All of the
code for the function body must be indented (four spaces is the Python
standard) from the function header. The function ends when the code is
no longer indented.
Function Body
Calling a Function
Enter the code below into the editor and click the TRY IT button. Nothing
is printed. Defining a function does not cause Python to run it.
def greet_twice():
print("Hello")
print("Hello")
You have to explicitly call the function if you want it to run. Add
greet_twice() after the function definition. Remember do not indent the
function call. Run the code again.
def greet_twice():
print("Hello")
print("Hello")
greet_twice()
challenge
Whitespace
Whitespace refers to indentations and blank lines in your program.
Indentations matter greatly for Python; your program can change
greatly when indentation is not properly done. Notice that there is no
function call in the code below. What do you think will happen when you
run this program?
def greet_twice():
print("Hello")
print("Hello")
So the first print statement does not run because there is no function
call. However, the second print statement is not a part of the function
definition because it is not indented. So it will run when the program is
executed.
challenge
def greet_twice():
print("Hello")
print("Hello")
greet_twice()
print("Hello")
print("Hello")
greet_twice()
def greet_twice():
print("Hello")
print("Hello")
greet_twice()
Order Matters
The order of function definitions and function calls is important in
Python. In the code below, the function call appears before the function
definition. What do you think will happen when you run the code?
greet_twice()
def greet_twice():
print("Hello")
print("Hello")
Python says that greet_twice is not defined. But two lines later the
function is clearly defined. Python requires that functions be defined
before they are called.
Docstring
Function Documentation
Python has a built-in function called help that explains how other
functions work. This is a handy way of learning how to use a function
without having to look it up in the official documentation.
help(len)
challenge
def greet_twice():
print("Hello")
print("Hello")
help(greet_twice)
Docstring
The help function does not provide any information for user-defined
functions. Adding a docstring to a user-defined function will provide
output for the help function. A docstring goes between the function
header and the function body. Use triple-quotes to create a string which
explains what the function does and how to use it. Remember, triple-
quotes respect all of the whitespace in the string. You can indent or add
a new line to increase readability.
Docstring
def greet_twice():
"""Print the string 'Hello' two times"""
print("Hello")
print("Hello")
help(greet_twice)
challenge
Functions allow for the same divide and conquer approach to problem
solving but for programming. Each of the tasks above become a
function. Combine all of the functions together, and you should have a
house.
import turtle
t = turtle.Turtle()
def roof():
"""Draw a triangle to represent a roof"""
for i in range(3):
t.lt(120)
t.forward(100)
def house():
"""Draw a rectangle to represent a house"""
for i in range(4):
t.rt(90)
t.forward(100)
roof()
house()
turtle.mainloop()
challenge
Add a Door
Write a new function that adds a door to the house. Hint, move the
turtle to an appropriate starting position before drawing the door.
One Possible Solution