1. 首页
  2. 文档
  3. function

白皮书下载

实在智能Agent产品白皮书(7.2.0).pdf

function

发刊日期:
2021/10/15

编辑团队:
实在学院

Functions are organized, reusable snippets of code used to implement a single, or associated function. Function can improve the modularity of application and the reuse rate of code. similarPython provides many built-in functions, such as print (). But you can also create your own functions, which are called user-defined functions.

1、 Define a function

You can define a function that you want to function. The following are simple rules:

Function code blockDef keyword followed by function identifier name and parentheses ().

Any passed in parameters and arguments must be placed between parentheses. Parentheses can be used to define parameters.

The first line of the function optionally uses a document string— Used to store function descriptions.

The function content starts with a colon and is indented.

Return [expression] ends the function and optionally returns a value to the caller. A return without an expression is equivalent to returning none.

2、 Function call

Defining a function only gives the function a name, specifies the parameters contained in the function, and the code block structure. After the basic structure of this function is completed, you can execute it through another function call.

3、 Parameter transfer

When calling a function, there are necessary parameters, keyword parameters, default parameters and variable length parameters.

Required parameters:Functions must be passed in the correct order. The number of calls must be the same as when declared. callFor the printme () function, you must pass in a parameter, or there will be a syntax error.

Keyword parameters:Keyword parameters are closely related to function calls. Function calls use keyword parameters to determine the incoming parameter values. Using keyword parameters allows the order of parameters when calling a function to be inconsistent with that when declaring, because the language interpreter can match parameter values with parameter names.

Default parameters:When calling a function, the value of the default parameter is considered as the default value if it is not passed in.

Variable length parameters:You may need a function that can handle more parameters than you originally declared. These parameters are called indefinite length parameters, and the aboveThe two parameters are different and will not be named when declared.

4、 Return statement

Return statement [expression] exits the function and optionally returns an expression to the caller. Return statements without parameter values return none.

5、 Variable scope

Not all variables of a program can be accessed anywhere. Access rights depend on where the variable is assigned. The scope of the variable determines which part of the program you can access which specific variable name. It is divided into two categories: global variables and local variables. Global variables variables defined inside the function have a local scope, and variables defined outside the function have a global scope. Local variables can only be accessed inside the declared function, while global variables can be accessed throughout the program. When a function is called, all variable names declared within the function are added to the scope.

 

 

企业培训
技术支持
加入社群
公众号
实在智能Agent学习群
扫码关注微信公众号