Python functions are user defined
Besides the built-in Python functions, you can also define Python functions yourself, these functions are called user-defined functions (Python). What are the benefits of using these self-defined functions, how to define functions in Python, we will learn in this Python lesson.
What is the user-defined Python function?
The functions that we define ourselves to perform specific tasks are called user-defined functions. The function definition and function call are mentioned in the Python function.
The functions available in Python are called integrated functions. If we use functions written by other users in the form of a library, these functions are called library functions. Thus, the function we define can become a library function for certain users.
Advantages when using user-defined Python functions
- User-defined functions help analyze a large program into small pieces, making it easier to understand, maintain, and debug.
- When a code is repeated in the program, it is possible to use the function to gather this code and run it as needed by calling the function.
- Developers work on a large project, which can divide each other's work by creating different functions.
Example of user-defined function
To define a function, you know when you learn about the Python function overview. Here, I will repeat the basic syntax when defining a function:
def ten_ham (DoiSo1, DoiSo2, ., DoiSon)
function block of the function
For example:
def them_so (a, b):
tong = a + b
return tong
so1 = 5
so2 = 6
so3 = int (input ("Enter a number:"))
so4 = int (input ("Enter some more:"))
print ("The first two numbers are:", them_so (so1, so2))
print ("The sum of the following two numbers is:", them_so (so3, so4))
In the above example, the int (), input (), and print () functions are built-in functions in Python. Here, we define the them_so () function, whose function is to add two numbers, sum the two numbers and return the result. The output of this program is as follows:
Enter a number: 8
Enter another number: 10
The first two numbers are: 11
The sum of the following two numbers is: 18
Naming functions by function or task will make it easier for the reader to understand, you should practice this when code.
Python Exercises: More than 100 Python exercises have solutions (sample code)
Next lesson: Python function parameter
Previous article: Built-in Python functions
You should read it
May be interested
- The vars () function in Pythonthe vars () function in python returns the __dict__ attribute of the passed object if the object has the __dict__ attribute.
- How to use Closure in Pythonin this article, tipsmake.com will work with you to learn about closure in python, how to define a closure and why you should use it. let's go find the answer!
- Wool () function in Pythonhow is the syntax of len () function, what parameters do it have and how is it used? invites you to read the track.
- What is a function in Python? Functions in Pythonfunctions in python have many different types. you need to clearly understand python functions to use them properly.
- Module time in Pythonpython has a time module used to handle time-related tasks. tipsmake.com will work with you to find out the details and functions related to the time specified in this module. let's follow it!
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Multiple choice quiz about Python - Part 7following the previous test set, part 7 continues with the topic built-in functions in python. let's try with quantrimang to try the 10 questions below.
- Zip () function in Pythonthe zip () function in python returns a zip object, a list iterator of tuples that combines elements from other iterators (made of iterable words).
- Function sleep () in Pythonmodule time in python provides some very useful functions to handle time-related tasks. one of the most commonly used functions is sleep ().
- Decorator in Pythondecorator is used a lot in python. so how to create a decorator and why you should use it. let's go find the answer!