Skip to content
Sign upLog in
Python Functions Tutorial!
Profile icon

Python Functions Tutorial!

I already posted my first version of the Python Functions Tutorial, but this one will go a little deeper and detailed into functions.

Why You Should Learn Functions

There are plenty of reasons why you should learn functions. First of all, functions are pretty easy to work with, and it shortens the code. For example, if you want to get the sum of five random numbers, and you want to do it three times? Well, you can just write the function and put it in a for loop! Another reason why you should use functions is that it makes your code look more organized, step-by-step, instead of just directing writing the code all at once.

Other sources

If you find this tutorial confusing or still do not understand the concepts, I suggest you look at the links given below.

Now, let's get to the main part!

Making Functions

** Example 1: **

def func(): print("Hello, world!")

In the first line, we make a name for this function. It is called "func". They can be named any valid variable name.

def func():

In the second line of the code, we write what is done inside the function. We print "Hello, world!", so when we call the function, it basically runs the code that is inside the function. Note that any code inside the function must be indented.

print("Hello, world!")

** Example 2: **

def addition_facts(): print("5 + 2 = 7") print("9 + 28 = 37")

In the first line of the code, the function name is initialized. It is "addition_fact".

def addition_fact():

In the second and third lines, we describe what is inside the function. Note that functions can have unlimited lines; it has no limits!

That is the first part of my tutorial on functions! Let's move on to the next part!

Calling Functions

Calling functions is quite easy. All it requires is one line of code!

** Example 1: **

For example, we'll repeat our functions from the last part. To call them, we just need to write their function name with a full set of parentheses "()". For example, with the function in Example 1 in Part 1, to call it, we just simply do:


This line of code calls the functions. In other terms, we basically run whatever is inside the function.


Hello, world!

** Example 2: **


Same scenario here. It just runs the function.


5 + 2 = 7 9 + 28 = 37

You just learned about calling functions! Let's move on!


Parameters are like variables inside the function, and just like variables, you have to define them! Let's do some examples!

** Example 1: **

def add(num1, num2): print(num1 + num2)

In this first example, we have two parameters; num1 and num2. They can be named any valid variable name, too, and there is no limit to how many parameters they are. For now, the parameters are unknown, until the user calls the function and defines the parameters. Let's think about this function for now. Suppose we say that num1 is 4, and num2 is 8. Therefore, this function would return 12. Let's test it out!

To call a function with parameters, we just call the function, then inside the parentheses, we put the values of the parameters.


As expected, we get 12.



** Example 2 **

In this example, we will make a greeting function!

def greeting(name): print("Hello, " + name + "!")

When we call the function, we will give in a value for the name! Then, this function will return "Hello, (name value)!"

Let's say my name is Andrew.



Hello, Andrew!
You are viewing a single comment. View All
Profile icon

It's ok, in the bottom, it says "Style your post with Markdown!", I got it :)