Directory
1. Basic function syntax and features
2. Parameters and Local variables
3. Return value
Nested functions
4. Recursion
5. Anonymous functions
6. Introduction to Functional programming
7. Higher-order functions
8. Built-in functions
1. Basic function syntax and features
function is derived from mathematics, but the concept of "function" in programming is very different from the function in mathematics, the specific difference, we will say later, the function of programming in English also has a lot of different names. In basic it is called subroutine (sub-process or subroutine), in Pascal is called procedure (process) and function, in C only function, in Java is called method.
Definition: A function that encapsulates a set of statements by a name (function name), and to execute the function, simply call the name of its functions
Characteristics:
- Reduce duplicate code
- To make the program extensible
- Make programs easier to maintain
Syntax definitions
Def sayhi (): #函数名 print ("Hello, I ' m nobody!") Sayhi () #调用函数
can take parameters
#下面这段代码a, b = 5,8c = A**bprint (c) #改成用函数写def calc (x, y): res = x**y return res #返回函数执行结果 c = Calc (b) # result assigned to c variable Print (c)
Python functions, recursion, built-in functions