You can define your own functions using the def
keyword. Here’s a simple example of a user-defined function that calculates the square of a number:
def square(number):
"""This function returns the square of the given number."""
return number ** 2
Code language: Python (python)
Let’s break down the components of this function:
def
: This keyword is used to start the definition of a function.square
: This is the name of the function. You can choose any valid name for your function.(number)
: This is the function’s parameter list. Here, we have a single parameter namednumber
, which is the input to the function."""This function returns the square of the given number."""
: This is called a docstring. It is a multi-line string that provides a brief description of what the function does. It’s good practice to include docstrings in your functions to explain their purpose and usage.return number ** 2
: This is the body of the function. It calculates the square of thenumber
parameter using the**
exponentiation operator and returns the result.
After defining the function, you can call it by passing an argument to it. For example:
result = square(5)
print(result) # Output: 25
Code language: Python (python)
In this example, the square()
function is called with the argument 5
, and it returns 25
, which is the square of 5
.
How do you write a user-defined function?
To write a user-defined function in Python, you need to follow these steps:
- Use the
def
keyword followed by the function name to start the function definition. - Define the function parameters within parentheses
()
. Parameters are inputs that the function can accept. You can have zero or more parameters. - Add a colon
:
at the end of the function definition line. - Indent the function body with four spaces (or a tab). This is important as Python uses indentation to determine the scope of the function.
- Write the code statements that make up the body of the function.
- Optionally, include a
return
statement to specify the value the function should return. If there’s noreturn
statement, the function returnsNone
by default.
Here’s a more general template for writing a user-defined function:
def function_name(parameter1, parameter2, ...):
"""Optional docstring: description of the function."""
# Function body: code statements that perform some operations.
# Optionally, use the 'return' statement to provide a result.
return result
Code language: Python (python)
Let’s see an example of a simple user-defined function that adds two numbers:
def add_numbers(a, b):
"""This function adds two numbers and returns the result."""
result = a + b
return result
Code language: Python (python)
You can call this function by passing two numbers as arguments:
sum_result = add_numbers(3, 5)
print(sum_result) # Output: 8
Code language: Python (python)
Remember that you can have user-defined functions with zero or more parameters, and they can return different types of values or even no values (using return
without any expression).
Read More;
- What is slicing and indexing in Python explain with an example?
- What are the 7 operators in Python?
- How can I run a Python script online for free?
- What is Generator in Python With Example?
- What is class and object in Python with example?
- What are type hints in Python With Example?
- Python Script Example for DevOps
- What is enumerate() in Python with an example?
- What is a for else in Python With Example?
- What is the filter() function with an Example?
- What is module and example in Python?
- What is overriding in Python with example?