In this article, we will discuss what a function is in Python? and how to create one?

Introduction to Functions

A function is a set of statements grouped together which performs a specific task and can be used multiple times. Fundamentally, functions allow us to not have to repeatedly write the same code again and again.

Functions are the main building blocks used to construct larger and larger amounts of code to solve problems.

  • It helps us to reuse code in python.
  • Reduces the program complexity.
  • Makes program design easy and less lengthy.

def in Python

Syntax to declare a function in python:

The function declaration begins with a def keyword then a space followed by the name of the function.

Note: Do not name a function the same name as a built-in function in Python (such as len).

Next, comes a pair of parenthesis with a number of arguments separated by a comma. These arguments are inputs to the function. You can use these inputs inside the function and reference them. After this, you need to put a colon (:).

Important: you must indent to begin the code inside your function correctly. Python makes use of whitespace to organize code. Lots of other programming languages do not do this, keep that in mind.


Let’s see some examples to make have clear understanding of functions in python.



Return in Function

Let’s see some examples with the return statement.

return in a function is used to return a result back to from where it has been called so that it can be used further.



Using Break


I hope that you now have a basic understanding of creating your own functions to save yourself from writing the same code again and again.

Leave a Reply