yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Functions | Intro to CS - Python | Khan Academy


4m read
·Nov 10, 2024

You're writing a program, and it's starting to get pretty long. Plus, you're duplicating a lot of code because you need to perform the same task at several different points in the program. How can we better keep this code organized and reduce the repetition?

We need functions. A function is a named block of code that performs a specific task. There's really not too much special here; it's just normal lines of code that we've packaged up and attached a name to. Like a variable gives a name to a value, a function gives a name to a set of instructions.

We define the function once, and then that name lets us execute the task over and over again without having to rewrite all those lines of code every time. Typically, a function takes in some number of input values, it operates on them in some way, and then it returns out an output value. The input values allow us to generalize the function's task so it can round any number to any number of decimal points or drink a potion of any color. Each time we call the function, we can pass in different input values.

The function call expression then evaluates down to whatever output value the function returns. To define a new function, we use a function definition. A function definition starts with a keyword def, which stands for "define." Then, we put the name of our function followed by a set of parentheses, and then, like most things in Python, we end it with a colon. We call this whole line the function header.

Now we're just missing the input values. We need to specify how many input values the function takes and give each of them a name. We then put those names inside the parentheses. These are called the parameters to our function. Parameters are effectively just variables, but those variables are only accessible within the function body.

The function body is all the lines of code that perform the function's task. These can include any normal lines of code, like variable assignments, conditionals, or loops. To tell the computer that these lines of code belong to the function body, we indent them one tab over from the function header. Typically, at the end of the function body, we specify the output value that the function returns. To do this, we use the keyword return. Whatever value we put after it will be the output value.

Our function definition is now complete, but a function definition just defines how to perform a task. To actually perform that task, we still need to call the function because the computer needs to learn the task before it can do the task. A function definition needs to appear in the program before any of its function calls.

To call a function, we use its name and a set of parentheses. Inside the parentheses, we pass in any input values, which we call arguments. To execute a function call, the computer jumps back to the function definition. It plugs in the input values by binding the arguments to the corresponding parameters. This is effectively the same as a variable assignment, so we're assigning the value pink to the variable color.

Then the computer executes the function body. When it reaches the return statement, it'll exit out of the function body and jump back to the line of code where the function call occurred. The function returns out whatever value appeared after that return. So, this function call evaluates down to the value four, and then we can call the function again and again with different arguments.

Let's take another look at our original program. I can see that we're building some kind of Adventure game, and the player has some number of potion items in their inventory. Then within the game, the player can choose to drink a potion at any point in time. Instead of duplicating this potion conditional all over the program, it's much easier if we refactor it into a function.

We've already written this function definition; it's called drink_potion. It takes in the color of the potion as a parameter, and it returns out the number of affected Health points. This means we can replace all these potion conditionals with calls to the drink_potion function, and we'll pass in as an argument the potion that the user selected.

The more we break down our programs into functions, the easier they are to read and maintain. For example, if I want to add a blue potion, I only need to modify this one function definition. Before, I would have had to find every duplicated potion conditional in the entire program and update all of them to include a blue potion.

But now, every time I drink a potion, I just call this function. So if I just make a quick change here, the entire game now supports blue potions. Plus, now that I've built this function, I don't ever have to think about how potions work again. As I continue building out my game, anytime I need to deal with potions, I can just call the drink_potion function without having to really understand what the code does.

In this way, functions let us solve the problem once and move on, which is super convenient when solving big problems.

More Articles

View All
Lecture 5 - Competition is for Losers (Peter Thiel)
All right, all right, good afternoon. Uh, today’s speaker is Peter Thiel. Peter was the founder of PayPal, Palantir, and Founders Fund, and has invested in, uh, most of the tech companies in Silicon Valley. And he’s going to talk about strategy and compet…
Ionic bonds and Coulombs law
I bonds are the bonds that hold together ionic compounds. So basically, it’s what holds together cations and anions. An example of a compound that’s held together with ionic bonds is sodium chloride, also known as table salt. So here, we have a close-up …
Gordon Tries Fermented Fish | Gordon Ramsay: Uncharted
I’ve still got lots to learn, so I’m off to try a traditional Christmas dish that I hear tastes much better than it smells. Now trust me, I want to get the best of Christopher, and I’m up here to meet two guys who make this amazing delicacy that can only …
Arteries vs. veins-what's the difference? | Circulatory system physiology | NCLEX-RN | Khan Academy
Let’s talk a little bit about arteries and veins and the roles they play in the circulatory system. So, I want you to pause this video and first think to yourself, do you have a sense of what arteries and veins are? Well, one idea behind arteries and vein…
The Illusion of Self
Recently, I was scrolling through old pictures, and I found the picture from when I was a little kid. I took the picture and held it up to my face in front of a mirror, and I realized, wow, I look nothing like that kid in the picture. We don’t have the sa…
Thoughtful Disagreement is the Key to an Innovative and Harmonious Society
The art of thoughtful disagreement is the basis of a very, uh, innovative and also harmonious society. If you want to have an innovative, harmonious society, you have to have the art of thoughtful disagreement. The mediator is a very, uh, important role t…