Python Functions Tutorial!
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.
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!
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.
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.
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".
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 is quite easy. All it requires is one line of code!
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.
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!
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.
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.
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 :)