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.
- Built-in functions
- 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.
# code
# end of a function
Let’s create our first Python function.
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.
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.
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.
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.
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.
Let’s learn by example.
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