参数和变量之间的差异 (Visual Basic)Differences Between Parameters and Arguments (Visual Basic)

在大多数情况下,过程必须具有有关在其中调用该过程的环境的一些信息。In most cases, a procedure must have some information about the circumstances in which it has been called. 执行重复或共享任务的过程对每个调用使用不同的信息。A procedure that performs repeated or shared tasks uses different information for each call. 此信息包含变量、常量和在您调用过程时传递给该过程的表达式。This information consists of variables, constants, and expressions that you pass to the procedure when you call it.

若要将此信息传递给过程,过程定义了一个参数,并且调用代码将参数传递给该参数。To communicate this information to the procedure, the procedure defines a parameter, and the calling code passes an argument to that parameter. 可以将参数视为停车空间,将参数视为汽车。You can think of the parameter as a parking space and the argument as an automobile. 正如不同的汽车可以在不同时间的停车空间中停放一样,每次调用该过程时,调用代码都可以将不同的参数传递给相同的参数。Just as different automobiles can park in a parking space at different times, the calling code can pass a different argument to the same parameter every time that it calls the procedure.

参数Parameters

参数表示一个值,在调用该过程时,该过程要求您传递该值。A parameter represents a value that the procedure expects you to pass when you call it. 过程声明定义了其参数。The procedure's declaration defines its parameters.

在定义 FunctionSub 过程时,请在紧跟在过程名称后面的括号中指定参数列表When you define a Function or Sub procedure, you specify a parameter list in parentheses immediately following the procedure name. 对于每个参数,可以指定名称、数据类型和传递机制(ByValByRef)。For each parameter, you specify a name, a data type, and a passing mechanism (ByVal or ByRef). 还可以指示参数是可选的。You can also indicate that a parameter is optional. 这意味着,调用代码不必为其传递值。This means that the calling code does not have to pass a value for it.

每个参数的名称作为过程中的局部变量The name of each parameter serves as a local variable in the procedure. 使用参数名称的方式与使用任何其他变量的方式相同。You use the parameter name the same way you use any other variable.

参数Arguments

参数表示在调用过程时传递给过程参数的值。An argument represents the value that you pass to a procedure parameter when you call the procedure. 调用代码在调用过程时提供自变量。The calling code supplies the arguments when it calls the procedure.

调用 FunctionSub 过程时,将在紧跟过程名称的括号中包含参数列表When you call a Function or Sub procedure, you include an argument list in parentheses immediately following the procedure name. 每个参数都对应于列表中同一位置的参数。Each argument corresponds to the parameter in the same position in the list.

与参数定义不同,参数没有名称。In contrast to parameter definition, arguments do not have names. 每个自变量都是一个表达式,它可以包含零个或多个变量、常量和文本。Each argument is an expression, which can contain zero or more variables, constants, and literals. 计算表达式的数据类型通常应与为相应参数定义的数据类型匹配,在任何情况下,它都必须可以转换为参数类型。The data type of the evaluated expression should typically match the data type defined for the corresponding parameter, and in any case it must be convertible to the parameter type.

另请参阅See also