This tutorial will help the python beginners to learn functions in python.
A function is a block of code that contains one or more Python statements and used for performing a specific tasks.
As I mentioned above, a function is a block of code that performs a specific task. Lets discuss what we can achieve in Python by using functions in our code:
1. Code re-usability:
Lets say we are writing an application in Python where we need to perform a specific task in several places of our code, assume that we need to write 10 lines of code to do that specific task. It would be better to write those 10 lines of code in a function and just call the function wherever needed, because writing those 10 lines every time you perform that task is tedious, it would make your code lengthy, less-readable and increase the chances of human errors.
2. Improves Readability:
By using functions for frequent tasks you make your code structured and readable. It would be easier for anyone to look at the code and be able to understand the flow and purpose of the code.
3. Avoid redundancy:
When you no longer repeat the same lines of code throughout the code and use functions in places of those, you actually avoiding the redundancy that you may have created by not using functions.
def function_name(function_parameters): function_body # Set of Python statements return # optional return statement
when function doesn't return anything
# when function returns something # variable is to store the returned value variable = function_name(parameters)
Here we have a function add() that adds two numbers passed to it as parameters. Later after function declaration we are calling the function twice in our program to perform the addition.
def add(num1, num2): return num1 + num2 sum1 = add(100, 200) sum2 = add(8, 9) print(sum1) print(sum2)
There are two types of functions in Python:
Built-in functions: These functions are predefined in Python and we need not to declare these functions before calling them. We can freely invoke them as and when needed.
User defined functions: The functions which we create in our code are user-defined functions. The add() function that we have created in above examples is a user-defined function.
We have alredy learn about the functions and its types. Now, it is important to learn how to declare a call a function. Now we will have to see how can we use the default arguments.
By using default arguments we can avoid the errors that may arise while calling a function without passing all the parameters. Lets take an example to understand this:
In this example we have provided the default argument for the second parameter, this default argument would be used when we do not provide the second parameter while calling this function.
# default argument for second parameter def add(num1, num2=1): return num1 + num2 sum1 = add(100, 200) sum2 = add(8) # used default argument for second param sum3 = add(100) # used default argument for second param print(sum1) print(sum2) print(sum3)
300 9 101
A function is said to be a recursive if it calls itself. For example, lets say we have a function abc() and in the body of abc() there is a call to the abc().
In this example we are defining a user-defined function factorial(). This function finds the factorial of a number by calling itself repeatedly until the base case(We will discuss more about base case later, after this example) is reached.
# Example of recursion in Python to # find the factorial of a given number def factorial(num): """This function calls itself to find the factorial of a number""" if num == 1: return 1 else: return (num * factorial(num - 1)) num = 5 print("Factorial of", num, "is: ", factorial(num))
Factorial of 5 is: 120
Explaination: We use recursion to break a big problem in small problems and those small problems into further smaller problems and so on. At the end the solutions of all the smaller subproblems are collectively helps in finding the solution of the big main problem.
Pros of Recursion Recursion makes our program:
Cons of Recursion
If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
When going through coding examples, it's quite common to have doubts and errors.
If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at email@example.com. We'll help you or point you in the direction where you can find a solution to your problem.
You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.
If you want to