Last Updated on November 29, 2022 by Prepbytes
In this article, we will study how python functions work right from defining them to calling them. We will study in detail the types of functions in python and how to call a function in python. Applications and Uses of functions are a part of this article on python functions.
What is a Python Function?
Functions in Python are defined to increase the reusability of code and divide the code into modular tasks instead of rewriting the same part of code over and over again. Functions are based on DRY (Don’t Repeat Yourself) where we try to minimize the redundancy in our code and reuse the already defined components of our code.
There are various types of functions in Python. Generally, a function that a user defines has a def ‘keyword’ which is used to indicate that a function is created with a suitable or user-provided function name and inside the function is a set of instructions or statements in an indented manner. If there is a need, value can be returned from the called function.
Argument in Python Function
Python functions have the ability to pass arguments in the function call which makes it easy to preserve values from being reinitialized and use them repetitively. As we are aware of how to call a function, let us look at a code where we will pass a value on calling a value and print that value as a statement embedded within the function.
Example of Argument in Python Function:
def fun(y): #Function Definition print("Hello PrepBytes In", y) year = 2022 fun(year) #Function Calling
Output: Hello PrepBytes In 2022
As mentioned in above python function, we passed year as function argument and a copy was created with the name, y, that was printed as output with the string we intend to print. Instead of making an object and passing, we can actually pass a value, which will be turned into an object according to the named parameters we have written in the function definition.
Return in Python Function
Python Function can return a value after performing certain computations in the body of function. The returned value is sent back to the actual function wherein the function was called to store or print the returned value.
Example of Return in Python Funtion
def fun(h): #Function Definition return h * 60 hours = 4 print(fun(hours)) #Function Calling
Output: 240
In the above code snippet, we are passing the number of hours as our argument which is being multiplied by 60 and returned back to the main function and printed using the print function.
Pass in Python Function
Sole purpose of pass is to avoid getting an error when the function is left empty as blank functions throw an error but pass keyword in a user defined function covers it.
Example of Pass in Python Function:
def fun(val): pass fun(21)
Arbitrary Argument
A sequence of arguments is converted into tuple and defined with asterisk + tuple name in the function definition, Keyword Argument
def fun(*subjects): print("Favorite subject is " + subjects[2]) fun("OS", "CN", "DSA")
Output: Favorite subject is DSA
Keyword Argument
In the calling function, a keyword is assigned to the value. Later in the called function, keyword is used to access the value of the passed argument.
def fun(sub3,sub1,sub2): print("Favorite subject is " + sub1) fun(sub1="OS", sub2="CN", sub3="DSA")
Output: Favorite subject is OS
Arbitrary Keywod Argument
While passing the argument, a keyword is assigned to all the values but in a defined function, a single dictionary based parameter is set up where all the keyword and assigned values can be accessed using key-value based technique.
** is attached with the dictionary based parameter that behaves as a dictionary. To access the keywords, they are mapped as string-based keys so to access them, they must be placed within quotes.
def fun(**subjects): print("Favorite subject is " + subjects["sub1"]) fun(sub1="OS", sub2="CN", sub3="DSA")
Output: Favorite subject is OS
Now that we have studied in-depth about functions, we have a clear indea on how to call a function in python and the protocols we must follow to work in functional programming.
Types of Functions in Python
There are four different types of functions in python mentioned as follows:-
- Built-in Python Function
- Lambda Python Function
- Recursive Python Function
- User-Defined Python Function
Now we will be discussing the types of functions in python one by one with detailed example.
Built-in Python Function
These are the functions that is included packaged by the manufacturer. All the pre-defined functions are part of built-in functions. Some of the examples of Built-in Python Functions are:-
- print()- to print object values
- min()- to print the least value in a sequence
- max() – to print the greatest value in a sequence
- type() – to get the class of the object
- sorted() – used to sort the sequence.
User Defined Function
Functions created by a user to perform a certain task fall under the category of user-defined function. To define a function. ‘def’ keyword is used followed by a whitespace, function name and colon (:). Set of statements are written inside the function that needs to be executed on calling.
The function can be called from the main part of the function.
Example of User Defined Function:
def fun(): #Function Definition print("Hello PrepBytes") fun() #Function Calling
Recursive Python Function
A Python Function that calls itself until a certain criteria or condition is fulfilled. The condition is known as base condition and values. Recursive function makes the code look elegant.
Recursive function works under the hood as a stack call where the function one above another is called until we return back or pop the stack to bind up the operation.
def fact(n): if n == 1: return 1 return n * fact(n-1) print(fact(5))
Output: 120
Lambda Function
These functions can be thought of as abstract functions which work in the same manner as an actual function but do not have a name and defined in a same manner as they have varied syntax.
lambda x : x+3 is how we can implement adding 3 to a number using lambda expression
Using a function, we can do this by
def fun(x): return x+3 fun(2)
Why to Uses Python Functions
As we are aware of the working of a python function, how to call a function in python and its types, let’s look at how well they are crafted for real world application.
Functions enhance code reusability and code readability.
Functions better the memory management as creation of redundant data is neglected.
Code becomes more structured and its testing can be performed easily on modules.
How to call a function in Python
Right from learning how to define a function and how to call a function, we studied what are different types of functions in python and various python function concepts.
Hope you liked this article on Python. See you soon with another piece of information on PrepBytes.
FAQs Related to Python
1. How are arguments different from parameters?
They have exact working but when passing from a parent function, they are called arguments while being in the function definition, they can be termed as function parameters.
2. How to call a function in Python?
Answer to how to call a function in python is given in Section ii) What is a python function?