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
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 named
number, 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 the
numberparameter 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
In this example, the
square() function is called with the argument
5, and it returns
25, which is the square of
How do you write a user-defined function?
To write a user-defined function in Python, you need to follow these steps:
- Use the
defkeyword 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
returnstatement to specify the value the function should return. If there’s no
returnstatement, the function returns
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
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
You can call this function by passing two numbers as arguments:
sum_result = add_numbers(3, 5) print(sum_result) # Output: 8
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).
- 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?