How to Use def () Function in Python

How to Use def () Function in Python

3 mins read2.9K Views Comment
Updated on Aug 16, 2024 14:40 IST

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.

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

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

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.
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 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
  • 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 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)
Copy code

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

How to Check if a Python String is a Palindrome

How to Generate Random Numbers in Python?

For Loop in Python (Practice Problem) – Python Tutorial

Calculator Program in Python: A Step-By-Step Guide

Conditional Statements in Python – Python Tutorial

How to Find an Armstrong Number Using Python

Difference Between Module and Package in Python

Constructors in Python: Definition, Types, and Rules

Difference Between Methods and Functions in Python

Star Pattern in Python

Methods to Check for Prime Numbers in Python

Top 7 Online Python Compiler Picks for 2024

How to Compute Euclidean Distance in Python

Bubble Sort Algorithm in Python

How to Find the Factorial of a Number Using Python

Find the Second Occurrence of a Substring in Python String

Types of Functions in Python

Difference Between Mutable and Immutable in Python

ord() and chr() Function in Python

 

About the Author