Functions in Python are very easy to use, they do specific actions with the value you call them. The function substitute a value into a block of code and the returns the calculation (or sometimes they do nothing). For example, you have probably used before the print function:

Print(“Hello world”)
This funtion is a built-in-function that takes an integer or a string (or a combination of them) and prints them for the user. There are a lot of built-in-funtions but we are not going to ge into all of them, the way to recognize a function is to see a word and then a value(or values) between parenthesis.
Name_function(value)
Name_function(value,value1,value2)

Calling a function

In order to work all functions need to be called. You can just write the name of the function with some values inside but that is just going to calculate whatever the function do without responding to the user. If you want to show the calculation to the user you can include the function in a print function or asign the value of the calculation to a variable:
superpower(2,3) #Gives you the value of x raised to the y
nothing happens
print (superpower(2,3))
8
value = superpower(2,3)
print (value)
Keep in mind that if you want that the function takes a value and return it (adquire the value) you need to put the word return inside the function. If you dont do this the function will do nothing and you wont b able to use it for more calculations. 

Creating a function

Now that you know how to call a function you can create one. The stepts are really simple. Fisrt lets see the body of a function:
def function_sum(x,y,z):
     return x+y+z
As you can see to create a function we need to tell Python that we want to make one so we write def, this way Python will know that you are creating a function and not calling it. The next step is to write the name, you can use whatever name you want but keep in mind that if you are in a big project you are going to use a lot of functions so use names easy to remember. I recommend you to name the function with something related to it like:
def root_square(x) #It calculates the root square of a number
def pyramid(x) #It prints a pyramid made of strings
def fibonacci(x) #It prints the value of n in the fibonacci sequence
The next ting to do is to write the parameters that you want the function to take, it can be anything as long as you use them inside the function. It works as a variable but they are not a variable, it just sustitute the values inside the function. Look at the first example and you can see that I used 3 parameters (x, y and z) and later on I used them inside the function to retunr the value of a sum of the 3 parameters. Lets see some examples of the function working:
print (function_sum(1,2,3))
6
print (function_sum(10,4,16))
30
Remember to use the return so that we can use it as the value of the calculation
function_sum(1,2,3) + 10  #6 + 10
nothing 
Remember that we need to print it or asing it to a value. Otherwise the calculation will be useless.
print (funtion_sum(1,2,3) + 10)
16

Notes

-While creating the function remeber to use the : at the end and then put the code inside the function with identation
-Remember that most of the time the function needs the return
-You can use as many parameters as you want
-While calling the function remember to give the value some use, print it or asign it to a variable


CC BY 4.0 Masteries 11 and 12 – Calling and creating functions by Jose Carlos Peñuelas Armenta is licensed under a Creative Commons Attribution 4.0 International License.