Python Functions with Examples

Functions are a basic building block of the program. Functions consist of multiple programming statements grouped together for reusability. Functions provide the structure to the program and allow programmers to write reusable code.

There are two types of functions in Python.

  1. Built-in functions
  2. User-defined functions

As we have studied in the previous lessons, print() is a built-in function and there are more. In this lesson, we will be focusing more on how to create our own function i.e user-defined functions.

Here is the syntax to define the function in Python.

def function_name():
   # code
# end of a function

Let’s create our first Python function.

def printMessage(message):
    print(message)

printMessage("Hello World")
printMessage("Hi!")
printMessage("How are you")

We have defined a function that accepts a single parameter and prints the parameter value on the console.

Upon running the code above, you should see the following output.

Hello World
Hi!
How are you

This tutorial is a part of the free course – learn python from scratch. Enroll in this free course to learn Python from scratch.

Most of the time, you will come across a situation where we need to return some values from the function. To do so, Python provides return statement.

Let’s learn with the example.

def printMessage(name):
    return "Hello" + name

print(printMessage("shahid")) # prints hello shahid

In the function above, we take a parameter as an input and return the string as the output of the function.

The scope of the function in Python is simple. The variables assigned in the function cannot be used outside of the function unless it is returned.

Functions as argument

In Python, a function can become an argument for another function. Let’s learn by example.

def add(n1, n2):
    return n1 + n2

def subtract(n1, n2):
    return n1 - n2

def multiply(n1, n2):
    return n1 * n2

def divide(n1, n2):
    return n1 / n2

def calculator(operation, n1, n2):
    return operation(n1, n2)

result = calculator(add, 10, 20)
print(result)

Observer how we call the operation in the calculator function. The operation should be the name of the function i.e add, subtract, multiply, divide.

Let’s study how to create recursion functions in Python.

Recursion in Python

Recursion is the process in which a function calls itself during its execution. Each recursive call takes the program one scope deeper into the function.

Let’s learn by example.

def printNumber(number):
    print(number)
    if number == 0:
        return 0
    printNumber(number - 1)  # recursive call

printNumber(5)

In the function above, we are calling the function, we are first printing the number in the console, then reducing the number by 1 and calling the function again. When the number goes to 0, we stop by returning the value 0 in the function.

Let’s study the lambda functions in Python.

Lambda functions

We name our functions using the def keyword.

However, there is a special class of functions for which we do not need to specify function names, they are called lambda functions.

Here is the syntax of a lambda function.

lambda parameters:expression

Let’s learn by example.

add = lambda num1,num2: num1+num2

print(add(10,20)) # prints 30

lambdas are simpler and more readable than normal functions.

However, they are not a replacement for a normal function. Lambda function cannot have a multi-line expression. So for a single line statement execution, we can use lambda functions instead of creating a function using a def keyword.

This tutorial is a part of the free course – learn python from scratch. Enroll in this free course to learn Python from scratch.

Related Tutorials

Python Data Structures
GET and POST requests using Python

Pankaj Kumar
Pankaj Kumar
Articles: 207