Argument vs. Parameter | 参数值与参数

注:机翻,未校。


Argument vs. Parameter

参数值(Argument)与参数(Parameter)

What’s the Difference?

有什么区别?

Argument and parameter are two terms commonly used in the field of computer programming. An argument refers to a value that is passed to a function or method during its execution. It provides the necessary input for the function to perform its task. On the other hand, a parameter is a variable that is declared within the function or method definition. It acts as a placeholder for the arguments that will be passed when the function is called. In essence, arguments are the actual values passed to a function, while parameters are the variables that receive those values. Both arguments and parameters play crucial roles in defining and executing functions, allowing for the flexibility and customization of code.

“参数值(Argument)” 和 “参数(Parameter)” 是计算机编程领域中常用的两个术语。参数值(Argument)是指在函数或方法执行期间传递给它的值。它为函数执行其任务提供了必要的输入。另一方面,参数(Parameter)是在函数或方法定义中声明的变量。它充当占位符,用于接收在调用函数时将传递的参数值。本质上,参数值是传递给函数的实际值,而参数是接收这些值的变量。参数值和参数在定义和执行函数方面都起着至关重要的作用,使得代码具有灵活性和可定制性。

Comparison

Attribute
属性
Argument
参数值
Parameter
参数
Definition
定义
A value passed to a function or method during its invocation.
在调用函数或方法期间传递给它的值。
A variable or value that is used in a function or method declaration.
在函数或方法声明中使用的变量或值。
Usage
用途
Used to provide input or data to a function or method.
用于向函数或方法提供输入或数据。
Used to define the behavior or requirements of a function or method.
用于定义函数或方法的行为或要求。
Position
位置
Can be placed anywhere within the parentheses of a function or method call.
可以放在函数或方法调用括号内的任何位置。
Must be declared within the parentheses of a function or method declaration.
必须在函数或方法声明的括号内声明。
Value
Can be any valid data type or expression.
可以是任何有效的数据类型或表达式。
Can be any valid data type or expression.
可以是任何有效的数据类型或表达式。
Default Value
默认值
May or may not have a default value assigned.
可能有也可能没有分配默认值。
May or may not have a default value assigned.
可能有也可能没有分配默认值。
Number
数量
Multiple arguments can be passed to a function or method.
可以向函数或方法传递多个参数值。
Multiple parameters can be defined in a function or method declaration.
在函数或方法声明中可以定义多个参数。
Order
顺序
Order of arguments matters when passing values to a function or method.
向函数或方法传递值时,参数值的顺序很重要。
Order of parameters matters when defining a function or method.
定义函数或方法时,参数的顺序很重要。
Modification
修改
Arguments can be modified within the function or method.
可以在函数或方法内部修改参数值。
Parameters can be modified within the function or method.
可以在函数或方法内部修改参数。

Further Detail

更多细节

Introduction

简介

When it comes to programming and computer science, two terms that often come up are “argument” and “parameter.” While they may seem similar at first glance, they have distinct meanings and play different roles in the world of programming. In this article, we will explore the attributes of arguments and parameters, highlighting their differences and similarities.

在编程和计算机科学领域,经常出现的两个术语是 “参数值(Argument)” 和 “参数(Parameter)” 。乍一看,它们可能似乎相似,但在编程领域中,它们有着不同的含义并扮演着不同的角色。在本文中,我们将探讨参数值和参数的属性,突出它们的差异和相似之处。

Definition and Purpose

定义和目的

An argument is a value or expression passed to a function or method during its execution. It provides the necessary data for the function to perform its intended task. Arguments can be of various types, including numbers, strings, booleans, or even other functions. They allow us to customize the behavior of a function by providing different inputs.

A parameter, on the other hand, is a variable declared in the function or method signature that represents the data that will be passed as an argument. Parameters act as placeholders for the values that will be supplied when the function is called. They define the type and name of the expected input, allowing the function to be reusable and flexible.

While arguments are used when calling a function, parameters are used when defining a function. Arguments are the actual values passed to a function, while parameters are the variables that receive those values. Understanding this distinction is crucial for writing clean and efficient code.

参数值(Argument)是在函数或方法执行期间传递给它的值或表达式。它为函数执行其预期任务提供了必要的数据。参数值可以是各种类型,包括数字、字符串、布尔值,甚至是其他函数。通过提供不同的输入,它们使我们能够定制函数的行为。

另一方面,参数(Parameter)是在函数或方法签名中声明的变量,它代表将作为参数值传递的数据。参数充当占位符,用于接收在调用函数时将提供的值。它们定义了预期输入的类型和名称,使函数具有可重用性和灵活性。

参数值在调用函数时使用,而参数在定义函数时使用。参数值是传递给函数的实际值,而参数是接收这些值的变量。理解这种区别对于编写简洁高效的代码至关重要。

Declaration and Syntax

声明和语法

When declaring a function, parameters are listed within the parentheses following the function name. They are separated by commas and can have default values assigned to them. For example:

在声明函数时,参数列在函数名后面的括号内。它们用逗号分隔,并且可以为其分配默认值。例如:

function calculateArea(width, height = 10) {
    // function body 函数体
}

In this example, “width” and “height” are the parameters of the “calculateArea” function. The “height” parameter has a default value of 10, which means it can be omitted when calling the function.

在这个例子中,“width” 和 “height” 是 “calculateArea” 函数的参数。“height” 参数的默认值为 10,这意味着在调用函数时可以省略它。

On the other hand, arguments are passed to a function when calling it. They are enclosed in parentheses after the function name, separated by commas. For example:

另一方面,在调用函数时传递参数值。它们被括在函数名后面的括号内,用逗号分隔。例如:

calculateArea(5, 8);

In this case, 5 and 8 are the arguments passed to the “calculateArea” function, which will be assigned to the “width” and “height” parameters, respectively.

在这种情况下,5 和 8 是传递给 “calculateArea” 函数的参数值,它们将分别分配给 “width” 和 “height” 参数。

Relationship and Binding

关系和绑定

Arguments and parameters are closely related, as they work together to enable the execution of functions. When a function is called, the arguments provided are bound to the corresponding parameters in the function’s definition. This binding allows the function to access and manipulate the passed values.

参数值(Argument)和参数(Parameter)密切相关,因为它们共同作用以实现函数的执行。当调用函数时,提供的参数值会绑定到函数定义中的相应参数上。这种绑定使函数能够访问和操作传递的值。

It is important to note that the number and order of arguments must match the number and order of parameters in the function declaration. If there is a mismatch, the function may not work as expected or may throw an error.

需要注意的是,参数值的数量和顺序必须与函数声明中参数的数量和顺序匹配。如果不匹配,函数可能无法按预期工作,或者可能会抛出错误。

Additionally, arguments are typically passed by value, meaning that a copy of the argument’s value is made and assigned to the parameter. This ensures that any modifications made to the parameter within the function do not affect the original argument outside of the function’s scope.

此外,参数值通常是按值传递的,这意味着会创建参数值的副本并将其分配给参数。这确保了在函数内部对参数所做的任何修改都不会影响函数作用域之外的原始参数值。

Flexibility and Reusability

灵活性和可重用性

Parameters play a crucial role in making functions flexible and reusable. By defining parameters, we can create functions that can accept different inputs and produce different outputs based on those inputs. This allows us to write generic functions that can be used in various contexts.

参数(Parameter)在使函数具有灵活性和可重用性方面起着至关重要的作用。通过定义参数,我们可以创建能够接受不同输入并根据这些输入产生不同输出的函数。这使我们能够编写可在各种上下文中使用的通用函数。

Arguments, on the other hand, provide the ability to customize the behavior of a function for a specific use case. By passing different arguments, we can achieve different results without modifying the function itself. This flexibility allows us to reuse the same function with different inputs, reducing code duplication and improving maintainability.

另一方面,参数值(Argument)提供了针对特定用例定制函数行为的能力。通过传递不同的参数值,我们可以在不修改函数本身的情况下获得不同的结果。这种灵活性使我们能够使用不同的输入重用同一个函数,减少代码重复并提高可维护性。

Furthermore, parameters can have default values assigned to them, making them optional when calling the function. This feature enhances the flexibility of functions, as it allows us to provide default behavior while still allowing customization through arguments.

此外,参数可以分配默认值,这使得在调用函数时它们是可选的。此功能增强了函数的灵活性,因为它允许我们提供默认行为,同时仍然可以通过参数值进行定制。

Conclusion

结论

While arguments and parameters are closely related and often used together, they have distinct roles and purposes in programming. Arguments are the actual values passed to a function, while parameters are the variables that receive those values. Parameters are declared in the function signature, while arguments are provided when calling the function. Understanding the difference between these two concepts is essential for writing clean, reusable, and flexible code.

虽然参数值(Argument)和参数(Parameter)密切相关且经常一起使用,但它们在编程中有着不同的角色和目的。参数值是传递给函数的实际值,而参数是接收这些值的变量。参数在函数签名中声明,而参数值在调用函数时提供。理解这两个概念之间的区别对于编写简洁、可重用且灵活的代码至关重要。


Difference between an argument and a parameter

实参和形参之间区别

As a programmer, we have probably encountered the terms argument and parameter many times. They are often used interchangeably, but they both have different meanings. In this post, we will explain the difference between an argument and a parameter.

作为一名程序员,我们很可能已经多次遇到过 “argument(实参)” 和 “parameter(形参)” 这两个术语。它们常常被互换使用,但它们的含义其实是不同的。在这篇文章中,我们将解释实参和形参之间的区别。

1. What is an Argument?

什么是实参?

An argument is a value that we pass to a function or a method when we call it. An argument can be a constant, a variable, an expression, or another function. Arguments represent the actual values supplied to a function. For example, in the following code, we call the function pow() with two arguments: 2 and 3.

#include <stdio.h>
#include <math.h>

int main() {
    int result = pow(2, 3); // 2 and 3 are arguments
    printf("The result is %d\n", result);
    return 0;
}

值得注意的是,变量也可以作为实参。如果函数按如下方式调用,那么变量 xy 就是 pow() 函数的实参,而不是 23 这两个值。

#include <stdio.h>
#include <math.h>

int main() {
    int x = 2;
    int y = 3;
    int result = pow(x, y); // x and y are arguments
    printf("The result is %d\n", result);
    return 0;
}

根据编程语言和函数定义的不同,实参可以以不同的方式传递给函数。一些常见的方式有:

  1. By value: The argument value is copied to the corresponding parameter in the function. Any changes made to the parameter do not affect the argument.
    按值传递:实参的值会被复制到函数中对应的形参。对形参所做的任何更改都不会影响实参。

  2. By reference: The argument is passed as a reference to the corresponding parameter in the function. Any changes made to the parameter affect the argument as well.
    按引用传递:实参作为对函数中对应形参的引用进行传递。对形参所做的任何更改也会影响实参。

  3. By name: The argument is passed as a name or an expression to the corresponding parameter in the function. The argument is evaluated only when it is used in the function.
    按名称传递:实参作为名称或表达式传递给函数中对应的形参。实参仅在函数中使用时才会被求值。

2. What is a Parameter?

什么是形参?

A parameter is a variable that is declared in the function or method definition. A parameter can have a name, a type, and a default value. Parameters are also known as formal parameters because they represent the formal names that are used in the function definition. For example, consider the following function definition: Here, the add() function takes two parameters, x and y. If the function is called as add(2, 3), then 2 and 3 are the arguments.

int add(int x, int y) {
    return x + y;
}

区分实参和形参最简单的方法是看它们在代码中的使用位置。实参用于函数调用中,而形参用于函数定义中。实参是传递给函数的值,而形参是接收这些值的变量。

The easiest way to distinguish between an argument and a parameter is to look at where they are used in the code. Arguments are used in the function call, while parameters are used in the function definition. Arguments are the values that are passed to the function, while parameters are the variables that receive the values.

3. Key Points

要点

  1. A parameter is also called a formal parameter or formal argument, and an argument is often called actual arguments or actual parameters.
    形参也被称为 “形式参数” 或 “形式实参”,而实参通常被称为 “实际参数” 或 “实际实参”。
  2. A parameter is optional in some programming languages, and some programming languages allow for a default argument to be provided in a function’s declaration. This means that while calling the function, the caller can omit that argument.
    在某些编程语言中,形参是可选的,并且一些编程语言允许在函数声明中提供默认实参。这意味着在调用函数时,调用者可以省略该实参。
  3. A parameter has a name, a data type, and a calling mechanism (call by reference or call by value). In contrast, an argument is an expression that does not have any name, but it can be a variable, a constant, or a literal.
    形参有名称、数据类型和调用机制(按引用调用或按值调用)。相比之下,实参是一个没有任何名称的表达式,但它可以是变量、常量或字面值。
  4. The scope of a parameter is the function itself, and it serves as a local variable inside the function.
    形参的作用域是函数本身,它在函数内部充当局部变量。
  5. The number of arguments in a function call should match the total number of parameters in the function definition. One exception to this rule is functions with variable-length parameter lists.
    函数调用中的实参数量应该与函数定义中的形参总数匹配。这条规则的一个例外是具有可变长度参数列表的函数。
  6. The data type of arguments in a function call should match with the data type of parameters in the function definition.
    函数调用中实参的数据类型应该与函数定义中形参的数据类型匹配。
  7. If a parameter is passed by value, modifying it inside the function does not change the corresponding argument in the caller. However, if it is passed by reference, the values of the corresponding argument can be changed in the caller.
    如果形参是按值传递的,在函数内部修改它不会改变调用者中对应的实参。但是,如果是按引用传递的,调用者中对应实参的值可以被修改。

That’s all about the difference between an argument and a parameter.

以上就是关于实参和形参之间区别的所有内容。


The Difference Between an Argument and a Parameter

Last updated: September 29, 2022
Written by: baeldung

1.Overview

概述

Many programmers use the terms argument and parameter interchangeably, although they have different meanings. Hence, we’ll look at the difference between an argument and a parameter in this tutorial.
许多程序员会互换使用 “参数(argument)” 和 “形参(parameter)” 这两个术语,尽管它们含义不同。因此,在本教程中,我们将探讨参数和形参之间的区别。

2. Parameters and Arguments

参数和形参

Let’s see a pseudocode example to show clear definitions of the terms parameter and argument. In fact, a method or function in this program takes two numbers as inputs and outputs the sum of those values:
让我们通过一个伪代码示例来明确 “参数” 和 “形参” 这两个术语的定义。实际上,在这个程序中,一个方法或函数将两个数字作为输入,并输出这些值的和:

algorithm Sum(a, b):
    // INPUT
    //a, b = 要相加的数字
    // OUTPUT
    // 返回 a 和 b 的和
    Sum <- a + b
    return Sum
algorithm Main():
    // OUTPUT
    // 打印 Sum 的结果
    a <- 5
    b <- 10
    result <- Sum(a, b)
    print result

2.1. Parameters

The parameters are the variables that we can define in the function declaration. In fact, we utilized these variables within the function. Also, the programming language in the function description determines the data type specification. These variables facilitate the function’s entire execution. In addition, they are known as local variables because they are only available within the function.

形参是我们在函数声明中定义的变量。实际上,我们在函数内部使用这些变量。此外,函数描述中的编程语言决定了数据类型的指定。这些变量有助于函数的整个执行过程。而且,它们被称为局部变量,因为它们仅在函数内部可用。

Now, let’s understand these variables in detail with the help of the example above. The function Sum(a,b) contains two values inside the parenthesis. a and b So. a and b are the parameters. In fact, these are two local variables, with a lifetime limited to the function. They can also take any values that are given to the function when it is called.

现在,让我们借助上面的示例详细了解这些变量。函数 Sum(a, b) 的括号内包含两个值 ab。所以,ab 是形参。实际上,它们是两个局部变量,其生命周期仅限于该函数。在函数被调用时,它们也可以接受赋予该函数的任何值。

2.2. Arguments

The arguments are the variables given to the function for execution. Besides, the local variables of the function take the values of the arguments and therefore can process these parameters for the final output.

参数是传递给函数用于执行的变量。此外,函数的局部变量获取参数的值,从而可以处理这些参数以获得最终输出。

Now, let’s look at the function calling in the pseudocode above. So, at the end of the pseudocode, we invoked the function Sum. In other words, the arguments are the real values that we give as input to get the desired output. So, in our example, we choose 5 and 10 as arguments, then the obtained output will be 15.

现在,让我们看一下上面伪代码中的函数调用。在伪代码的末尾,我们调用了函数 Sum。换句话说,参数是我们作为输入给出以获得期望输出的实际值。所以,在我们的示例中,我们选择 5 和 10 作为参数,那么得到的输出将是 15。

3. The Difference Between an Argument and a Parameter

参数和形参之间的区别

When building functions, we can make certain inputs in order to process the instructions included in the function. In this context, we frequently use the terms “arguments” and “parameters” interchangeably to refer to these inputs. To clarify, we take a look at the differences to see which to use in which situation. Let’s now summarize the main differences between arguments and parameters:
在构建函数时,我们可以进行某些输入,以便处理函数中包含的指令。在这种情况下,我们经常互换使用 “参数(arguments)” 和 “形参(parameters)” 这两个术语来指代这些输入。为了明确,我们来看一下它们的区别,以便了解在何种情况下使用哪个术语。现在,让我们总结一下参数和形参之间的主要区别:

We used the variables in the function to send the value of the calling function to the receiving function.
我们在函数中使用变量,将调用函数的值发送到接收函数。
We defined the parameters when defining the function.
我们在定义函数时定义形参。
We used the arguments to transfer values from the calling function to the receiving function in function call statements.
我们在函数调用语句中使用参数将值从调用函数传递到接收函数。
To get the value from the arguments, we utilized the local variables in the function declaration.
为了从参数中获取值,我们在函数声明中使用局部变量。
We always assigned each argument to the parameter in the function declaration at the moment of the call.
在调用时,我们总是将每个参数分配给函数声明中的形参。
When we invoked the function, the values of the arguments are assigned to the local variables.
当我们调用函数时,参数的值被分配给局部变量。
Actual Parameters is another name for them.
它们也被称为 “实际参数”。
They’re sometimes referred to as Formal Parameters.
它们有时被称为 “形式参数”。

4. Conclusion

结论

In this tutorial, we discussed the main difference between arguments and parameters. We demonstrated when and how they are used.

在本教程中,我们讨论了参数和形参之间的主要区别。我们展示了它们何时以及如何被使用。


“Argument”与“Parameter”的区别

1. 定义

  • Parameter(参数)
    参数是函数或方法声明中定义的变量,位于函数名后面的括号内。它是函数的“输入接口”,用于接收调用时传入的值,定义了函数需要哪些数据来完成其操作。参数是抽象的、固定的,是函数内部使用的变量。

  • Argument(参数值)
    参数值是在调用函数时传递给参数的实际值。它是具体的输入数据,用于填充函数定义中的参数,为函数提供实际的输入值,用于执行函数的逻辑。参数值是实际的、具体的,是传递给函数的“具体输入”。

2. 位置与作用

参数(Parameter)的作用

  • 定义函数的输入需求
    参数定义了函数需要哪些数据来完成其操作,明确了函数需要接收的变量类型和数量。例如,函数 add(a, b) 定义了两个参数 ab,表明函数需要两个输入值来完成加法操作。

  • 在函数内部处理数据
    参数是函数内部的变量,用于存储从外部传入的数据,并在函数内部进行操作以完成其逻辑。例如,在函数 add(a, b) 中,参数 ab 被用于执行加法操作 a + b

  • 提供灵活性和通用性
    参数使得函数能够处理不同的输入数据,而无需为每种情况编写单独的代码。这提高了函数的通用性和复用性。例如,同一个 add 函数可以用于计算 add(1, 2)add(3, 4),而无需编写两个不同的函数。

参数值(Argument)的作用

  • 提供具体的输入数据
    参数值是调用函数时传递给参数的实际值,是函数执行逻辑的具体输入,使得函数能够根据这些数据进行操作。例如,在调用 add(3, 4) 时,34 是参数值,用于填充函数定义中的参数 ab

  • 触发函数的执行
    参数值的传递是函数执行的触发条件。只有当具体的参数值被传递给函数时,函数才会开始执行其逻辑。例如,调用 add(3, 4) 时,函数接收到参数值 34 后,才会执行加法操作并返回结果。

  • 影响函数的行为
    参数值的不同会导致函数的行为不同。通过传递不同的参数值,可以控制函数的输出结果。例如,调用 add(1, 2)add(3, 4) 会得到不同的结果,因为参数值不同。

3. 数据流向与协同作用

在函数调用过程中,参数和参数值是协同工作的:

  • 参数值(Argument):是调用函数时提供的“具体数据”,用于填充函数定义中的参数。
  • 参数(Parameter):是函数内部的“占位符”,用于接收从外部传入的数据。

数据从参数值流向参数,最终在函数内部被处理。其流程可以表示为:

调用函数时:
参数值(Argument) --> 参数(Parameter) --> 函数内部处理

4. 示例

假设有一个函数 multiply,用于计算两个数的乘积:

def multiply(a, b):  # a 和 b 是参数(Parameter)
    return a * b

调用这个函数时:

result = multiply(5, 6)  # 5 和 6 是参数值(Argument)
  • 参数(Parameter)ab 是函数定义中的变量,用于接收调用时传入的值。它们定义了函数需要两个输入值,并在函数内部用于执行乘法操作。
  • 参数值(Argument)56 是调用函数时提供的具体值,用于填充参数 ab,使得函数能够执行乘法操作并返回结果。

5. 调用机制

按值调用(Call-by-Value)

仅传递参数值的副本,函数内部对参数的修改不会影响外部变量。
示例:

void increment(int num) {
    num++;
}
int main() {
    int a = 5;
    increment(a);  // a 的值仍为 5
    return 0;
}

按引用调用(Call-by-Reference)

传递参数值的引用,函数内部对参数的修改会影响外部变量。
示例(C++):

void increment(int& num) {
    num++;
}
int main() {
    int a = 5;
    increment(a);  // a 的值变为 6
    return 0;
}

6. 不同语言中的定义

  • C/C++

    • 参数(Parameter):函数声明中的变量,也称为形式参数(Formal Argument)。
    • 参数值(Argument):函数调用时传递的值,也称为实际参数(Actual Argument)。
  • Java

    • 参数(Parameter):方法声明中的变量列表。
    • 参数值(Argument):调用方法时传递的实际值。
  • Python

    • 参数(Parameter):函数定义中的变量。
    • 参数值(Argument):调用函数时传递的值,支持关键字参数和可变参数。
  • 数学中的区别
    在数学中,“argument”(自变量)更为常见,表示函数的输入,如 f ( x ) f(x) f(x)中的 x x x。而“parameter”(参数)通常表示定义函数族或几何形状的变量,如参数方程中的 t t t

7. 实际应用中的注意事项

术语混淆

在非正式场合,参数(parameter)和参数值(argument)可能会被误用或混用。正确使用术语可以避免误解,尤其是在讨论函数逻辑和值传递时。

错误处理与调试

在调试时,检查传递给函数的参数值类型和值是否正确。在错误处理中,参数值可用于提供错误上下文,帮助定位问题。

8. 特殊情况

函数重载(Overloading)

在支持函数重载的语言(如 Java 和 C++)中,参数列表用于区分同名函数。调用时,根据参数值的数量、类型和顺序选择正确的重载函数。

总结

  • 参数(Parameter):是函数定义中的变量,是抽象的、固定的,用于接收输入。
  • 参数值(Argument):是调用函数时传递的具体值,是实际的、具体的,用于填充参数。
  • 记忆方法:参数值是具体的,参数是抽象的;参数值在外,参数在内。
  • 语言差异:不同语言对参数和参数值的定义和使用方式可能有所不同,需根据具体语言规范理解。
  • 实际应用:正确区分参数和参数值有助于清晰表达程序逻辑,避免混淆和错误。

via:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值