We know that the area of the circle is calculated as:
S =ΠR2
When we know r the value of the radius, we can calculate the area based on the formula. Suppose we need to calculate the area of 3 circles of different sizes:
r1 = 12.34r2 = 9.08r3 = 73.1s1 = 3.14 * r1 * r1s2 = 3.14 * r2 * r2s3 = 3.14 * r3 * r3
When the code repeats itself regularly, you need to be careful about it, and it's not only troublesome to write every time, 3.14 * x * x but if you want to 3.14 change it, you have to replace it 3.14159265359 all.
With the function, we don't write every time s = 3.14 * x * x , but rather write a more meaningful function call s = area_of_circle(x) , and the function area_of_circle itself needs to be written once, and it can be called multiple times.
Basically all high-level languages support functions, and Python is no exception. Python is not only very flexible in defining functions, but it has built in many useful functions that can be called directly.
Abstract
Abstraction is a very common concept in mathematics. As an example:
Calculate the sum of the series, such as: 1 + 2 + 3 + ... + 100 , writing is very inconvenient, so mathematicians invented the summation symbol ∑, can be 1 + 2 + 3 + ... + 100 recorded as:
100
∑n
N=1
This abstract notation is very powerful, because we see that ∑ can be understood as sums, rather than reduced to lower-level addition operations.
Moreover, this abstract notation is extensible, such as:
100
∑ (n2+1)
N=1
The reduction into the addition operation becomes:
(1 x 1 + 1) + (2 x 2 + 1) + (3 x 3 + 1) + ... + (x 100 + 1)
It can be seen that, with the help of abstraction, we do not care about the underlying computational process, but directly consider the problem at a higher level.
The same is true of computer programs, where functions are the most basic form of code abstraction.
Functions of Python