A Python function is a self-contained module of code that performs a specific task. It takes inputs, processes them, and then returns a result.
For example, a function add_two() takes two numbers as input performs an addition operation on them, and then returns their sum.
Define a function once and use it as many times as you want. It helps larger code to keep organized and enhances the code reusability. In Python, there are two types of functions –
- Built-in functions – These are predefined function in the Python language
- User-defined functions – These function are defined by the user themselves
In this article, we will learn user-defined functions in Python along with some examples.
Why you should use functions
- With the use of functions, you can reuse the code instead of rewriting it
- It helps our program to be modularized that means you can test a smaller part of the program in isolation.
- You can easily track a larger code when it is divided into functions which make code maintenance easy
Syntax of a Python function
In Python, you can define a function with the help of the def
keyword. The syntax of which is given below –
def func_name(parameter(s)): statement(s)
Where func_name is the name of the function
parameter(s) are comma-separated variables passed to the function
and statement(s) represents the definition of the function.
Steps to write a function in Python
Follow the given steps to write a function in Python –
- First, understand the purpose of the function and why you need it
- Define the data that will be provided by the caller in the form of parameters
- Decide variables to use inside the function to accomplish our goal
- Follow the Python syntax of defining a function which is given in the above section and write its main logic
Example of a Python function
In the following example, the function will add two numbers and return their sum.
def add_two(a,b): return a+b # Call add_two() function and print the sum of two numbers print(f"The sum of 5 and 7 is {add_two(5,7)} ")
You can see the output of this program in the given image –
Where a and b are the parameters passed to the function. The 5, 7 are the real values passed by the user to the called function these are known as the function argument.
Function parameters
The function parameters are the named variables passed to function while defining it. These parameters are initialized with the real value that is passed while calling a function in the program.
You can pass multiple comma-separated parameters to a function. While calling it you need to supply the same number of arguments as parameters are given.
For example, in function add_two() there are two parameters i.e. a and b and when we call it we are passing two arguments i.e. 5 and 7. So we cannot call it by passing a single or more than two arguments this can raise some error in the program.
Default parameters
The default parameters are the named variable whose default value is provided while giving function definition. For example –
def cal_power(a,b=2): return a**b # call the function with default argument print (f"The square of 5 is {cal_power(5)}") # call the function by passing both arguments print (f"The cube of 5 is {cal_power(5,3)}")
Now when you run this program you will see –
Always define default parameters, in the end, that means a non-default argument should not follow a default argument otherwise you will get a syntax error.
Python recursive function
The recursion is the process of defining something in the terms of itself. A function calling itself is known as a recursive function. The following example shows how it works.
def factorial(x): if x == 1: return 1 else: return(x * factorial(x-1)) print("The factorial of 5 is {factorial(5)"}
Now when you pass an integer the function will calculate its factorial by recursively calling itself.
For better understanding try to create a Python function on your own and manually pass the value and calculate the result. Now if you have a query then write us in the comments below.