PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Python Functions

Python Functions

Updated on: January 26, 2025 | 13 Comments

In Python, the function is a block of code defined with a name. We use functions whenever we need to perform the same task multiple times without writing the same code again. It can take arguments and returns the value.

Python has a DRY principle like other programming languages. DRY stands for Don’t Repeat Yourself. Consider a scenario where we need to do some action/task many times. We can define that action only once using a function and call that function whenever required to do the same activity.

Function improves efficiency and reduces errors because of the reusability of a code. Once we create a function, we can call it anywhere and anytime. The benefit of using a function is reusability and modularity.

Also, See

  • Python Functions Exercise
  • Python Functions Quiz

Table of contents

  • Types of Functions
  • Creating a Function
    • Creating a function without any parameters
    • Creating a function with parameter s
    • Creating a function with parameters and return value
  • Calling a function
    • Calling a function of a module
  • Docstrings
    • Single-Line Docstring
    • Multi-Line Docstring
  • Return Value From a Function
    • Return Multiple Values
  • The pass Statement
  • How does Function work in Python?
  • Scope and Lifetime of Variables
    • Local Variable in function
    • Global Variable in function
      • Global Keyword in Function
    • Nonlocal Variable in Function
  • Python Function Arguments
    • Positional Arguments
    • Keyword Arguments
    • Default Arguments
    • Variable-length Arguments
  • Recursive Function
  • Python Anonymous/Lambda Function
    • filter() function in Python
    • map() function in Python
    • reduce() function in Python

Types of Functions

Python support two types of functions

  1. Built-in function
  2. User-defined function

Built-in function

The functions which are come along with Python itself are called a built-in function or predefined function. Some of them are listed below.
range(), id(), type(), input(), eval() etc.

Example: Python range() function generates the immutable sequence of numbers starting from the given start integer to the stop integer.

for i in range(1, 10):
    print(i, end=' ')
# Output 1 2 3 4 5 6 7 8 9Code language: Python (python)

User-defined function

Functions which are created by programmer explicitly according to the requirement are called a user-defined function.

Creating a Function

Use the following steps to to define a function in Python.

  • Use the def keyword with the function name to define a function.
  • Next, pass the number of parameters as per your requirement. (Optional).
  • Next, define the function body with a block of code. This block of code is nothing but the action you wanted to perform.

In Python, no need to specify curly braces for the function body. The only indentation is essential to separate code blocks. Otherwise, you will get an error.

Syntax of creating a function

def function_name(parameter1, parameter2):
       # function body    
       # write some action
return valueCode language: Python (python)

Here,

  • function_name: Function name is the name of the function. We can give any name to function.
  • parameter: Parameter is the value passed to the function. We can pass any number of parameters. Function body uses the parameter’s value to perform an action
  • function_body: The function body is a block of code that performs some task. This block of code is nothing but the action you wanted to accomplish.
  • return value: Return value is the output of the function.

Note: While defining a function, we use two keywords, def (mandatory) and return (optional).