Python Functions: def, Parameters and return

About 35 minutes

What you need before this lesson
  • Variables
  • Strings and basic data types
  • Lists
  • Dictionaries
  • Conditions
  • for loops
  • while loops
  • break
  • continue
  • set
What this lesson adds
  • 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

The rule
  • 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.
Example
Expected output
Hello
Why that output?
  1. 1Python reads def say_hello() and learns there's a function called say_hello.
  2. 2The print line inside it doesn't run at the moment the function is defined.
  3. 3Then Python reaches say_hello().
  4. 4This is where we call the function.
  5. 5Python goes into the function and runs print("Hello").
  6. 6That's why Hello appears in the output.

Defining a function doesn't run it

Example
Expected output
Function created
Why that output?
  1. 1say_hello was defined.
  2. 2But we didn't write say_hello() after the definition.
  3. 3So the instructions inside the function didn't run.
  4. 4The only line that ran was print("Function created").

Predict the output

Predict the output

Before you run the code, predict how many times the line Checking page will appear.

Why that output?
  1. 1The function was defined once.
  2. 2Then it was called twice.
  3. 3Each call runs the instructions inside the function from the start.
  4. 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

Try it: change it and run
Expected output
Hello Sarah
Hello James
Why that output?
  1. 1When we defined greet, we wrote name inside the brackets, so name is a parameter.
  2. 2In the first call we sent the value Sarah, so Sarah is an argument.
  3. 3Inside the function, name became Sarah, so Hello Sarah appeared.
  4. 4In the second call we sent James.
  5. 5name became James for that call only, so Hello James appeared.

Passing a URL to a function

An SEO example: try changing the URLs and running it
Expected output
Checking: https://example.com/
Checking: https://example.com/about
Why that output?
  1. 1The function is called show_url, and it receives a parameter called url.
  2. 2In the first call, url took the home page's value.
  3. 3Checking was printed, then the value of url.
  4. 4In the second call, url took the about page's value.
  5. 5The 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.

Example
Expected output
URL: https://example.com/
Status: 200
Why that output?
  1. 1The function receives url and status.
  2. 2The first value we sent went to url.
  3. 3The second value, 200, went to status.
  4. 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

Example
Expected output
200
Why that output?
  1. 1get_status was defined.
  2. 2When we called get_status(), Python went into the function.
  3. 3It reached return 200, so the function sent back the value 200.
  4. 4The value that came back was stored in the variable page_status.
  5. 5Then we printed page_status, and 200 appeared.

return with a parameter

Try it: change it and run
Expected output
True
False
Why that output?
  1. 1In the first call, status became 200.
  2. 2Python checked the condition status == 200, and the result was True.
  3. 3return sent back True, which was stored in result and then printed.
  4. 4In the second call, status became 404.
  5. 5The condition 404 == 200 is False.
  6. 6return sent back False, so False appeared.

A function that makes a simple decision

An SEO example: change the numbers and run it
Expected output
OK
Check page
Why that output?
  1. 1In the first call, the value 200 reached status.
  2. 2The condition status == 200 was met.
  3. 3return "OK" ran.
  4. 4In the second call, 404 arrived.
  5. 5The condition wasn't met, so Python moved on to else.
  6. 6return "Check page" ran.

Using a function inside a loop

An SEO example: lessons 4 and 5 together
Expected output
Checking: https://example.com/
Checking: https://example.com/about
Checking: https://example.com/contact
Why that output?
  1. 1We have a list with three URLs.
  2. 2We defined a function called check_url that receives one URL.
  3. 3The for loop started and took the first URL from the list.
  4. 4That URL was sent to check_url, which printed it.
  5. 5The loop came back, took the second URL and ran the same function.
  6. 6The same happened with the third URL.
  7. 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

Function + loop + condition in one workflow
Expected output
https://example.com/ -> OK
https://example.com/about -> OK
https://example.com/old-page -> Check status
Why that output?
  1. 1We have a list called pages, and every item in it is a dictionary with url and status.
  2. 2The function is called audit_page, and it receives one page.
  3. 3Inside the function, we check the value of status.
  4. 4If it's 200, we print the URL with OK.
  5. 5If it isn't 200, we print the URL with Check status.
  6. 6The for loop goes through every dictionary in pages.
  7. 7Each round, it sends the current page to audit_page.
  8. 8This way we split two jobs: the loop goes through the data, and the function runs the check on each page.

Complete the code

Complete the code

Fill in the blanks to create a function that receives a URL and prints it.

Spot the error

Fix the error

The code below has an indentation error. Fix it, then run it.

Check your understanding

Check your understanding

When do the instructions inside a function run?

Check your understanding

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

Note
The output stays hidden until you solve it. You can run the code, ask for a hint after your first try, and the full solution shows after two tries.

Question 1

Predict the output

Predict the output before you run the code.

Question 2

SEO challenge

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

SEO challenge

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/8

Exercises 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.