In this and the previous two courses, you may have noticed that there are occasionally steps that we would do over and over. Such as opening, reading and then closing the file. Or defining a few variables then create a prompt using an f-string, using those variables, and then getting the large language model to respond using that prompt. We've seen how for loops can help you with doing the same thing a few times in a row. But for loops, expect you to do that operation multiple times, one right after another in a row, rather than, say, loading read one file and then maybe do some other things and then read a different file as opposed to the more rigid structure of a for loop of loading one file right after another. But it turns out Python has another very useful way of creating reusable blocks of code called functions. Functions are very important concepts in programming, and all modern programming languages give you a way to define and use functions. You've already encountered, and used functions in many places in this and in the previous couple of courses. And in this lesson, you'll see how to create your own functions and you'll see how they can help you avoid repeating code and avoid having to type the same thing over and over. Functions are important. Let's go see how to create them. Functions are reusable set of commands to help you perform a specific task. You've seen the print function or the Len function. Given a list, returns the length or prints LLM response. Now, let's take a look at why you might want to define your own function. Previously we had written chunks of code like this that would open the file, read it, and print out the text loaded from this file. If I had to do this with a different city like Paris, I would have to do this and replace this with Paris. what if I don't want to do this over and over. I'm going to define the function print journal as follows. We'll go through later what every single line of this does. But now you can run print journal on say, Sydney dot text. And have it load and print out the file Sydney dot text. And then if you wanted Paris dot text, you can do this and this prints out Paris dot text. Let's take a look now at what every line of code here does. Here's the code. Def is a special command that tells Python you want to define a new function. Print journal is the name of the function. You then have parentheses, and within the parentheses is the variable named file. File is called a parameter of this function. And what this means is this when you use or when you call this function, this function is expecting you to give it a file name. Then there's a colon followed by an indented block of code. And the function will then run these four lines of code, which are to set f equals open the file. journal equals F dot read. Close the file and then print the journal. So all four lines of code here will be run every time you call the print journal function. And this is just the same four lines of code that we've been using over and over. Now here we define the function to load and print out a journal. What if you want a function that loads a journal but doesn't print it out right away? Let me define a different function. I'm going to put it down here so you can compare and contrast these two functions. First, instead of print journal I've added a live return journal. This no longer prints out the journal that was just read. What the return command does is it makes the read journal function take the value journal and give this back. If I call read journal Tokyo dot text, this function will return a value. And so I'm actually going to define a new variable journal Tokyo equals read Journal Tokyo. And if I run this then journal Tokyo will be set equal to whatever value's returned here by the function. I will now print Journal Tokyo. Then you get that article that we had previously. And now that you save this in the variable Journal Tokyo, we can also do things like maybe print the length of this article. And it turns out this article has 1430 characters. Just to explain in detail what we just saw. Everything is the same as before, except we got rid of the print statement And we have a new return statement. And the return statement takes the value of journal and gives it back. In this case, the value gives back, or the it value returns as the value of whatever is stored at the variable journal. Now let's look at one more example. Previously you saw that Python let's you do calculation like converting from Fahrenheit to Celsius. And you use code like this 72°F is 22.22°C. Now if you want to convert another temperature you can write this code again. And so 68°F is 20°C. How about 76 degrees. Oh that is 24.44 degrees. And so if you want to convert a lot of temperatures, you might end up writing this code over and over. Here is how you can define a function to do conversion and define Fahrenheit to Celsius. And I'm going to put a parameter. This would be a number which is a temperature in Fahrenheit. You have to have a colon and is to enter into block of code. And so to convert to Celsius this is the formula. And then we can just have this print statement over here. And if I now run this block of code it now defines the Fahrenheit to Celsius function. And now if I call this Fahrenheit to Celsius on 71, it converts it. Or if I ask it to convert what is 70 degrees? Or if I want to convert 212°F to Celsius, that turns out to be 100 degrees. So you notice that without functions, if you wanted to convert different temperatures, we would write very similar code over and over, but with functions, you can define the function once and then use the function over and over. And so the code on the right is much shorter. In the previous example this function didn't return any value, it just printed the result to the screen. To save the result from a temperature conversion, you can use a return statement. So rather than having a print statement, we can instead have it returned celsius. Let me just comment that out. And now, when I run this I am defining a new Fahrenheit to Celsius function. Now, if I were to say Fahrenheit equals 45 Celsius equals Fahrenheit to Celsius Fahrenheit, and then let's print the temperature in Celsius, you get 7.222. And you can do other things with Celsius like ask what is the type of Celsius, which in this case is a floating point number. Here I've written functions to convert from Fahrenheit to Celsius. Can you modify this code to convert from Celsius to Fahrenheit, or to convert from feet to meters? I hope you have fun with functions. After all, you can't spell function without fun. And in the next lesson, we'll take everything you've seen so far and tie it all together. And we'll go planned the perfect vacation around the world using text and CSV files. I'll see you in the next lesson.