How to Use def () Function in Python
The def () keyword in Python defines the function users can use to build their own. In this article, we will discuss using a def () keyword to create your own function in Python.
In this article, we will learn how to create our own function in Python. In Python, we can create functions using def keywords.
So, without further delay, let’s move on to learn how to use a def keyword to define a function in Python.
Explore How to Find an Armstrong Number Using Python
Table of Content
Best-suited Python courses for you
Learn Python with these high-rated online courses
Functions in Python
Functions play an important role in any programming language. A function is a set of instructions that can be repeatedly used. In simple terms, it is a reusable piece of code that carries out a specific task.
In Python, we mainly have three different types of functions:
- Built-in Function
- They are pre-defined functions that can be directly used.
- Example: abs (), min (), max (), print (), type (), etc.
- User Defined Function
- They are not pre-defined functions; Python allows users to create their own functions as per their requirements.
- Anonymous Function
- Besides built-in and user-defined functions, Python has an anonymous function that is used without defining a name.
- The lambda function is an anonymous function in Python.
- Besides built-in and user-defined functions, Python has an anonymous function that is used without defining a name.
How to Define and Call a Function
User–Define Function
If you have to create a program you use repeatedly in your program, then you can define your functions. In Python, user can define their function using def keywords.
- def keyword can be used to define the method of a class.
- it is also used to define the member function of a class __init__().
Now, let’s see how def keyword defines a function.
Syntax
def function_name(arguments): “ “ “ functions logics ” ” ” return values
Parameters
- def: It is a keyword that is used to define (declare) a function.
- function_name: It is a name that you give to your function.
- The function name should be straightforward, easily remembered, and understandable.
- arguments: value that is passed in the function.
- There are two special types of arguments:
- *args: it is used when we don’t know how many arguments a function will receive during runtime.
- **kwargs: It is used if we want to receive named arguments
- There are two special types of arguments:
- return: It is an optional value that returns a value from the function.
- Function immediately exits when they come across the return statement.
- It can be used to return multiple values.
Programming Online Courses and Certification | Python Online Courses and Certifications |
Data Science Online Courses and Certifications | Machine Learning Online Courses and Certifications |
Examples
Example-1: Create a function to find the cube of a number.
def cubic(n): cube = n*n*n return cube
# Using the cubic function find the cube of 3result = cubic(3)print(result)
Output
27
Example-2: Create a function to find the sum of two numbers.
def addition(m, n): add = m + n print('Addition of two numbers is:', add)
# Find the sum of 9 and 10addition(9, 10)
Output
Addition of two numbers is: 19
Example-3: Create a function using *args
def addition(*args): add = 0 for i in args: add += i return add
# Find the sum of first 5 integersprint('Sum of first five integers is:', addition(1, 2, 3, 4, 5))
# Find the sum of first 3 prime numbersprint('Sum of first 3 prime numbers is:', addition(2, 3, 5))
Output
Sum of first five integers is: 15 Sum of first 3 prime numbers is: 10
Example – 4: Create a function using **kwargs
def roll_number(**kwargs): for i in kwargs: print(i, kwargs[i])
# Find the roll number and student nameroll_number(A = 1001, B = 1002, C = 1003, D = 1004, E = 1005)
Output
A 1001 B 1002 C 1003 D 1004 E 1005
Until now, we have a clear understanding of what a Python function is and how to define one. When defining a function, we use an argument to pass the value to the function. Python allows using a user-defined function as an argument.
Confused!!
Let’s see the below example of how we can pass a function as an argument.
Passing Function as an Argument
def cubic(n): return n * n * n
result = list(map(cubic, [1, 2, 3]))print(result)
Output
[1, 8, 27]
Conclusion
In this article, we discussed the def () keyword in Python and how to define and call a user-defined function using an example. Finally, we have discussed how a function can be passed inside another function as an argument.
Hope you will like the article.
Check Our More Python Blogs
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio