BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python User defined Functions

Last Updated: June 10, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

In this tutorial you will learn user defined functions in Python with the help of examples.

In the last tutorial of Python functions, we discussed that there are two types of functions in Python: Built-in functions and user defined functions. Built-in functions are those that are already defined in Python libraries and we can call them directly. User defined functions are those that we define ourselves in our program and then call them wherever we want. In this article, we are going to discuss about user defined functions.

What are User defined Functions in Python

A function that you define yourself in a program is known as user defined function. You can give any name to a user defined function, however you cannot use the Python keywords as function name.

In python, we define the user-defined function using def keyword, followed by the function name.

Function name is followed by the parameters in parenthesis, followed by the colon

For example:

def function_name(parameter_1, parameter_2, ...) :
      statements
      ....

Calling a user-defined function

You can call a user defined function by using function name followed by the arguments in the parenthesis.

For example:

function_name(argument_1, argument_2)

Example of a user-defined function

In the following example we have defined a user-defined function sum. This function can accept two arguments as we have defined this function with two parameters. Inside the print() function we are calling the function sum and passing the numbers x & y as arguments.

In the sum() function we have a return statement that returns the sum of two parameters, that are passed to the function as arguments.

As you can see, we have used a print() function in the following example without even defining that function, this is because print() is a built-in function, which is already available to use and we can just call it.

# Program to demonstrate the
# use of user defined functions

def sum(a,b):
   total = a + b
   return total

x = 10
y = 20

print("The sum of",x,"and",y,"is:",sum(x, y))

Output:
Python user defined functions

❮ PreviousNext ❯

Top Related Articles:

  1. Python Recursion
  2. Python pass Statement
  3. Python Constructors – default and parameterized
  4. Python Keywords and Identifiers with examples
  5. Python Function Arguments – Default, Keyword and Arbitrary

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap