Python function basics entry 9: the concept of function

1. The concept of function
1) Definition (what is a function): A function is an encapsulation of the code that implements a specific function. (The essence of a function is actually a function, and different functions have different functions)
2) Classification of functions:
(machines made by others) System functions: Python has defined functions, programmers only need these functions Just call it when. For example: print, input, type, id, hex, sum, max,...
(make your own machine) Custom function: the function defined by the programmer
"""
2. Definition of function (make machine)

Syntax:
def function name (parameter list):
function description document
function body

Description:
1) def-keyword; fixed writing
2) function name-named by the programmer; it is an identifier, not a keyword;
all letters are lowercase, and words are separated by underscores to
see the name and meaning (see The function name probably knows the function of this function)
does not use the system function name, class name and module name
3) ():-fixed writing
4) formal parameter list-a. With'variable 1, variable 2, variable 3,...' The form exists, and there is no formal parameter;
b. The function of the formal parameter is to transfer the data outside the function to the function. When defining the function, the formal parameters are not
needed, and several are needed. See the need for additional data to realize the function of the function Need several
5) Function description documents-equivalent to the machine manual, used to help others to use the current function more conveniently;
use three double quotation marks (not necessarily written)
6) Function body-keep with def An indented one or more statements; the
function body is the code segment (core) that implements the function

# 练习1:写一个函数求一个整数的阶乘
# N! = 1*2*3*4*...(N-1)*N
def factorial(n):
    sum1 = 1
    for x in range(1, n+1):
        sum1 *= x
    print(f'{n}是:{sum1}')
练习2:写一个函数,将指定字符串中指定的字符全部删除
def del_char(string, char):
    # 方法一:
    new_str = ''
    for x in string:
        if x != char:
            new_str += x
    print(new_str)
    # 方法二:
    # new_str = ''.join([x for x in string if x != char])
    # print(new_str)

3. Function call
(important!) The function body will not be executed when the function is defined; the function body will be executed when the function is called!

1) Call function (using machine)
syntax:
function name (list of actual parameters)

Description:
Function name-the name of the function that needs to be used (this function name must be the function name of the function that has been defined)
Argument list-multiple data separated by commas; the specific number depends on the formal parameter of the corresponding function The
actual parameters are the data passed to the function through the formal parameters when the function is used

Note: The same function can be called multiple times

2) Function call process
Step 1: Return to the position of function definition
Step 2: Pass parameters (assign values ​​to formal parameters with actual parameters to ensure that each parameter has a value)
Step 3: Execute the function body
Step 4: Execute After the function body, return to the position of the function call and then execute

Guess you like

Origin blog.csdn.net/SaharaLater/article/details/111564044