Create a simple function in Python

problem

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.

SOLUTION

We can use the def keyword to define our own custom function. Let’s create a simple function that prints a message:

function.py
				
					def greet():
     print("Hello function!")

				
			
running

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

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

				
			

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

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

				
			
output
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

				
			
output
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.

function.py
				
					def add(a,b):
     total = a + b
     return total
				
			

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

				
					addition = add(10,34)
print(addition)

				
			
output
Python function with return example

Let’s create a function that only returns something:

function.py
				
					def only_return():
     return "only return this string"

				
			

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

				
					msg = only_return()
print(msg)
				
			
output
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:

function.py
				
					def test():
     x = 5
				
			

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

				
					result = test()
				
			
output
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.

conclusion

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!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

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

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x