Home  >  Article  >  Backend Development  >  How to define and call functions in Python

How to define and call functions in Python

WBOY
WBOYOriginal
2023-10-20 18:40:451581browse

How to define and call functions in Python

How to define and call functions in Python

Python is a concise, easy-to-read and powerful programming language, in which functions are an important concept in Python . A function is a named and reusable block of code that accepts parameters and returns a result. This article will introduce how to define and call functions in Python, while providing specific code examples.

  1. Define functions

Defining functions in Python is very simple, use the keyword def followed by the function name, then a pair of parentheses and a colon . The indented block of code after the colon is the function body. The following is a simple example:

def greet():
    print("Hello, Python!")

In the above example, we defined a function named greet, and the code in the function body is used to output "Hello, Python!".

  1. Call the function

After defining the function, we can call the function using the function name followed by a pair of parentheses. The following is an example of calling the greet function in the above example:

greet()

In the above example, we called the greet function, and the code in the function body was executed, Output "Hello, Python!".

  1. Function with parameters

In Python, a function can receive one or more parameters. Parameters are placeholders when the function is defined, and specific values ​​can be passed to these parameters when the function is called. The following is an example of a function with parameters:

def greet(name):
    print("Hello, " + name + "!")

In the above example, we define a function named greet and specify a parameter in parentheses after the function namename. The code in the function body is used to output "Hello," followed by the value of the name parameter entered.

When calling a function with parameters, you need to pass the corresponding parameter value. The following is an example of calling the greet function in the above example:

greet("Python")

In the above example, we called the greet function and passed a string parameter "Python ". The code in the function body is executed and "Hello, Python!" is output.

  1. Default values ​​of parameters

In Python, the parameters of functions can be set to default values. If the corresponding parameter value is not passed when the function is called, the function will use the default value as the value of the parameter. The following is an example of setting the default value of a parameter:

def greet(name="Python"):
    print("Hello, " + name + "!")

In the above example, we define a function named greet and specify a parameter in parentheses after the function namename, and set the default value to "Python". The code in the function body is used to output "Hello," followed by the value of the name parameter entered.

When calling a function with default value parameters, you can optionally pass parameter values. The following is an example of calling the greet function in the above example:

greet()  # 输出: Hello, Python!
greet("World")  # 输出: Hello, World!

In the above example, no parameters are passed when the greet function is called for the first time, and the function uses the default value "Python", output "Hello, Python!". When calling the greet function for the second time, the parameter "World" is passed. The function uses the passed parameter value and outputs "Hello, World!".

Through the introduction of this article, we have learned how to define and call functions in Python, and provided specific code examples. Functions are an important concept in Python programming, which can improve code reusability and readability. Mastering the definition and calling methods of functions is very helpful for further learning and using the Python language.

The above is the detailed content of How to define and call functions in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn