0 Fundamentals python-15.1 Why you need to write a function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.