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.
1 2 3 4 5 6 7 |
# Defining the function def FunctionDemo(): print('hello python') print('second line') # Calling the Function using small parentheses FunctionDemo() |
Sample Output:

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.
1 2 3 4 5 6 7 8 9 10 |
# Defining a function with user input def FunctionDemo2(UserInp): print('hello') print('your input is', UserInp) # Calling the function with string FunctionDemo2('hello') # Calling the function with number FunctionDemo2(200) |
Sample Output:

Accepting interactive user input
You can use the input() function to ask the user to give a value when you call the function.
1 2 3 4 5 6 7 8 9 |
# Accepting a interactive user input def FunctionUserInp(): %matplotlib inline print('please enter a value') userInp=input() print('your input is:', userInp) # Calling the function FunctionUserInp() |
Sample Output:

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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Defining a function with two arguments def FunctionAdd(num1, num2): sumValue=num1+num2 print('the sum of',num1,'and',num2, 'is:', sumValue) # Positional call FunctionAdd(4,5) # Positional call FunctionAdd(5,4) # Call by reference # preferred way to call your functions FunctionAdd(num2=5, num1=4) |
Sample Output:

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.
1 2 3 4 5 6 7 8 9 10 |
# Defining a function with default argument values def FunctionAddDefault(num1=10, num2=20): sumValue=num1+num2 print('the sum of',num1,'and',num2, 'is:', sumValue) # Calling without any inputs, using default values FunctionAddDefault() # Calling with inputs, it will overwrite the defaults FunctionAddDefault(num1=2, num2=5) |
Sample Output

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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Defining a function with a return statement def FunctionAddDefault(num1=10, num2=20): sumValue=num1+num2 multiplyValue=num1*num2 print('the sum of',num1,'and',num2, 'is:', sumValue) print('the multiplication of',num1,'and',num2, 'is:', multiplyValue) # return must be the last statement # there must be only one return statement return sumValue, multiplyValue # Calling the function # The output of the function can be stored # in the same order in which the values are returned sumResult, prodResult = FunctionAddDefault(num1=2, num2=10) # Printing the results print('sumResult:',sumResult) print('prodResult:',prodResult) |
Sample Output:

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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Global and Local variables in a Function # Declaring a global variable x=5 # Defining the function def FunctionLocalGlobal(): # Creating a local variable y=x+10 print('value of x is:',x) print('value of y is:',y) # Calling the function FunctionLocalGlobal() ## Variable x is available outside function print('value of x is:',x) # Variable y is not available outside function print('value of y is:',y) |
Sample Output:
