In this tutorial, we will learn all about functions in Python.
Table Of Contents
What is a Function in Python?
A function is a group of statements, coupled together to performs a certain task. Every function has a name associated with it, and whenever we call this function by its name, it executes all the statements inside it. Let’s understand it with an example,
Why do we need functions in Python?
Suppose we are writing an application, in which we need to print student names. If its one time operation then we can print it directly,
print('Mark') # statement 1 print('Shaun') # statement 2 print('Rose') # statement 3 print('Ritika') # statement 4
But what if our code is big and broken into multiple files, and we want to print names of these students at 10 places. Then the basic solution, is we can copy these lines at 10 places. But that is not an effcient solution, because of code duplication and manageable issues. Suppose in future we want to print an addition student’s name, then we need to change the code at 10 different places. That will waste the time, and increases the chances of error. So, what is the solution? To avoid these kind of issues, we need functions in Python.
To avoid this kind of solution, we can create a function with these statements, and call this function from 10 different places.
It avoids the code duplication, and makes code more manageable. Like, if we need to print an extra student name, then we can add that in the function defination only, and it will be done. So, change will be required at one place only.
So, with the help of functions, we can break the large tasks into smallet subtasks, and code becomes more manageable.
How to define a function in Python
Each function has a name, which we provide while defining the function. There are certain rules, that we need to follow while choosing the function name. These rules are,
- Function name should not be similar to any of Python’s keywords.
- No spaces are allowed in function names.
- The first character must be a letter i.e. either A-Z or a-z or the “_” character, and nothing else.
- After the first character, we can eithjer use any letter i.e. A-Z or a-z or an underscore “_” or any number i.e. 0-9.
- Function names are case sensitive. For example “getData()” and “GetData()” are different function names.
Syntax of function defination
def functionName(): print('Mark') # statement 1 print('Shaun') # statement 2 print('Rose') # statement 3 print('Ritika') # statement 4
To call this function we can directly call the function name, along with the brackets,
Mark Shaun Rose Ritika
When we call a function, it executes all the statements inside the function. When the function is completed, then the control comes back to the line from here function was called.
Python function arguments and return value
A function can also accepts arguments. These arguments can be used inside the function to customize the behaviour of a function. Also, function can return a value when it completes the execution of statements.This returned value reaches the point from where function was called. A function defination with arguments, and return value will be like this,
def functionName(argument1, argument2, argument3) statement 1 statement 2 statement 3 return value
We can call this function like this,
result = functionName(data1, data2, data3)
Let’s see an example, where we will create a function to that accepts three arguments, and returns the larges number from these three numbers,
""" Get largest number from the three numbers """ def get_largest_number(num1, num2, num3): if num1 > num2 and num1 > num3 : return num1 elif num2 > num3 : return num2 else : return num3 # call the function to get the largest number largestNumber = get_largest_number(34, 23, 30) print(largestNumber)
Here we called the function
get_largest_number(), and passed three numbers in it while calling. The function
get_largest_number() executed the statements in it, and returned the max value out of the three given values.
In the upcoming articles, we will learn more about Python Functions.
Thanks for reading.