1.1 function overview

Functions are organized snippets of code that implement a single function or associated functions. We can think of a function as a piece of code with a name, which can be called in the form of "function name ()" where necessary.

• Using function to program can make the program modular and reduce redundant code
• Make the program structure clearer
• It can improve the programming efficiency of developers
• Facilitate later maintenance and expansion.

It can realize code reuse more efficiently, and copy and paste can also realize code reuse, but the efficiency is low.

```# # Method 1: Print
# # Print a square with 2 asterisks on each side
# for i in range(2):
#     for j in range(2):
#         print('*', end='')
#     print()
#
# # Print a square with 3 asterisks on each side
# for i in range(3):
#     for j in range(3):
#         print('*', end='')
#     print()
#
# # Print a square with 4 asterisks on each side
# for i in range(4):
#     for j in range(4):
#         print('*', end='')
#     print()

# Mode 2: function call
def print_t(lenth):
for i in range(lenth):          # i representative bank
for j in range(lenth):      # j stands for column (x-axis)
print('*', end='')
print('')

print_t(2)							# function call
print_t(3)
print_t(4)

# print(print_t(2))
# print(print_t(3))
# print(print_t(4))

```

1.2 defining functions and calls

Principle: first define the function and then call the function

Definition of function

The print() function and input() used earlier are built-in functions of Python, which are defined by Python.

Developers can also define functions according to their own needs. Python uses the keyword def to define functions. Its syntax format is as follows:

Example: define a function to calculate the sum of two numbers

```# Definition of parameterless function
result = 11 + 22
print(result)

result2 = a + b
print(result2)

```

Function call

The function will not be executed immediately after the definition is completed, and will not be executed until it is called by the program.

The syntax format of function call is as follows:

Function name ([parameter list])

```def add2(a,b):
result2 = a + b
print(result2)

```

When calling the function, the computer program goes through the following four steps:

• The program pauses at the location where the function is called.
• Pass data to function parameters.
• Execute the statements in the body of the function.
• The program returns to the pause to continue.

Other functions can also be called inside a function, which is called nested call of a function.

Examples are as follows:

```# Definition of parameterless function
result = 11 + 22
print(result)

result2 = a + b
print(result2)

# Function call
```

extend

Nested definition of functions

When a function is defined, another function can be nested inside it. At this time, the nested function is called the outer function, and the nested function is called the inner function.

Examples are as follows:

```# Nested definition of functions
# Note: the outer function can not directly call the inner layer function, and can only call the inner layer function in the outer function.
result2 = a + b
print(result2)
print('test:Test of function nested definition!')

```

Note: the outer function can not directly call the inner layer function, and can only call the inner layer function in the outer function.

1.3 transfer of parameters

We usually call the parameters set when defining a function as formal parameters (formal parameters for short), and the parameters passed in when calling a function as actual parameters (actual parameters for short). Parameter transfer of function refers to the process of transferring actual parameters to formal parameters.

The transfer of function parameters can be divided into:

• Transfer of position parameters
• Keyword parameter passing
• Transfer of default parameters
• Packaging and unpacking of parameters
• Mixed transfer

Transfer of position parameters

Transfer of position parameters

Positional parameter: when calling a function, it is passed to the formal parameter in turn according to the actual parameter position defined by the function, that is, the first actual parameter is passed to the first formal parameter, and the second actual parameter is passed to the second formal parameter.

```# Transfer of position parameters
# Positional parameters: when calling a function, parameters are passed according to the parameter positions defined by the function
def user_info(name, age, gender):
print(f'The name is:{name}, Age:{age}, Gender is:{gender}')

user_info('Xiao Ming', 42, 'male')
```

Note: the order and number of parameters passed and defined must be consistent

```user_info('Xiao Ming', 'male', 42, 12)   # If the number of parameters is inconsistent, an error will be reported
user_info('Xiao Ming', 'male', 42)       # If the parameter order is inconsistent, no error will be reported, but the data is meaningless
```

Keyword parameter passing

Background: if the number of parameters of a function is large, it is difficult for developers to remember the role of each parameter, and it is not advisable to use location parameters.

At this time, you can pass parameters in the form of keyword parameters.

Keyword parameters are specified in the form of "key = value" or "formal parameter = argument", which makes the function clearer and eliminates the order requirements of parameters.

Note: if there are location parameters and keyword parameters at the same time, the location parameters should be put in front, and the keyword parameters still do not consider the order

```# Keyword parameter passing
# Keyword parameters are specified in the form of "key = value", which makes the function clearer and eliminates the order requirements of parameters.
# Note: if there are location parameters and keyword parameters at the same time, the location parameters should be put in front, and the keyword parameters still do not consider the order

def user_info(name, age, gender):
print(f'The name is:{name}, Age:{age}, Gender is:{gender}')

user_info(name='Xiao Ming', gender='male',age=13)
user_info('Fang Wang',gender='female', age=20)

```

Only the slash symbol "/" is restricted for position parameters, indicating that the parameters of the specified position can only pass arguments in the form of position parameters.

Examples are as follows:

```# Positional parameters only
def user_info(name, age, /, gender):
print(f'The name is:{name}, Age:{age}, Gender is:{gender}')

user_info('Fang Wang',12, gender='male')
```

Passing of default (default) parameters

Introduction to default parameters: assign default values to formal parameters when defining functions; When calling a function, you can choose whether to use the default value as needed. If not, the argument can specify a parameter to the formal parameter again

Note: the position of the default parameter must be after (at the end of) the position parameter

```# Default (default) parameter passing
# Introduction: when defining a function, assign a default value to the formal parameter; When calling a function, you can choose whether to use the default value as needed. If not, the argument can specify a parameter to the formal parameter again
# Note: the position of the default parameter must be after (at the end of) the position parameter

def user_info(name, age, gender='female'):
print(f'The name is:{name}, Age:{age}, Gender is:{gender}')

user_info('Fang Wang', 12)                       # Use default parameters
user_info('Fang Wang', 12, gender='male')          # Modify default parameter values
```

Run the program and the output results are as follows:

Stage case - student management system

Keywords: Python

Added by scliburn on Wed, 16 Feb 2022 13:49:47 +0200