Create a simple function in Python


You wish to create your own function in Python. In this post we can see how to create different types of functions. Python already gives us built-in functions like print(), exit(), etc. But we can also create custom functionality and reuse it.


We can use the def keyword to define our own custom function. Let’s create a simple function that prints a message:
					def greet():
     print("Hello function!")


In the main program we can use it by calling it:

					greet() #the opening and closing parenthesis mean it’s a function
python hello function
Function with a parameter
					def greet(message):


In the main program we can use it by calling it:

					greet("Hi there") #inside the parentheses goes the parameter

python function with parameter

This function takes a parameter and uses it as a parameter to the print function. It can be of any type as python is dynamically typed. The parameter goes inside the parentheses of the function.

Other examples of calling with various data-types:

					greet(5.34) #float
greet(1023) #int
greet([1,2,3,4,5]) #list

python example of function with float parameter
python example of function with int parameter
python example of function with list parameter
A function with return statement

Let’s see an example of a function that returns a value. This one accepts two parameters, adds them together and returns us the result.
					def add(a,b):
     total = a + b
     return total

In the main program we can use it by calling it:

					addition = add(10,34)

Python function with return example

Let’s create a function that only returns something:
					def only_return():
     return "only return this string"


In the main program we can use it by calling it:

					msg = only_return()
Python function with return statement but no parameters

This function takes no parameter but returns a string message. A function does not necessarily have to have parameters. It can be parameterless.

What happens if I save the result of a function that doesn’t return anything?

For example, the function below does not have a return statement:
					def test():
     x = 5

In the main program I will save the result of the test() function as follows:

					result = test()
python function default return value

No error is thrown. In fact the function returned the value None which expresses the value of nothing. That is because according to mathematics a function returns a value. In Python if we don’t put a return statement, Python secretly adds one that returns None by default.


In this post we saw how to create different types of functions. Function with no parameter, function with parameter, function with and/ or without return statement. In any case, one of the benefits of using a function is that we can reuse its functionality by simply typing the function’s name.

Share it!

Picture of Ellion


Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x