C++ 自学教程 第1.4a章 初识函数参数与参量
–
前言:连续几节都是first look什么什么, 我要编不下去“初”字后面跟什么了。。
–
函数的参数与参量
在前面的章节,我们学习了函数可以向调用它的函数返回一个数值。
在许多情况下,我们希望将一些信息传递给被调用的函数,以便以其进一步处理。比方说,如果我们想要写一个能够对两个数求和的函数,我们就需要告诉这个函数我们想要求哪两个数的和。不然的话它怎么知道求什么的和呢?这个功能可以通过函数的参数与参量来实现。
函数的参数(parameter)是一个变量,用于储存呼叫函数传递来的数值。函数参数被放在函数名后面的括号内。如果有多个参数,它们之间可以用逗号分隔。
下面几个函数有不同数目的参数:
// This function takes no parameters
// It does not rely on the caller for anything
//这个函数没有参数
void doPrint()
{
std::cout << "In doPrint()" << std::endl;
}
// This function takes one integer parameter named x
// The caller will supply the value of x
//这个函数有一个整数参数x
void printValue(int x)
{
std::cout << x << std::endl;
}
// This function has two integer parameters, one named x, and one named y
// The caller will supply the value of both x and y
//这个函数有两个参数x, 和y
int add(int x, int y)
{
return x + y;
}
每个函数的参数只在这个函数内有效。所以即使printValue()和add()都有一个叫做x的参数,它们之间并不矛盾。
参量(argument)是在函数被调用时具体输入到它的参数里的数值。
printValue(6); // 6 is the argument passed to function printValue()
add(2, 3); // 2 and 3 are the arguments passed to function add()
注意参量之间用逗号分隔。参量的数量必须与参数的数量一致。不然编译器会报错
协同合作的参数与参量
当函数被调用时,函数的所有参数都被生成为变量,而且每一个参量的数值都被复制到它对应的参数中。这个过程叫做值传递(pass by value)。
比方说:
//#include "stdafx.h" // Visual Studio users need to uncomment this line
#include <iostream>
// This function has two integer parameters, one named x, and one named y
// The values of x and y are passed in by the caller
void printValues(int x, int y)
{
std::cout << x << std::endl;
std::cout << y << std::endl;
}
int main()
{
printValues(6, 7); // This function call has two arguments, 6 and 7
return 0;
}
printValue() 以参量6和7被调用,首先生成printValue的参数x,并被赋值为6,printValue参数y被生成,并被赋值为7.
输出结果如下:
6
7
参数和返回值的互动
通过共同使用参数和返回值,我们能够创建: 接受数据输入,完成一些计算,并向呼叫函数返回数值的函数。
下面是一个非常简单的函数,它将两个数字相加并把结果返回。
//#include "stdafx.h" // Visual Studio users need to uncomment this line
#include <iostream>
// add() takes two integers as parameters, and returns the result of their sum
// The values of x and y are determined by the function that calls add()
int add(int x, int y)
{
return x + y;
}
// main takes no parameters
int main()
{
std::cout << add(4, 5) << std::endl; // Arguments 4 and 5 are passed to function add()
return 0;
}
当函数add()被调用,参数x被赋值为4,参数y被赋值为5。
函数add()下面估值x+y,得到数值9,并把它放回给主函数。数值9被发送给cout并输出到屏幕上。
输出:
9
用图片表示:
更多例子
我们来看看别的例子:
//#include "stdafx.h" // Visual Studio users need to uncomment this line
#include <iostream>
int add(int x, int y)
{
return x + y;
}
int multiply(int z, int w)
{
return z * w;
}
int main()
{
std::cout << add(4, 5) << std::endl; // within add(), x=4, y=5, so x+y=9
std::cout << multiply(2, 3) << std::endl; // within multiply(), z=2, w=3, so z*w=6
// We can pass the value of expressions
std::cout << add(1 + 2, 3 * 4) << std::endl; // within add(), x=3, y=12, so x+y=15
// We can pass the value of variables
int a = 5;
std::cout << add(a, a) << std::endl; // evaluates (5 + 5)
std::cout << add(1, multiply(2, 3)) << std::endl; // evaluates 1 + (2 * 3)
std::cout << add(1, add(2, 3)) << std::endl; // evaluates 1 + (2 + 3)
return 0;
程序的输出:
9
6
15
10
7
6
前面两个叙述比较简单直接。
第三个叙述,这个参数是首先被估值的表达式。在这个例子里,1+2估值得3,所以3被赋值给了x。3*4估值为12,所以12被赋值给了y。add(3,12)最终得15。
下面一段叙述一样简单:
int a = 5;
std::cout << add(a, a) << std::endl; // evaluates (5 + 5)
在这个例子里,add()被以x=a,y=a调用。由于a=5, add(a,a) = add(5,5), 所以得10。
下面来看看第一个有点难度的叙述:
std::cout << add(1, multiply(2, 3)) << std::endl; // evaluates 1 + (2 * 3)
当函数add()被执行的时候,CPU需要知道参数x和y的数值是多少。估值x很简单,因为我们输入了整数1,所以赋值x=1。为了得到y的数值,CPU需要先估值multiply(2,3)。CPU赋值z=2, 和w=3,所以multiply(2,3)返回数值6。返回值6现在能被发送给了add函数的参数y。add(1,6)返回整数7, 然后被发送给cout然后输出。
用更简练的方法表达的话(使用=>符号表达估值):
add(1, multiply(2, 3)) => add(1, 6) => 7
下面这行叙述看上去有点绕,因为其中一个给定的参数也调用了函数add()。
std::cout << add(1, add(2, 3)) << std::endl; // evaluates 1 + (2 + 3)
但是其实跟上面的例子非常相似。
在CPU能够输出add()的最终值之前,它必须先计算内部调用add(2,3)。add(2,3)估值为5。然后才能计算add(1,5)的 结果,也是6, 也就是cout最后得到的数值。
换个表达方式:
add(1, add(2, 3)) => add(1, 5) => 6
小结
参数的存在使得函数能够被重复使用。因为它使得函数可以在不知道具体输出的情况下执行一些任务。呼叫函数以参量的形式传入具体的输入数值。
返回值使得函数可以将数值返回给呼叫函数。
小测验
1) 下面的程序哪里错了?
#include <iostream>
void multiply(int x, int y)
{
return x * y;
}
int main()
{
std::cout << multiply(4, 5) << std::endl;
return 0;
}
2) 下面的程序有那两处错误?
#include <iostream>
int multiply(int x, int y)
{
int product = x * y;
}
int main()
{
std::cout << multiply(4) << std::endl;
return 0;
}
3) 下面的程序输出什么?
#include <iostream>
int add(int x, int y, int z)
{
return x + y + z;
}
int multiply(int x, int y)
{
return x * y;
}
int main()
{
std::cout << multiply(add(1, 2, 3), 4) << std::endl;
return 0;
}
4) 写一个叫做doubleNumber() 的函数,接受一个整数参数,并输出的它的两倍。
5) 写一个完整的程序:获得用户的整数输入,并使用上面编写的doubleNumber求得它的两倍,并把最后结果输出到屏幕上。
答案:
1) 函数的返回值是空,但却有返回叙述
2) 问题1,主函数向multiply传递了一个参量,但multiply需要两个。
问题2. Multiply计算了结果并把它赋值给了一个变量,但是没有返回给呼叫函数。但是由于函数返回值类型是int,也就是必须要返回一个整数,这会导致编译错误。
3) 24
4)
int doubleNumber(int x)
{
return 2 * x;
}
5)
#include <iostream>
int doubleNumber(int x)
{
return 2 * x;
}
int main()
{
int x;
std::cin >> x;
std::cout << doubleNumber(x) << std::endl;
return 0;
}
/*
// The following is an alternate way of doing main:
int main()
{
int x;
std::cin >> x;
x = doubleNumber(x);
std::cout << x << std::endl;
return 0;
}
*/