0% found this document useful (0 votes)
17 views9 pages

01_02_function-basics-lesson-notes-optional-download_User-Defined Functions - Function Basics

Uploaded by

RAKESH SWAIN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

01_02_function-basics-lesson-notes-optional-download_User-Defined Functions - Function Basics

Uploaded by

RAKESH SWAIN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Chapter 8 - User-Defined

Functions

User-Defined Functions

Function Basics

Learning Objective - Function


Basics
Demonstrate how to define a function

Differentiate between the function header and the


function body

Identify the proper order of declaring and calling


functions

Explain the importance of whitespace in functions

Identify the purpose of a docstring


Function Definition

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.

Fuction Header & 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

What happens if you:


Add another line code that says greet_twice()?
Indent greet_twice four spaces?
Add a 1 between the parentheses of the function call
greet_twice(1)?
Functions and Whitespace

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

What happens if you:


Change the code to look like this:

def greet_twice():
print("Hello")
print("Hello")
greet_twice()

Change the code to look like this:


def greet_twice():

print("Hello")

print("Hello")

greet_twice()

Change the code to look like this:

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

What happens if you:


Replace len with max?
Change the code to look like this:

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

What happens if you:


Remove the indentation for the docstring?
Indent the docstring and then change it to:

"""Print the string


'Hello' two times"""

Change the docstring to "Print the string 'Hello' two times"?


Change the docstring to:

"Print the string


'Hello' two times"
When to Use a Function

Divide and Conquer


If you had to explain how to draw a house, you would make a list of
shapes to draw. Draw a triangle for the roof, draw a square for the
house, draw a rectangle for the door, etc. Combine all of these shapes,
and you have a house. This approach to problem solving makes
complex problems easier to understand, easier to solve, and easier to
fix.

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

You might also like