You can understand it at a glance! ! ! Python functions from entry to advanced!

Function is an important concept in programs. It can encapsulate a piece of reusable code and complete specific tasks by passing parameters and return values. In this blog, we will start from the introductory level of functions and gradually deepen into advanced concepts.

1. Basic definition of function:
In Python, the definition of a function is completed using the keyword `def`. A simple function definition looks like this:

```python
def greet():
    print("Hello, world!")
```


The above code defines a function called `greet`, which when called, will print out "Hello, world!".

2. Function parameters:
The function can accept parameters and is used to receive externally passed data. Parameters can be required or optional. Here is an example of a function that accepts one required parameter:

```python
def greet(name):
    print(f"Hello, {name}!")
```


In the above code, `name` is a required parameter. When the function is called, passing a parameter to it, such as `greet("Alice")`, will print out "Hello, Alice!".

3. The return value of the function:
The function can return a value to the caller through the `return` keyword. Here is an example of a function that accepts parameters and returns a result:

```python
def add_numbers(a, b):
    return a + b
```


In the above code, the function `add_numbers` accepts two parameters `a` and `b` and returns their sum. We can use `result = add_numbers(3, 5)` to call the function and assign the return value to the `result` variable.

4. Default parameters:
The function can use default parameters to define an optional parameter. If a value for this parameter is not provided when the function is called, the default value will be used. Here is an example of a function using default parameters:

```python
def greet(name, greeting="Hello"):
    print(f"{greeting}, {name}!")
```


In the above code, `greeting` is a parameter with a default value. If we call `greet("Alice")`, it will print out "Hello, Alice!"; and if we call `greet("Alice", "Hi")`, it will print out "Hi , Alice!".

5. Variable parameters:
Functions in Python can also accept a variable number of parameters. These varargs can be positional or keyword arguments. Here is an example of a function that accepts a variable number of positional arguments:

```python
def calculate_sum(*numbers):
    sum = 0
    for num in numbers:
        sum += num
    return sum
```


In the above code, `*numbers` means accepting any number of positional parameters. We can use `calculate_sum(1, 2, 3)` to call the function and get 6 as the return value.

6. Anonymous function:
Python also supports using the `lambda` keyword to create anonymous functions. These functions are usually used for simple operations and do not have function names. Here's an example of using an anonymous function to calculate the sum of two numbers:

```python
add_numbers = lambda a, b: a + b
result = add_numbers(3, 5)
print(result)  # 输出:8
```

7. Higher-order functions:
In Python, functions can also be passed as parameters to other functions, or as return values ​​of other functions. Such functional operations are called higher-order functions. Here's an example that accepts a function as a parameter:

```python
def apply_func(func, x):
    return func(x)

def square(x):
    return x ** 2

result = apply_func(square, 3)
print(result)  # 输出:9
```


In the above code, the function `apply_func` takes a function `func` and a value `x` and passes the value to the function for processing.

Guess you like

Origin blog.csdn.net/weixin_49016330/article/details/131865240
Recommended