A mathematical-defined function and a function in Python
Junior high School math function definition: Generally, in a change process, if there are two variables x and Y, and for each definite value of x, Y has a unique value corresponding to it, then we refer to x as the independent variable, y is called the dependent variable, Y is the function of X. The range of values of the argument x is called the definition field of the function.
such as Y=2*x
function definitions in Python: functions are a programmatic method of logical structuring and process.
def foo (x): " The function Definitions " x+=1 return x def : Defines the function's keyword foo: function name (): Inner definable parameter "" : Document description (not necessary, but strongly recommend adding descriptive information for your function) x+=1: Refer tocode block or program processing logic return : Define return value call run: can take parameters or do not have function name ()
Two. What's wrong with no function
1. Unstructured without structure, code redundancy
2. Poor readability
3. Unable to achieve unified management, and maintenance costs
Three. Functions in Python are divided into two classes: built-in functions, custom functions
Built-in functions: Sum: Sum () Max: Max () exponentiation: Min () Calculate length: Len ()
Example:
A = Len ([1,2,3,4])print= max ([1,2,6,4,])print(b)
Custom functions
def Print_start (): #def definition function keyword #print_start函数名 print( ' * '*6) #: Colon write child code block
Print_start () #调用函数直接输出
defprint('helloWorld')
Four. Why to define a function
Summarize the benefits of using a function:
1. Code Reuse
2. Maintain consistency and ease of maintenance
3. Extensibility
Functions are defined and used, and are used directly if not defined, which is equivalent to referencing a non-existent variable name
4. The use of functions consists of two stages: defining function stages and usage stages
Grammar:
# Syntax: def function name (parameter 1, Parameter 2,): """ Document Comment Code function, parameter, result, what is the return value """ function Body return value,
Five. Define three forms of a function
1. No parameter function: If the function is simply performing some operation, it is defined as an parameterless function.
def foo (): Print ('*'*6)
2. Define an argument function: The function's execution depends on the externally passed-in parameter, and the parameter function usually has a return value
x = 10
y = 2
def My_max (x, y):
Resifelse y return resres=my_max #调用函数的表达式形式
Three-dimensional expression
x=10y=2resifelse y # Condition established write left, condition not established write right X can be replaced by any other, what is the return value ?
Six. Calling functions
deffoo ():Print('From foo')defBar (name):Print('bar===>', name)#function calls can be divided into two types according to the arguments and no argumentsFoo ()#no arguments are defined, and no parameters are passed in the callBar ('Egon')#when defined, there must be arguments at the time of invocation.
Day03-python Basic Functions