Introduction of functions
Functions can be seen as a way of organizing a program, an abstraction and encapsulation of functional code blocks
Second, function definition
def function name ():
Function code Statement
Return
The function terminates the entire function when it encounters a return, and defaults back to none if the function does not write a return statement for it.
Iv. Invocation of functions
function name (corresponding parameter list)
Five, empty function
An empty function is a function that does nothing, and its function body can use the keyword pass directly, for example:
def non ():
Pass
Six, return multiple values
Functions in Python support return multiple values
def newxy (x, y):
return x+10, Y + 10
X, y = newxy (10,20)
Returning multiple values is an illusion of syntax, because returning a tuple in Python can be a syntax that is not required ().
Seven, parameter check
Python is a weak type of scripting language, so you need to pay great attention to checking the parameter types
The parameter type check can be easily implemented by using the system built-in function in Python: isinstance ().
def check (x):
If not isinstance (x, (int,float)):
Raise TypeError ("type not compliant")
......
Viii. Default Parameters
The default parameters in Python make Python functions more flexible and extensible
def student_info (name, age, sex = "male", school = "XX Primary School"):
Print ("=========== Student Information ===========")
Print ("Name:", name)
Print ("Ages:", age)
Print ("Gender:", Sex)
Print ("School:", school)
Print ("============end=============")
When calling a function, you can either: Student_info ("CQ", 20), or: Student_info ("CQ", 20,sex= "female")
Default parameter usage rules: Fixed parameter variable parameter before, default parameter is behind
Parameters can be passed either in a fixed order or directly by executing the specified parameters
The default parameter must point to an immutable object, because if you point to a Mutable object, then the function creates a variable object in memory and points to it when the call is defined, and then each subsequent invocation uses the object created and used between them directly.
Nine, variable parameters
Allow variable parameters to be passed to a function in Python, even if it is 0 arguments
You can define mutable parameters in the following ways:
def sum (*numbers):
sum = 0
For I in numbers:
sum + = i
return sum
You can sum (1,2,3,4) or sum (1,2,4,5,...) When the function is called
If a tuple or list already exists, it can be passed directly using a variable that is a variable parameter, that is, sum (*num_lsit)
X. keyword parameters
Inside the variable parameter is to assemble the variable parameters into a tuple, in Python can also be allowed to pass the keyword parameter, that is, the parameter name and its corresponding value of the specified function, the keyword parameter inside is to encapsulate the host as a dict to complete
Definition of the function of the keyword parameter:
def student_info (Name,age,**other):
Print ("Name:", name)
Print ("Ages:", age)
Print ("Others:", other)
When passing, you can use Student_info ("name", "City" = "Beijing", "sex" = "male"), of course, if there is already a defined dict, you can also call the function as follows: Student_info (" Cheqnian ", 20,**other_dict)
Note: The passed dict will have a copy inside the function, so the dict changes to the copy obtained within the function will not affect the external dict
XI. named keyword parameters
A named keyword can be viewed as a range that restricts the receipt of keyword parameters, such as the ability to specify what keywords to receive
Definition of a named keyword function:
def student_info (name,age,*,city= "Beijing", salary):
Pass
def student_info (name,age,*kw, city= "Beijing", salary)
Pass
12. Parameter combination
Multiple parameter forms can be defined in Python, in order to be non-confusing, the order of definition is: Required parameter, default parameter, variable parameter, named keyword parameter and keyword parameter
13. Recursive function
The recursive function refers to the function itself calling itself
def FAC (n):
if n = = 1:
return n
Else
return n * FAC (N-1)
Note: Excessive recursive calls can lead to stack overflow
Python Learning notes (vi)