While programming we often perform the same task repeatedly, such as performing the addition of numbers or printing the same statement with different inputs. These are general examples, but for these would you rather write the same code 10 times or just once?
That’s the purpose of functions, they’re pieces of code only defined once for a particular task and come with reusable functionality so that users can call them whenever they want to perform a task. This makes your program understandable and debugging easier.
Sometimes functions require some input and sometimes they may or may not return some value, but all of these vary from function to function and the task assigned to that specific function. In Python, functions are essential parts that may be user-defined or built-in. In this article, we’ll discuss functions in Python, types of functions, how to define them along examples for your better understanding.
Functions in Python
There are four types into which functions in Python are categorized and each one is useful in its own way, here are these categories:
- User-defined Functions: These are the functions that users create for their own task, and their complexity can vary depending on the task assigned to them.
- Built-in Functions: These are already defined in python that helps users to easily perform the task without creating a function. Such functions are print(), min(), max() and more.
- Recursive Functions: Functions that have the ability to call themselves and executing until a result is reached are called recursive functions.
- Lambda Functions: those functions that are not declared with the keyword “def” are termed as anonymous or Lambda Functions.
Let’s understand each of these with examples to help you understand the concept of functions better.
Those functions in Python that users defined on their own to perform specific tasks in their program are User Defined Functions. These functions reduce the complexity of a program as a particular chunk of code is being called and reused to perform specific tasks with different inputs.
Hence, the program becomes understandable and less complex with easier debugging. This is also useful as sometimes developers are working on larger projects, so the complexity of it is divided into various functions and hence such projects become easy to debug later as you just have to go and find the error in a particular function and not the whole code.
define nameOfFunction(arg1, arg2, .., argN): statement[s] ....
- nameOfFunction: name you define of the function that can be anything.
- arg1 , arg2, argN: number of arguments that can be single, multiple or none.
- Statement[s]: the line of codes are executed and an action is performed after the function is called.
Here in our Linux system, we have created a file on our desktop with the name “python.py” along with the code saved in it and we’ll run that file using the command provided below in which python3.9 is the version of python being used:
The simplest example of a user-defined function would be a simple printing statement that prints when the function is called using the constructor “myFunction()”.
OutputHello, It's a function I defined
In this example, we defined an additional function that takes two numbers as arguments and adds them, the result is returned whenever the function is called.
Now if we run the file, it’ll display the following output which shows how functions work.
OutputSum is 14
There are 68 built-in functions available to be used in Python to make the task easy for developers. These functions are already defined so that the user doesn’t have to define a function for the simplest task. Let’s see a few of these built-in functions in Python.
all() Python Function:
all() function in python takes in a single iterable argument that could be a tuple, list or dictionary and returns a boolean value. True is returned when the given iterable elements are true, else false is returned.
OutputTrue True False False False
bin() Python Function:
bin() function takes in a single integer parameter and returns the binary value of that particular integer. In case the parameter passed is not an integer, the function has to implement _index_() method to return an integer.
OutputThe binary value of 10 is: 0b1010
bool() Python Function:
bool() functions in Python generally take one parameter value and return a boolean value. If no parameter is passed or False, None, numeric (0.0, 0) type of parameters are passed, False boolean value is returned, otherwise true is returned.
OutputFalse True True False False True
len() Python Function:
len() function in Python either takes in a string, list, tuple, or a collection like a dictionary and returns the length of them in integers.
Output length is 0 [5, 5, 3] length is 3 (5, 2, 3) length is 3 Length of range(1, 5) is 4
E. reversed() Python Function:
reversed() function takes in a single parameter and returns the output as a reversed iterator.
OutputFactorial of 5 is 120
Those anonymous functions where we don’t use the “def” keyword and directory define them are called lambda functions in Python. Instead of “def” these functions use lambda keywords to be defined. These are mostly used when the user needs to work on smaller codes that don’t require various lines of codes or proper functions.
lambda argument: expression
Here, the number of arguments can be multiple but only one expression is evaluated and returned.
Functions are an essential part of every programming language that helps us reduce the complexity of a program by dividing it into various reusable chunks of code. Python has four different types of functions which we discussed in this article. Users can define their own functions or use the built-in ones in Python.
Furthermore, some functions call themselves repeatedly until the result is reached. The article provided the syntax for each type of function along with examples for your better understanding of functions in Python.