Functions…

Functions are cool. They really are. You know when you have a math or physics exam and you have to do the same procedure in your calculator for each problem again, and again? Isn’t it annoying or tedious? It really is. That happens too in programming. You don’t want your code to end up like a bunch of copy and paste stuff and for it to end up being huge. That’s why functions exists. Inside a function you write something that you know that you’ll be using or that you think that it will be more practical to use it if it’s stored in a certain piece of code. Later on, when you want to use that piece of code that you wrote in the function you can just simply call it by typing the function’s name, and setting it’s parameters. Here’s a nice example:Screenshot from 2015-10-28 18:20:48

Here’s 4 functions. The first one, inp, asks for a certain number of integers, adds them to a list, and returns that list. The second one, total, returns the addiction of all the numbers inside the list created by the first function. The third one, avg, calculates the average of the numbers inside the list. Finally, the last one, sd, calculates the standard deviation .

As you can see calling a function is really simple. You just write the name of the function and it’s parameters. Simple.

CC BY 4.0 Masteries 11 and 12 – Creating Python functions and calling Python functions by drag04 is licensed under a Creative Commons Attribution 4.0 International License.