1. Why do I need a function?
1) Maximized code reuse and minimized code redundancy
Let's give a simple example: addition
>>> a=1>>> b=1>>> c=a+b>>> c2>>> d=1>>> f=1>>> e=d+f >>> E2
From the above code we see that if we want to do multiple additions, we need to write a large piece of code, but if we add the addition to the function:
From the above method, we can see that the number of written code is significantly reduced.
Moreover, the encapsulation method is more advantageous to later maintenance
If: In a large application, we use the addition tens of thousands of times, and then I now need to be, before the addition of two to print the sum, if not using the encapsulation function, then we must be in the large application of thousands of code changes, this cost is very high, but if encapsulated into a function, because it is reused, So we just need to change one place, we can compare the following two sets of code:
For the first method, we modify a lot of places, for the second method, we only need to modify a place, the cost is greatly reduced, and reduce the occurrence of bugs
2) decomposition of the process
For example, to make a steamed bread, his steps are: face, seasoning, cooking and other steps, each step we can be defined as a function, and then do a steamed bread is required to function is the one-way, and so on, each small step to execute the correct rate is higher than the overall operation.
In summary, this section notes why you need to write a function, mainly because it provides efficiency and decomposition steps, making the program easier to understand and execute
This chapter is here, thank you.
------------------------------------------------------------------
Click to jump 0 basic python-Catalogue
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
0 Fundamentals python-15.1 Why you need to write a function