Python04- function (ordinary functions, anonymous functions)

Function is a program in a program can be used repeatedly.

======================================

Ordinary function
  • Function name (required)
  • Parameters: parameter (formal parameters, when defined), arguments (actual parameters, call time)
  • Statement block (Required)
  • return
  • variable

Can perform print directly, no return value. Running may be the results returned using a block of statements return.

def learn_python(location):
    print("我正在{}上学Python".format(location))    #语句块

def learn_python(location):
    doing = ("我正在{}上学Python".format(location))
    return doing

======================================

Anonymous function

Function without a name, which is omitted from the process of def-defined functions.
lambda is just an expression, not a function of the body, using lambda as follows:

lambda  arg1,arg2,arg3,...: expression    #arg1,arg2,arg3表示具体参数,expression表示参数要执行的操作
f = lambda x,y:x+y
f(1,2)
3

======================================

supplement:

Keyword arguments : next to the name of a parameter defined on it saysome (words = 'make love to change the world', name = 'Luoying Xi')

The default parameters : define the parameter default values

Collection parameters : in the end is not clear how many parameters to, preceded by a *****

Functions and procedures:
Function - returns a value returned by default tuples
process - none Return value

Global and local variables : the entire code which are accessible get, do not try to modify it within a function, you can go to access its value inside the function, Python will create a local variable inside a identical in function to replace it .
Have to change, then use the global keyword.

def MyFun():    global count
    count = 10
    print(count)
Published 56 original articles · won praise 0 · Views 777

Guess you like

Origin blog.csdn.net/xiuxiuxiu666/article/details/104315549