User defined functions in Python

Functions are at the heart of modern python programming. For each task, there is a function inside a specific library.

There are many ready-to-use available functions under libraries like numpy, pandas, matplotlib, sklearn, etc. However, if you are doing some steps in your job which is being repetitive, then you can create your own function to automate the tasks, these functions are known as user-defined functions.

User-defined functions are very commonly used in the IT industry while deploying the models. The front end UI (Java/Tableau/React) can call a python function, which can take the inputs from the UI and pass to the predictive model and get back the predictions.

Another use of functions is to split the complex functionalities into multiple smaller chunks, each chunk is performed by one user-defined function.

important things to remember about user-defined function are listed below

  • A user-defined function is created using the “def” keyword
  • A user-defined function can take any number of parameters as inputs called “arguments”
  • A user-defined function can have only one return statement but it can return multiple objects
  • The return statement must be the last statement inside a function
  • A function can call other functions inside it
  • The variables created inside the function are called local variables, these variables are NOT accessible outside the function
  • The variables created outside the function are called global variables, these variables are accessible inside the function

Creating a simple function and calling it

“def” is the keyword that is used to create a function. The simple function defined below does not take any argument as input, and simply prints two lines. Whatever is written using an indentation of a tab below the “def” line in a function is known as the scope of function, Here the scope is of two lines. It can be of 200 lines or 2000 lines also, it depends on how complex the function is.

Once the function is defined, in order to run it, you need to call it! By its name 🙂 and provide the inputs in parentheses if the function is defined to accept inputs. In the below example, the function does not take any inputs, hence we call it by just giving empty parentheses.

Sample Output:

Simple User Defined Function in Python
Simple User Defined Function in Python


Function with one argument

Below snippet defines a function which takes one argument. The important thing to note is that you don’t have to define the data type of the input argument! This is one of the basic properties of Python that the datatype of a variable is defined by the kind of value it stores.

Here you can pass any type of input to the function, string, or number or even a dataframe! It will not give you trouble unless and until you do something illegal with the input. For example, if you are trying to calculate the square root of the input and pass a string as in input then it will throw an error.

Sample Output:

Function with one argument in python
Function with one argument in python


Accepting interactive user input

You can use the input() function to ask the user to give a value when you call the function.

Sample Output:

Taking interactive user input in a function in Python
Taking interactive user input in a function in Python


Function with multiple arguments

A User-defined function can take as many arguments as you want, but if your the number of arguments is exceeding 10 in your user-defined function, then please consider spitting the functionality into two functions. It becomes difficult to manage functions with too many arguments.

When you call a function with multiple arguments, there are two ways to call the function

  • Positional call: Values are assigned based on the order in which they are passed
  • Reference call: Values are assigned to the arguments

Below is an example of a user-defined function to take 2 inputs.

Sample Output:

Function with multiple arguments
Function with multiple arguments


Function with default argument values

While defining the function, you can provide the default values to each argument. Hence, even if the user does not supply any input, the function will run with the default values of the arguments.

You can choose to give default values to all the arguments or only few of them. when you give default values for only some arguments, then the user must supply the input for other arguments.

Sample Output

Function with default input values
Function with default input values


The return statement in Function

There could be only one return statement in a function which has to be the last line in a function. The return statement can return multiple values at a time.

Sample Output:

The return statement in python function
The return statement in a python function


Global/Local variables in Function

A variable that is defined outside the function is known as a Global variable and it is available inside the function, but, a variable defined inside the function is known as Local variable and it will not be available outside the function.

For example, in the snippet below, variable “x” is global and the function was able to access it inside it. The variable “y” is a local variable, which is not available outside the function.

Sample Output:

Global and local variables in a python function
Global and local variables in a python function

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!

Leave a Reply!

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