Python Functions: def, Parameters and return
About 35 minutes
- Variables
- Strings and basic data types
- Lists
- Dictionaries
- Conditions
- for loops
- while loops
- break
- continue
- set
- Function
- def
- Function call
- Parameter
- Argument
- return
The counter at the top counts this lesson's exercises (8 of them). Reading and running the examples don't count.
The problem functions solve
In the previous lessons we started writing code that runs different instructions. As your code grows, a simple problem shows up: you may need to run the same set of instructions more than once.
You could copy the same code every time, of course. But the more you repeat code, the harder it gets to change, because every change has to be repeated in every copy.
A function lets us put a set of instructions under one name, and then run those instructions whenever we need them, using that name.
What a function is
A function is a set of instructions we give a name to. The instructions don't run when you define the function; they run when you call it.
You've already used ready-made functions, like print. When you write print, you're asking Python to run a function that already exists.
In this lesson we start making functions of our own.
How to write a function
- We start with the word
def, which tells Python we're defining a new function. - After
def, we write the function's name. - After the name, we write brackets
(). - After the brackets, we write
:. - The line or lines inside the function have to be indented, because Python needs to know which instructions belong to the function.
Hello
- 1
Python reads def say_hello() and learns there's a function called say_hello. - 2
The print line inside it doesn't run at the moment the function is defined. - 3
Then Python reaches say_hello(). - 4This is where we call the function.
- 5
Python goes into the function and runs print("Hello"). - 6
That's why Hello appears in the output.
Defining a function doesn't run it
Function created
- 1
say_hello was defined. - 2
But we didn't write say_hello() after the definition. - 3
So the instructions inside the function didn't run. - 4
The only line that ran was print("Function created").
Predict the output
Before you run the code, predict how many times the line Checking page will appear.
- 1The function was defined once.
- 2Then it was called twice.
- 3Each call runs the instructions inside the function from the start.
- 4So the line appears twice.
What a parameter is
So far, the function does the same thing every time. But in real work, we usually need to give it different data.
A parameter is a variable name we write in the function's definition, so the function can receive a value when it runs.
The value we send when we call the function is called an argument.
Parameters and arguments, with a simple example
Hello Sarah Hello James
- 1When we defined greet, we wrote name inside the brackets, so name is a parameter.
- 2In the first call we sent the value Sarah, so Sarah is an argument.
- 3Inside the function, name became Sarah, so Hello Sarah appeared.
- 4In the second call we sent James.
- 5name became James for that call only, so Hello James appeared.
Passing a URL to a function
Checking: https://example.com/ Checking: https://example.com/about
- 1
The function is called show_url, and it receives a parameter called url. - 2
In the first call, url took the home page's value. - 3Checking was printed, then the value of url.
- 4
In the second call, url took the about page's value. - 5
The same function worked on two different pages without repeating the code's logic.
A function can take more than one parameter
A function can receive more than one value. In that case, we write several parameters inside the brackets, separated by commas.
And when we call the function, we send the values in the same order.
URL: https://example.com/ Status: 200
- 1The function receives url and status.
- 2The first value we sent went to url.
- 3The second value, 200, went to status.
- 4Then the two print lines ran, using the values the function received.
The difference between print and return
print only shows a value on the screen.
return sends a value back from the function to the place that called it.
This difference matters, because we usually don't just want to show the result. We want to keep it in a variable, or use it in a condition, or in another part of the program.
A first example of return
200
- 1
get_status was defined. - 2
When we called get_status(), Python went into the function. - 3It reached return 200, so the function sent back the value 200.
- 4
The value that came back was stored in the variable page_status. - 5
Then we printed page_status, and 200 appeared.
return with a parameter
True False
- 1In the first call, status became 200.
- 2
Python checked the condition status == 200, and the result was True. - 3return sent back True, which was stored in result and then printed.
- 4In the second call, status became 404.
- 5
The condition 404 == 200 is False. - 6return sent back False, so False appeared.
A function that makes a simple decision
OK Check page
- 1In the first call, the value 200 reached status.
- 2
The condition status == 200 was met. - 3
return "OK" ran. - 4In the second call, 404 arrived.
- 5
The condition wasn't met, so Python moved on to else. - 6
return "Check page" ran.
Using a function inside a loop
Checking: https://example.com/ Checking: https://example.com/about Checking: https://example.com/contact
- 1We have a list with three URLs.
- 2
We defined a function called check_url that receives one URL. - 3The for loop started and took the first URL from the list.
- 4
That URL was sent to check_url, which printed it. - 5The loop came back, took the second URL and ran the same function.
- 6The same happened with the third URL.
- 7Here the loop is in charge of going through the pages, and the function is in charge of the work we want done on each page.
An SEO example that brings it together
https://example.com/ -> OK https://example.com/about -> OK https://example.com/old-page -> Check status
- 1We have a list called pages, and every item in it is a dictionary with url and status.
- 2
The function is called audit_page, and it receives one page. - 3Inside the function, we check the value of status.
- 4
If it's 200, we print the URL with OK. - 5
If it isn't 200, we print the URL with Check status. - 6The for loop goes through every dictionary in pages.
- 7
Each round, it sends the current page to audit_page. - 8This way we split two jobs: the loop goes through the data, and the function runs the check on each page.
Complete the code
Fill in the blanks to create a function that receives a URL and prints it.
Spot the error
The code below has an indentation error. Fix it, then run it.
Check your understanding
When do the instructions inside a function run?
What's the main difference between print and return?
Summary
A function lets you put a set of instructions under one name and reuse them.
We use def to define a function, but a function only runs when it's called.
A parameter is the name a function receives data through, and an argument is the actual value we send when we call it.
print shows the result, while return sends back a value you can store or use in another part of the code.
When we combine functions with loops and conditions, we start building a real workflow instead of writing separate, repeated instructions.
Lesson 5 quiz
Question 1
Predict the output before you run the code.
Question 2
Write a function that returns a result
Write a function called page_result that receives status. If it's 200, it returns OK; otherwise it returns Check. Then print the result of calling it with 404.
Question 3
A function inside a loop
You have a list of URLs. Create a function called show_url that receives one URL and prints it, then use a for loop to run the function on every URL in the list.
You still have exercises left
0/8Exercises left: 8 of 8. The lesson counts as complete once you solve them all, and you can come back to them any time.
Well done, you reached the end of the lesson.