User Defined Functions in Python

In the previous tutorial, we have familiarized about functions in Python. We have studied that there are two types of functions in Python programming language. They are:

  • Predefined functions (also called built-in functions)
  • User-defined functions

A predefined function is that function whose functionality is predefined in Python. There are several predefined functions that are always present to use in Python. For example: print(), int(), float(), pow(), etc.

Now we will learn about user-defined functions in Python with the help of some useful examples.

User defined Function in Python


A user defined function in Python is a function that is created by the user to perform a specific task in a program. It gets executed whenever we call the function.

A function makes a program smaller by eliminating repetitive code. It reduces the duplication of code. Using the function, we can divide a block of long code into smaller segments.

Functions improve the clarity of code that makes the program more understandable. They also help to hide vital information in the main program.

Syntax of User defined Function

The general syntax to create a user defined function is very simple, and it is as:

def function_name(parameter_list): # function header
    . . . . . .
    body_of_the_function
    . . . . . .
    return <value(s)>

In the above function declaration, the first line of the function definition is header, and the rest is the function of body.

In Python, the declaration of user defined function starts with the def keyword and followed by the function name and parentheses [()]. We can choose any logical name for a function. Generally, the rules for function names are the same as for identifiers (variable names).


The function header must end with a colon character (:) and the statements inside the function body have to be intended. Statements inside the block of function get executed whenever the function is called.

The formal parameters inside the pair of parentheses (()) in the function definition are optional. We can have zero or more parameters inside it.

When we include formal parameters inside the parentheses, they accept values sent to them by the calling function. When a function gets invoked, arguments passed to it are called actual parameters.

The function’s body can have one or more statements, each statement must intend with the same amount (4 spaces) from the header line. When a def statement is executed, then a function object creates to store the body of function (statements).

The return statement is optional. We use it to return a value or values to the calling function, then use the return keyword with the value/values by separating the comma.

Valid Examples of User-defined Functions in Python


Example 1:

# user defined function definition
def greeting():
    print('Hello world!')
    print('Welcome to the world of the programming!')
# Function calling.
greeting()
Output:
      Hello world!
      Welcome to the world of the programming!

In this example, we have created a function named greeting() with def keyword. The function does not contain any parameter and ends with a colon character (:).


The function’s body contains two statements with the same indentations (i.e. 4 white spaces). This function does not return any value.


Example 2:

# Simple Python function to check even and odd.
 def evenOdd(x): # function header. x is a local variable.
    if x % 2 == 0:
        print('Number',x,'is even')
    else:
        print('Number',x,'is odd')
# Calling functions by passing two argument values.
evenOdd(20)
evenOdd(25)
Output:
      Number 20 is even
      Number 25 is odd

In this example, we have created a function named evenOdd with one parameter x. Inside the body of the function; we have used the if-else statement to check even or odd number. This function is not returning any value.


Example 3:

# Function definition having two parameters, such as name and age.
def person(name, age): # Here, name and age are local variables.
    print(name)
    print(age)
person('John', 24)
Output:
      John
      24

Example 4:

# Function to find the sum of three numbers.
def sum_three_numbers(n1, n2, n3): # Here, n1, n2, n3 are local variables.
    sum = n1 + n2 + n3  # Here, sum is a local variable.
    return sum

# Calling a function by passing three argument values.
# We will store the returned value into a variable named s.
s = sum_three_numbers(20, 30, 40) # Here, s is a global variable.

# Displaying the value of s in the console.
print('Sum of three numbers = ',s)
Output:
      Sum of three numbers = 90

In this example, we have created a function named sum_three_numbers with three parameters, such as n1, n2, and n3.

This function’s body has two statements: first is a variable sum that adds three numbers and stores it into the variable sum and second is the return statement that returns the value to the calling function.

Advantage of User defined Functions


Following are the advantages of using user defined functions in Python:

  • Reduces duplication of code by eliminating the repetitive code.
  • Breaking up of a long and complex program into smaller segments.
  • Improved the clarity of code.
  • Helps to hide the vital information in the main program.
  • Makes the code to be reusable, thus saving memory.

In this tutorial, you have learned about user defined functions in Python programming language. You have also understood about how to create a user defined function using def keyword in a program.

Hope that you will have understood all the basic key points of user defined function and all practiced all example programs.

In the next tutorial, we will learn about how to call a function in Python with the help of various examples.
Thanks for reading!!!

⇐ Prev Next ⇒

Please share your love