Functions in Python

python programs

Summary: In this tutorial, we will learn what a function is, types of functions, how to create a function in Python using the def keyword.

Introduction to Functions in Python

A function is a set of statements that are grouped together under a common name to perform a specific task.

Function implements code reusability because a function can be called multiple times to perform the same task without rewriting the code.

Some of the prominent features of functions in Python are:

  • Makes the code reusable.
  • Reduces the program complexity.
  • Makes program design easy and less lengthy.

Types of Functions

Python has two types of functions:

  1. Built-In Functions: Functions that are already defined in Python e.g. max().
  2. User-Defined Functions: Functions defined by the users.

How to Create a Function in Python?

Following is the syntax to declare a function in python:

def name_of_function():
    """ docstring """
    #return desired result

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

It is recommended to not to name a function same as a built-in function in Python (such as len()) otherwise that built-in function will be overridden and lost.

Next, comes a pair of parentheses with arguments separated by a comma. These arguments are inputs to the function. You can use these inputs inside the function for your task.

After this, a colon (:) to mark end of the function header.

The function body is indented and starts with an optional docstring.

docstring in a human-readable statement that describes what the function does. It serves no purpose to the logic of the program.

Then comes the one or more python statement which is the main part of the function and performs the task which the function is intended to do.

The function ends with an optional return statement.

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.

Python Functions Examples

Here are some examples of Python user defined function.

Example 1:

def say_hello():
    This function output 'hello'

#call the function

Example 2:

#Function accepting a argument
def greeting(name):
    print('Hello ',name)

#must call function with a proper argument value
Hello Pencil

Examples with Return Statement

return inside a function is used for returning result back to the calling statement.

Here are some examples using the return statement.

Example 1:

def add(x, y):
    return x+y


Example 2:

def add(x, y):
    return x+y

#can also store the returned value in a variable
sum = add(5,4)

Examples using Break Statement

Break statement in Python is often used to stop and exit out of the loop iteration.


def check_prime(num):
    for x in range(2,num):
        if num%x == 0:
            print('Not Prime')
    if x == num-1:
Not Prime

In this tutorial, we learned what is a function in Python, types of python function and how to create a user defined funtion in Python using def keyword.

Leave a Reply

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