Getting Started with Python (13) Functions (1)

Author: xiou

1. Function overview

Functions are named blocks of code that perform specific tasks. To perform a specific task defined by a function, the function is called. When you need to execute the same task multiple times in the program, you don't need to repeatedly write the code to complete the task, you just need to call the function that executes the task and let Python run the code in it. We will find that programs are easier to write, read, test, and fix by using functions.

2. Function definition

Here is a simple function that prints a greeting, called greet_user():

 def greet_user():
     """显示简单的问候语。"""
     print("Hello!")

 greet_user()

This example demonstrates the simplest function structure. Use the keyword def to tell Python that you are defining a function. This is the function definition, which tells Python the name of the function, and possibly, within parentheses, what information the function needs to do its job. Here, the function is called greet_user(), and it doesn't need any information to do its job, so the parentheses are empty (even so, they are necessary). Finally, the definition ends with a colon.

All the indented lines immediately following def greet_user(): make up the function body. The text is comments called docstrings that describe what the function does. Docstrings are enclosed in triple quotes, and Python uses them to generate documentation about functions in a program.

The line of code print("Hello!") is the only line of code in the body of the function, so greet_user() does only one job: print Hello!.

To use this function, call it. A function call lets Python execute the code of a function. To call a function, specify the function name followed by the necessary information enclosed in parentheses. Since this function doesn't require any information, just greet_user() is required to call it. As expected, it prints Hello!:

insert image description here

2.1 Pass information to the function

With a little modification, the function greet_user() can not only display Hello! to the user, but also use the user's name as the title. To do this, add username inside the parentheses of the function definition defgreet_user(). By adding username here, you make the function accept whatever value you specify for username. Now, this function requires you to specify a value for username when calling it. When calling greet_user(), you can pass it a name, as follows:

def greet_user(username):
    """显示简单的问候语。"""
    print(f"Hello, {
      
      username.title()}!")

greet_user('jesse')

The code greet_user('jesse') calls the function greet_user() and provides it with the information it needs to execute the function call print(). This function takes the name you pass it, and sends a greeting to the person:

insert image description here

Likewise, greet_user('sarah') calls the function greet_user() passing it 'sarah', which prints Hello, Sarah!. The function greet_user() can be called as many times as needed, and no matter what name is passed in when calling, the corresponding output will be generated.

2.2 Actual parameters and formal parameters

When the function greet_user() was defined earlier, it was required to assign a value to the variable username. When this function is called and provided with such information (person's name), it will print the corresponding greeting.

In the definition of the function greet_user(), the variable username is a formal parameter (parameter), that is, the information needed by the function to complete the work. In the code greet_user('jesse'), the value 'jesse' is an argument (argument), which is the information passed to the function when it is called. When calling a function, enclose the information you want the function to use within parentheses. In greet_user('jesse'), the actual parameter 'jesse' is passed to the function greet_user(), and this value is assigned to the formal parameter username.

Note We sometimes don't distinguish between formal parameters and actual parameters, so if you see someone calling a variable in a function definition an actual parameter or a variable in a function call a formal parameter, don't be surprised.

Guess you like

Origin blog.csdn.net/qq_41600018/article/details/130902613