If you haven’t read the previous tutorial, go and read here. Today we’re gonna see python functions and indent.
Welcome to Python tutorial for beginners-2 and let’s get started.
We already knew why do we need variables and how to store values and reuse anywhere inside the program.
Now, you’ve got an idea of how to reuse the values but think how to reuse the lines of code? Sounds strange?
Let’s see with an example: What if you want to add two numbers “a” and “b” 100 times?
That’s where function comes, see the following code:
def add(a, b): print(a+b) def sub(a, b): print(a-b) add(3, 5) #Output : 8 add(4, 5) #Output : 9 add(10, 15) #Output : 25 sub(3, 5) #Output: -2 sub(4, 5) #Output: -1 sub(10, 15) #Output: -5
In the above code, you might have seen that I used add() thrice and sub() thrice.
Which means, we reused the add and sub function three times. We can reuse the lines inside add and sub method anywhere! (Note: So that we don’t need to write multiple lines of code each time we need to add two numbers. Just call, add(5, 4) to print the addition of 5 and 4 => 9.)
I hope now you’ve got a clear understanding of why do we need functions!
Now, see how to define functions in Python.
How to define functions in Python?
A function starts with the keyword def and then function_name and arguments within the brackets “()”. The function should end with “:“. All the statements inside the functions should be indented with four spaces. (See the below example)
def function_name(argument1, argument2): statement1 statement2 statement2 statement3
(Note: In some IDE’s indent will be only two spaces)
Indent? Sounds weird?
What is an indent in Python?
Indent is very important in Python to write explicit code. Python understands the block only with the indent, and if you write the code without proper indentation, you will end up seeing “Indentation error.”
Why indent inside the function? Because Python understands that all the indented code are the part of the function! Now, go to the previous example and see, we defined two functions add and sub, and see one function starts without indent and statements inside the function are indented. The next function starts without the indent, and the statements inside the function are indented! It’ll understand that the statements belong to that function. That’s how the indentation works in Python!
(Note: If you’ve worked with C, C++ and Java, there are curly braces to understand the code blocks, but in Python, there are no curly braces instead it follows indentation only.)
Will learn about indent further when we see conditional statements!
You might have a question ” Hey, you said “you can reuse the code using functions? But you didn’t tell how?” Now, we’re going into the next topic in functions, “FUNCTION CALL.”
What is mean by function call?
Consider the same previous example; we’ve defined two methods add and sub. Inside that, we wrote statements to find the sum and difference between the two numbers. And we resued the methods three times again, that is what function call is. Function call is just like calling the variable. We can call as many times we want.
Your Task: Define a function to multiply and divide two numbers!
Do you remember the term arguments that I told above when I define functions?
Arguments are the value that we pass to a function. We can use the arguments within the python functions. We’ll dive deeper into functions and arguments in the next tutorial!
See you tomorrow with more about functions!
Share and learn together! ❤️
Also, see: 30DaysOfCode Problems