Cprogramming 简明教程
Function Call by Value in C
在 C 语言中,可以从任何其他函数(包括自身)调用函数。有两种方法可以调用函数:(a) 按值调用和 (b) 按引用调用。默认情况下,使用按值调用机制。
In C language, a function can be called from any other function, including itself. There are two ways in which a function can be called − (a) Call by Value and (b) Call by Reference. By default, the Call by Value mechanism is employed.
Formal Arguments and Actual Arguments
您必须了解术语才能了解按值调用方法是如何工作的:
You must know the terminologies to understand how the call by value method works −
Formal arguments - 函数需要某些数据来执行其所需的进程。定义函数时,假定数据值将以参数或参数列表的形式提供,该列表位于函数名称前面的括号中。这些参数是某个数据类型的变量。
Formal arguments − A function needs certain data to perform its desired process. When a function is defined, it is assumed that the data values will be provided in the form of parameter or argument list inside the parenthesis in front of the function name. These arguments are the variables of a certain data type.
Actual arguments - 当要调用某个函数时,它应该提供所需数量的值,其类型与在函数定义中使用的类型相同,并且按相同的顺序提供。
Actual arguments − When a certain function is to be called, it should be provided with the required number of values of the same type and in the same sequence as used in its definition.
看一看以下片段:
Take a look at the following snippet −
type function_name(type var1, type var2, ...)
这里,参数变量被称为 formal arguments 。在函数的作用域内,这些变量作为其 local variables 。
Here, the argument variables are called the formal arguments. Inside the function’s scope, these variables act as its local variables.
考虑以下函数:
Consider the following function −
int add(int x, int y){
int z = x + y;
return z;
}
此函数定义中的参数 x 和 y 是形式参数。
The arguments x and y in this function definition are the formal arguments.
Example: Call by Value in C
如果调用了 add() 函数,如下面的代码所示,那么括号内的变量 ( a 和 b ) 是实际参数。它们被传递给函数。
If the add() function is called, as shown in the code below, then the variables inside the parenthesis (a and b) are the actual arguments. They are passed to the function.
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int add(int x, int y){
int z = x + y;
return z;
}
int main(){
int a = 10, b = 20;
int c = add(a, b);
printf("Addition: %d", c);
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
Addition: 30
按值传递方法表示实际参数的值被复制到形式参数变量中。因此,“x”取“a”的值,而“y”被赋值给“b”。add() 函数内部的局部变量“z”存储加法值。在 main() 函数中,由 add() 函数返回的值被赋值给“c”,然后打印出来。
The Call by Value method implies that the values of the actual arguments are copied in the formal argument variables. Hence, "x" takes the value of "a" and "b" is assigned to "y". The local variable "z" inside the add() function stores the addition value. In the main() function, the value returned by the add() function is assigned to "c", which is printed.
请注意,C 语言中的变量是内存中的一个命名位置。因此,变量在内存中创建,并且编译器为每个变量分配一个随机内存地址。
Note that a variable in C language is a named location in the memory. Hence, variables are created in the memory and each variable is assigned a random memory address by the compiler.
How Does "Call by Value" Work in C?
让我们假设 main() 函数中的变量 a 、 b 和 c 分别占据内存位置 100、200 和 300。当使用 a 和 b 作为实际参数调用 add() 函数时,它们的值分别存储在 x 和 y 中。
Let us assume that the variables a, b and c in the main() function occupy the memory locations 100, 200 and 300 respectively. When the add() function is called with a and b as actual arguments, their values are stored in x and y respectively.
变量 x 、 y 和 z 是 add() 函数的局部变量。在内存中,将为它们分配一些随机位置。我们假设它们分别在内存地址 1000、2000 和 3000 中创建。
The variables x, y, and z are the local variables of the add() function. In the memory, they will be assigned some random location. Let’s assume that they are created in memory address 1000, 2000 and 3000, respectively.
由于函数通过将实际参数的值复制到其相应的形式参数变量来调用,因此 x 和 y 的地址 1000 和 2000 分别容纳 10 和 20。编译器将它们的加法赋值给他们返回的第三个局部变量 z 。
Since the function is called by copying the value of the actual arguments to their corresponding formal argument variables, the locations 1000 and 2000 which are the address of x and y will hold 10 and 20, respectively. The compiler assigns their addition to the third local variable z which is returned.
当控制权返回 main() 函数时,返回的数据被赋值给 c ,并显示为程序的输出。
As the control comes back to the main() function, the returned data is assigned to c, which is displayed as the output of the program.
Example
按值传递是 C 语言中的默认函数调用机制。它消除了函数的潜在副作用,使你的软件更容易维护和理解。它最适合于期望对接收到的参数执行特定计算并返回结果的函数。
Call by Value is the default function calling mechanism in C. It eliminates a function’s potential side effects, making your software simpler to maintain and easy to understand. It is best suited for a function expected to do a certain computation on the argument received and return the result.
以下按值传递的另一个示例:
Here is another example of Call by Value −
#include <stdio.h>
/* function declaration */
void swap(int x, int y);
int main(){
/* local variable definition */
int a = 100;
int b = 200;
printf("Before swap, value of a: %d\n", a);
printf("Before swap, value of b: %d\n", b);
/* calling a function to swap the values */
swap(a, b);
printf("After swap, value of a: %d\n", a);
printf("After swap, value of b: %d\n", b);
return 0;
}
void swap(int x, int y){
int temp;
temp = x; /* save the value of x */
x = y; /* put y into x */
y = temp; /* put temp into y */
return;
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
Before swap, value of a: 100
Before swap, value of b: 200
After swap, value of a: 100
After swap, value of b: 200
它表明值没有发生变化,尽管它们在函数内部发生改变。
It shows that there are no changes in the values, although they had been changed inside the function.
由于值被复制到另一个函数的不同局部变量中,因此任何操作都不会对调用函数中的实际参数变量产生任何影响。
Since the values are copied in different local variables of another function, any manipulation doesn’t have any effect on the actual argument variables in the calling function.
然而,当我们需要将大型对象(如数组或文件)传递给另一个函数时,按值传递方法的效率较低。另外,在某些情况下,我们可能需要另一个函数操作实际参数。在这些情况下,按值传递机制不起作用。我们必须为此目的探索按引用传递机制。请参阅下一章以获取有关 C 语言按引用传递机制的详细解释。
However, the Call by Value method is less efficient when we need to pass large objects such as an array or a file to another function. Also, in some cases, we may need the actual arguments to be manipulated by another function. In such cases, the Call by Value mechanism is not useful. We have to explore the Call by Reference mechanism for that purpose. Refer the next chapter for a detailed explanation on the Call by Reference mechanism of C.
按引用传递方法涉及传递包含实际参数值的变量的地址。你可以设计一个调用方法,它是按值传递和按引用传递的混合。在这种情况下,一些参数按值传递,而另一些按引用传递。
The Call by Reference approach involves passing the address of the variable holding the value of the actual argument. You can devise a calling method that is a mix of Call by Value and Call by Reference. In this case, some arguments are passed by value and others by reference.