Python User-defined functions

All the functions that are written by any of us come under the category of user-defined functions. Below are the steps for writing user-defined functions in Python.

  • In Python, a def keyword is used to declare user-defined functions. 
  • An indented block of statements follows the function name and arguments which contains the body of the function. 

Syntax:

def function_name():
statements
.
.

Example: Here we have created the fun function and then called the fun() function to print the statement.

Python3




# Declaring a function
def fun():
    print("Inside function")
 
# Driver's code
# Calling function
fun()


Output: 

Inside function

Python User defined functions

A function is a set of statements that take inputs, do some specific computation, and produce output. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can call the function. Functions that readily come with Python are called built-in functions. Python provides built-in functions like print(), etc. but we can also create your own functions. These functions are known as user defines functions.

Table of Content 

  • User defined functions 
  • Parameterized functions 
    • Default arguments 
    • Keyword arguments  
    • Variable length arguments  
    • Pass by Reference or pass by value? 
       
  • Function with return value  

Similar Reads

Python User-defined functions

All the functions that are written by any of us come under the category of user-defined functions. Below are the steps for writing user-defined functions in Python....

Python Parameterized Function

...

Python Function with return value

The function may take arguments(s) also called parameters as input within the opening and closing parentheses, just after the function name followed by a colon....