User Defined Functions in Python

Last time off, we covered what are built-in functions and how they are used in Python. We also some common built-in functions which remove the hassle of writing extra lines of codes to do simple or complex operations.

But the same versatility cannot always be provided by built-in functions and for that, you need to go for User-Defined Functions. As the name suggests, user-defined functions are those which are written by the programmer himself for a variety of tasks. They generally include a combination of built-in function and each separate function serves a different purpose within the program. 

Given below is the general declaration of user-defined functions in Python:

def functionName(parameters):
    #Function code block goes in the indented body.

    return xyz

The above code gives us an idea of how to define our own functions. The def keyword is followed by the name of your function and in brackets, you can specify whether your function will take any parameters or arguments, plus how many parameters and arguments. 

After that in the indented body, you can write your own function expression based on the operation you want it to perform. The function is closed off by a return statement which once again is optional. 

Let us look at 2 examples of user-defined functions in Python. The first one below is a Digit Separation function for 4 digit numbers:

#Digit Separation Function
def dig_separate(num):
    d1 = num//1000
    d2 = num%1000//100
    d3 = num%100//10
    return d1,d2,d3,d4

#Program Initialization
num= int(input("Enter a 4 digit number:"))
print(d1,d2,d3,d4, d1+d2+d3+d4)

This program above takes a 4 digit number as an input and it transfers that as an argument to the dig_separate() user-defined function. The function then proceeds to divide the 4 digit number by multiples of 10 and the separated digits are returned at the end of the function. This simple program can be expanded to include any number of digits using a for loop. 

Given below we have another user-defined function which converts integers to binary code. The program successively prints all binary equivalents of numbers from 100 to 1000.

#Integer to Binary

def int2binary(x):
    while x>=1:
    return bina

#User Program

x= 100
while x<=1000:

In the program above, we are passing each integer value as an argument to our int2binary() user-defined function. The function then divides the value repeatedly by 2 and stores the binary numbers (1s and 0s). They are afterwards printed when the function returns the value below to variable “d” and the output is printed.

User-Defined functions can get complex as well but it is recommended to keep one operation specific to a function. This will allow you to trace any problems or bugs that may occur in your program. If you have crammed everything in a single function, then debugging it will get incredibly difficult. 


I am a Software Engineer with ample experience in making games, websites, mobile apps and augmented reality solutions.

Pin It on Pinterest