# 05 - Functions **Notebook:** `05-functions.ipynb` ## Video <iframe width="720" height="406" src="https://www.youtube.com/embed/SK3VKiz6QKw?si=SxPWsMWq6E1cZ7qt" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> --- ## What is a Function? I apologize because I've used the word "function" already and hopefully you heard it in a kind of non-technical way. But now we're going to get into it properly. A [**function**](/glossary/coding-basics-py/function) is going to be a piece of **reusable code** that you can apply to something. ```python def my_function(some_value): # do something return result ``` Let's break this down: - `def` — we create a function by **defining** it - `my_function` — the name we choose for our function - `(some_value)` — in brackets goes whatever the input is (the [parameters](/glossary/coding-basics-py/parameter) or arguments—stuff we put into the function) - `:` — a colon - Indented code — what the function accomplishes - `return result` — we might [return](/glossary/coding-basics-py/return) a result, passing something out of the function Think of it as: **input** → function does something → **output** Functions don't always have to have output, but it's a very common pattern for something to go in and then something to come out. --- ## Example: A Describe Function ```python def describe(poem_title, bird): return f"{poem_title} features a {bird}" ``` In this one, we're putting in **two inputs**: the poem's title and then the bird. And what we're returning is one of those template strings (f-strings). ### Calling the Function ```python sentence = describe("The Raven", "raven") print(sentence) # The Raven features a raven ``` What we're doing is: 1. Fire this function: `describe("The Raven", "raven")` 2. Store the value in `sentence` 3. Now that string is in `sentence` and we can print it ### Shorthand You could also do this in one line: ```python print(describe("The Raven", "raven")) ``` Instead of storing the output in `sentence` and then printing it, we're putting the function call right inside `print()`. Since we know the function returns a string, that's going to work with the print function. This is kind of like in math where you can do things in multiple steps or shove everything into your parentheses. --- ## Wait, Print is a Function Too! Here's where I mentioned I used the word "function" early on: **print itself is a function**. It's structured exactly like the functions we're making now: - A word (the name of the function) - Parentheses - Inside that goes the input We've been doing that the whole time! Firing off that print function and shoving stuff inside of it as input, and the output of the print function is stuff that we can see in the console. --- ## Functions in Loops Functions can do more complex things. And when we combine them with loops, that's when things get powerful: ```python poems = [ {"title": "The Raven", "bird": "raven"}, {"title": "Ode to a Nightingale", "bird": "nightingale"}, {"title": "The Windhover", "bird": "falcon"}, ] for p in poems: result = describe(p["title"], p["bird"]) print(result) ``` Output: ```text The Raven features a raven Ode to a Nightingale features a nightingale The Windhover features a falcon ``` --- ## The Building Blocks Are Complete Once we've done this, what we're going to go on to do is the project itself. We're going to be taking these poems and then sending them to OpenAI to get OpenAI to do something with them, and then we're going to create images. It's going to be fantastic. And honestly, **the logic doesn't get any harder than where we are right now**. If you take your time to understand what's going on in these first five notebooks, you understand all you need to know conceptually and logically to get the logical structure of everything we're going to do from now on. These are the basic building blocks. It will get more complex, and that complexity is sometimes intimidating, but I want to encourage you to understand that the logical moves are no more complicated than what we've encountered so far: - Values being stored in things - Functions where there's input and output - Loops where we're going through a list and doing the same things - Complex data types (objects/dictionaries) where there might be many properties of a thing Those are the basic building blocks, and everything is going to come out of that. --- ## Try It 1. Write a function that takes a poem title and returns something about it 2. Try calling your function with different inputs 3. Combine your function with a loop to process multiple poems 4. Think about what other reusable operations you might want to create --- ## Summary In this notebook, you learned: - A [**function**](/glossary/coding-basics-py/function) is reusable code with a name - Define functions with `def function_name(parameters):` - Functions can take **inputs** ([parameters](/glossary/coding-basics-py/parameter)) and [**return**](/glossary/coding-basics-py/return) outputs - `print()` is a function we've been using all along! - Combining **functions with loops** is powerful - You now have all the **building blocks** for the rest of the course Next up: **API calls** — talking to OpenAI!