How to Use def () Function in Python

How to Use def () Function in Python

4 mins read2.9K Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Oct 13, 2023 14:03 IST

The def () keyword in python is used to define the function that users can use to built their own function. In this article, we will discuss how to use def () keyword to create your own function in python.

2023_01_MicrosoftTeams-image-128.jpg

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

Functions in Python

In any programming language, function plays an important role. It is a set of instructions that can be repeatedly used. In simple terms, it is a reusable piece of code to carry 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.
What is Programming What is Python
What is Data Science What is Machine Learning

How to Define and Call a Function

UserDefine Function

If you have to create a program that you are using 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 member function of a class __init__().

Now, let’s see how def keyword is used to define 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
  • 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 3
result = cubic(3)
print(result)
Copy code

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 10
addition(9, 10)
Copy code

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 integers
print('Sum of first five integers is:', addition(1, 2, 3, 4, 5))
# Find the sum of first 3 prime numbers
print('Sum of first 3 prime numbers is:', addition(2, 3, 5))
Copy code

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 name
roll_number(A = 1001, B = 1002, C = 1003, D = 1004, E = 1005)
Copy code

Output

A 1001
B 1002
C 1003
D 1004
E 1005

Until now, we have a clear understanding of what is python function and how to define a function in Python. While defining a function, we use an argument to pass the value in the function. Python allows using the 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)
Copy code
About the Author
author-image
Vikram Singh
Assistant Manager - Content

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