Learn to Code via Tutorials on Repl.it!

← Back to all posts
Python Functions Tutorial!
CodingElf66

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:

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

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.

Example 2:

In the first line of the code, the function name is initialized. It is "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.

Output:

Example 2:

Same scenario here. It just runs the function.

Output:

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


Parameters


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

Example 1:

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.

Ouput:

Example 2

In this example, we will make a greeting function!

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.

Output:

Voters
kanosy88
SilentFoxy78
Gokubez1
RE-DJP
MARTINKELLY1
hsahiner
vikmokut
rickysong
yxnghamz
noebancel
Comments
hotnewtop
TomPilibi

je ne comprends rien du tout!

CodingElf66
DynamicSquid

Hey, with your other tutorial: https://replit.com/talk/learn/Python-Functions-Tutorial/141877

I would recommend combining this one with that one to make one mega tutorial on functions! (it would also be preferable if the tutorial was also in the description instead of being in the repl) Replit always likes big tutorials :)

CodingElf66

@DynamicSquid Sorry, I'm not done with the tutorial. I'm new to markdown, so I'm writing it, then slowly seeing how it works I will try to complete it

CodingElf66
DynamicSquid

@JeffreyChen13 No worries then! Also, if you want a guide to markdown, you can use this link :)

CodingElf66

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

Bookie0

Nice tutorial, but it's a bit short; it's just about functions. Perhaps add some more content? :)