Python [day 9] function Start 2

In this section:
1. What is the function of
tone 2. Using the function definition, function name, the function and the function of the body
3. The function returns the value of
the parameter of the function 4

First, what is the function

We can go to define a thing or functional (interfaces, services, functions, function) until the time required to go directly to a fine..
Thing, this is the definition of a function.
Function: encapsulation of functions or actions

Second, the definition of the function

def function name (parameter list):

Function body

Function body: Code function is called, to be executed

Call function
function name ()

Call the execution function

1, defined function
2, calling function
3, ready to execute the function
4, the body performing the function
code is 5, the function of the body is finished, this call is completed

Question:
After the function call, you need to get a result - that is, the return value (return)

Third, the return value of the function

1, returns a value of
2, to return multiple values (multiple values separated by commas), constituting a plurality of tuple values (caller receives a tuple), can be deconstructed
3, no return or return the blank back the default is to return None
4, encountered return, behind the code will not execute, execute the function terminates
      return function is similar to the for loop break

Question:
so what way, you can use a different chat tool, but do not modify the source code?
- have a way of passing parameters to solve

Fourth, the parameters of the function

1. Defined Functions

def function name (parameter list):

Function body

2, call the function

      Function name (argument list)

concept:

  • Parameter: the defined function written in the form that is variable parameters (hereinafter - the parameter)
  • Argument: specifically given function call is the actual value of the parameter (hereinafter - argument)
  • Biography Reference: during the function call, the process argument is assigned to the parameter of mass participation

Classification parameters of
001 arguments classification

  • 1, the position parameter: the parameter order of position, the incoming arguments - the most common
  • 2, keyword parameters: position without having to remember the order parameter, the only parameter by keyword arguments to pass to

Applicable scene: a long parameter list, the parameter memory location is not good when

  • 3, the mixing parameter:

Rule: location parameter must be placed in front, on the last keyword parameter (otherwise, error)
Examples: open ( 'xxx.txt', mode = 'r', encoding = 'utf-8')

Classification parameter 002

  • 1, the position parameter: argument position in the order parameter, which is passed - the most common
  • 2, the default value of the parameter:

Rules: positional parameters must be in front, the default value of the parameter must be in the final (otherwise, it will error)
example:
the Register (name, Age, Sex = 'M') #-defined functions (Applicable scene: Most of the students in the case of men, specify the default parameters sex = 'male')
the Register ( 'Jack', 18) # call the function 1, par 3 do not write, using the default parameters - male
register ( 'lucy', 17, ' female') # call the function 2 parameters 3 - female overrides the default parameters

  • 3, the dynamic parameters (the section * arg, ** kwargs)

 

Guess you like

Origin www.cnblogs.com/wangtp/p/11525363.html