Python: age = input("Type in your age here: ") print(age)

Python: def randomFunction(): print("Something random!") print("This is a random statement.") randomFunction() print("This is another random statement.")

Spoiler: Reveal answer here! This is the order:



This is a random statement.

Something random!

This is another random statement



Is this what you expected? If you're new to the concept of functions, you might've expected "Something random!" to be the first printed statement, and that makes sense right? I mean, that print statement is the first one in the code. But that doesn't necessarily mean that it will be executed first.

randomFunction()

Well, sometimes when you code, you have a lot of code that is related to each other. For example, let's say you're making a game, and you have a lot of code that just simply prints out a lot of instructions on how to play the game. You could organize all of this code into a function, and then call the function something like: "printInstructionsOnHowToPlayTheGame". How's that for a function name?Let me use a real-world example real quick. Let's say you want to explain to your mother how to change her Facebook password. Since she has never done it before, you have to explain each and every step along the way. You first tell her to switch on the computer. And then you tell her to open up her web browser (Chrome, Firefox, Edge, etc.) and tell her to type in her account username and password, and so on and so on. Eventually, you have explained the entire process to her. If she's bad with computers, she might have trouble remembering everything you told her, so you asked her to write down everything you tell her. You have now defined a function. You have outlined the steps for how to do a certain thing (like changing your Facebook password).If you, for some reason, need for your mom to change her Facebook password in the future, you can simply tell her to do so, and she will have the instructions ready because she wrote them down that one time you explained how to do it. So, instead of explaining everything again, you can just tell her to change her Facebook password, and tell her to read what she wrote down on the piece of paper if she can't remember how to do it. In this case, you arethe function you defined earlier. Imagine how tiring it would be if you had to explain everything over and over again every time you wanted your mom to change her Facebook password, assuming that this is something you had to do often. Let's assume she gets hacked constantly, so you need to tell her to change her password frequently. Man, your mom needs to get her act together! Anyways.This is one of the reasons why functions are really useful, and used everywhere in a program. When you have to do something over and over again in your program, like asking the user of your program for some input, or printing out the same kinds of instructions over and over, it can be useful to justa function, so that you can justthat function every time you want to do the same thing over again.Let me try and explain this from another angle. When you're coding something, and the program gets a little big, yourwill be all over the place. Ooh, control flow, that's an interesting word. Theis basically just the order in which your code is being executed in. Let's take this example program:is pretty obvious. First the program asks the user to type in their age, and then you print out the age variable's value. But, what about this program?The reason why it wasn't executed first in this scenario is because the first two lines in our code was actually a. Function definitions aren't actually executed until they are. Imagine if your mother had to constantly keep changing her Facebook password 24 hours a day just because she had the steps written down for how to do so. That would suck, how could she possibly survive? No, she only needs to change her Facebook password when someone tells her to (let's pretend she can't do it on her own accord).In the same way, function definitions aren't executed because they are just that: they are definitions for how to do something. If we wanted to use our function, we need toit in our code.a function = telling the computer to execute the function you defined. If you take a look at the code I posted, you'll see this line:right after the "This is a random statement." print function. By the way, did you know that theinstruction is actually a function? Every time you are printing something out, you are actuallythefunction to do it for you.Do you see how the execution of the program can jump back and forth because of calling functions?