Hello everybody! Today we are going to do a simple tutorial on how to create simple functions in python.
For this tutorial we will be using python 3, if you don’t have it installed take a look at this: how to install python in 5 minutes
Let’s start by defining a “function”. A function, a cording to this website, is a unit of code that is often defined by its role within a greater code structure. Specifically, a function contains a unit of code that works on various inputs, many of which are variables, and produces concrete results involving changes to variable values or actual operations based on the inputs.
You probably didn’t understand anything, so let’s see if we can explain it in another way.
Imagine a box, inside this box there is a bunch of code. We don’t exactly know what it is inside this box, and sometimes we don’t even care. All we know is what it needs to be put inside what we are going to get in return.
Our box is going to be called “sum”. We have no idea what is inside this box, all we know is that it requires 2 numbers and is going to return us the sum of them.
That is a function. Now we are going to learn how to make them in python and build some examples.
To create a function in python all you have to do is type this:
def name_of_the_function(): #some code here return
Here are some advantages of creating functions in python:
- code looks cleaner
- easy to find bugs
- avoid code repetition
- code can be reused in multiple projects
- giving your code to other people makes it a pleasure to read
Functions allow you to keep your code clean and organized. You don’t have to repeat your code any longer, just call your function and you are done. As your script become really big keeping everything in functions will allow you to debug much faster. For example, let’s say you are making a game and your character doesn’t move, you will go and check your
move_character() not your
And we are done. Pretty simple right? Time for examples!
Example 1 (average age)
Let’s say I want a function that gives me back (return) the average age of 2 people. We could write this code:
timmy_age = 10 sophie_age = 35 #function def average_age(age1, age2): new_age = (age1 + age2) / 2 return new_age print(average_age(timmy_age, sophie_age))
When you run this code you will see that it returns the average age of the 2 given people. So now every time you need to do this calculation in your code you don’t have to copy everything again, just call your function and you are done.
Example 2 (autonomous car)
Let’s take a look to another example. Say we are making a smart car controller and we want to add an option so that the user can choose between “miles per hour” or “kilometers per hour”. In order to do that we must create a function that can convert from mph to km/h.
Remember: a good way to start is just to create the function with a good name and then fill it.
def convert_mph_to_kph(): pass
Great, we have already done 50% of the job. Now we must do some research and find of how we can actually do the math. Just do some google search and find this:
Time to fill our function:
def convert_mph_to_kph(miles): return miles * 1.61
And that’s how you create a function in python. It’s really simple. If you need more examples just leave us a comment and We will add it. Hope this had helped you. Don’t forget to subscribe for more cool tutorials and examples.