Defined functions - functions basis

Dian a system function

Next, we will function in accordance with this system give you a detailed introduction function:

 * What is the function?
 * Why use functions?
 * Classification Function: built-in function with a custom function
 * how custom function
  1. Syntax
  2 is defined with function parameters, and has a function parameter scenarios
  application scenario 3. Define the non parametric function, a function of no arguments and
  4. Define empty function, function and application scenarios empty
 * calling function
  1. how to call the function
  returns the value 2. the function of the
  application 3. the function parameters: formal and actual parameters, the location parameter, the position of arguments, argument a keyword, the default parameter, * args, ** kwargs

 * Higher-order function (function objects)
 * nested function
 * and scope namespace
 * decorator
 * iterator with the function generator and coroutine
 * ternary operator, the list of parsed generator expression
 recursive function call *
 * Built functions
 * process-oriented programming and functional programming


Two Dian What is the function?

Suppose now that you are a sewer worker, if you are ready to advance your toolbox, such as sewer repair work you receive when you directly to your toolbox to get past on the line directly, without the need to prepare interim hammer Han .

In the program, the function is a function with the tool, the tool is ready to advance is defined functions, application scenarios brought on by experience that the called function.
Summary: The function is prepared in advance with some of the features of the tool, It can greatly improve development efficiency of the developer.


Wed and Why function

If you do not use the function, you will encounter three problems when writing programs:

 1. lengthy procedure
 2. Procedure differential extensibility
 3. The difference readability


Four Dian How function

Define function, after the call.
 * Defined Functions

def 函数名(param1、param2……):
    """
    函数功能的描述信息
    :param1:描述
    :param2:描述
    :return:返回值
    """
    code 1
    code 2
    code 3
    ...
    
    return 返回值

Personal vernacular: the complete body of the function def have a function name (parameter): Function Comment code function return value of the function body (the default return None, currently in contact with 2 forms yield (-> dynamic return), return, default is not to write return None there is the following function calling part of!)
 * call function

函数名(param1、param2……)

Personal vernacular: After the function name () is the function is called, in essence, is the implementation of information stored on the function name function address pointed to

4.1 registration function function

# 注册功能函数
def register():
    """注册功能"""
    username = input('username: ').strip()
    pwd = input('password: ').strip()

    with open('38a.txt', 'a', encoding='utf8') as fa:
        fa.write(f"{username}:{pwd}\n")
        fa.flush()


register()
# 复用
register()
register()

4.2 Login Function Function

# 登录功能函数
def login():
    """登录功能"""
    inp_username = input('username: ').strip()
    inp_pwd = input('password: ').strip()

    with open('38a.txt', 'rt', encoding='utf8') as fr:
        for user_info in fr:
            user_info = user_info.strip('\n')
            user_info_list = user_info.split(':')
            if inp_username == user_info_list[0] and inp_pwd == user_info_list[1]:
                print('login successful')
                break
        else:
            print('failed')


login()

4.3 function definition stage

def func():
    bar()  # 不属于语法错误,不会报错
    print('*'*10)
  • Key: function definition stage only detect syntax, the code does not execute the function body !!!

    4.4 function call stage

def bar():
    print('from bar')

def foo():
    print('from foo')
    bar()

foo()
'''
from foo
from bar
'''
def foo():
    print('from foo')
    bar()
    
def bar():
    print('from bar')

foo()
'''
from foo
from bar
'''

Function name () ---> call

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374785.html
Recommended