C++函数(二)
1.函数的参数传递
(1)在函数被调用时才分配形参的存储单元
(2)实参可以是常量、变量或表达式
(3)实参类型必须与形参相符
(4)值传递是传递参数值,即单项传递
(5)引用传递可以实现双向传递
(6)常引用作参数可以保障实参数据的安全
2.引用类型
引用类型:
引用(&)是标识符的别名
例如:
int i,j;
int &ri=i;//定义int引用ri,并初始化为变量i的引用
j=10
ri=j;//相当于i=j
一旦一个引用被初始化后,就不能改为指向其他对象
引用可以i作为形参
例:输入两个整数交换后输出(值传递)
#include"iostream"
using namespace std;
void swap(int a,int b){
int t=a;
a=b;
b=t;
}
int main(){
int x=5,y=10;
cout<<"x="<<x<<"y="<<y<<endl;
swap(x,y);
cout<<"x="<<x<<"y="<<y<<endl;
return 0;
}
运行结果:
x=5 y=10
x=5 y=10
3.含有可变参数函数
c++标准中提供了两种主要方法
如果所有实参类型相同,可以传递一个名为initializer_list的标准类型库
如果实参的类型不同,可以编写可变参数的模板(第九章)
initializer_list
initializer_list是标准库类型,用于表示某种特定类型值的数组,该类型定义在同名的头文件中4
例:计算长方体的体积
函数getVolume计算体积
由三个形参:length(长)、width(宽)、height(高)其中width和height带有默认值2和3
主函数中以不同形式调用getVolume函数
#include"iostream"
#include"iomanip"
using namespace std;
int getVolume(int length, int width = 2, int height = 3);
int main() {
const int X = 10, Y = 12, Z = 15;
cout << "Some box data is:";
cout << getVolume(X, Y, Z) << endl;
cout << "Some box data is:";
cout << getVolume(X, Y) << endl;
cout << "Some box data is:";
cout << getVolume(X) << endl;
return 0;
}
int getVolume(int length, int width, int height) {
cout << setw(5) << length << setw(5) << width << setw(5)
<< height << '\t';
return length * width * height;
}
运行结果:
Some box data is: 10 12 15 1800
Some box data is: 10 12 3 360
Some box data is: 10 2 3 60
例题:编写递归函数int fib(int n),在主程序中输入n的值,调用fib函数计算Fibonacci级数。
公式为:
fib(n)=fib(n-1)+fib(n-2),n>2;
fib(1)=fib(2)=1
#include"iostream"
using namespace std;
int fib(int n);
int main()
{
int n, answer;
cout << "Enter number:";
cin >> n;
cout << "\n\n";
answer = fib(n);
cout << answer << "is the " << n << "th Fibonacci number\n";
return 0;
}
int fib(int n) {
cout << "Processing fib(" << n << ")...";
if (n < 3)
{
cout << "Return 1!\n";
return (1);
}
else {
cout << "Call fib(" << n - 2 << ")and fib(" << n - 1 << ")).\n";
return (fib(n - 2) + fib(n - 1));
}
}
运行结果:
Enter number:4
Processing fib(4)...Call fib(2)and fib(3)).
Processing fib(2)...Return 1!
Processing fib(3)...Call fib(1)and fib(2)).
Processing fib(1)...Return 1!
Processing fib(2)...Return 1!
3is the 4th Fibonacci number
4.函数重载
c++允许功能近似的函数在相同作用域内以相同函数名声明,从而形成重载。
形参类型不同:
int add(int x,int y);
float add(float x,float y);
形参个数不同:
int add(int x,int y);
int add(int x,int y,int z);
例题:编写两个名为sumOfSquare的重载函数,分别求两个整数的平方和以及连个实数的平方和
#include"iostream"
using namespace std;
int sumOfSquare(int a, int b) {
return a * a + b * b;
}
double sumOfSquare(double a, double b) {
return a * a + b * b;
}
int main()
{
int m, n;
cout << "Enter two integer:";
cin >> m >> n;
cout << "Their sum of square:" << sumOfSquare(m, n) << endl;
double x, y;
cout << "Enter two integer:";
cin >> x >> y;
cout << "Their sum of square:" << sumOfSquare(x, y) << endl;
return 0;
}
运行结果:
Enter two integer:5 6
Their sum of square:61
Enter two integer:3.2 2.3
Their sum of square:15.53
5.c++系统函数
C++的系统库中提供了几百个函数可供程序员使用,例如:
求平方根函数(sprt)
求绝对值函数(abs)
使用系统函数是要包含相应的头文件,例如:
cmath
eger:5 6
Their sum of square:61
Enter two integer:3.2 2.3
Their sum of square:15.53
## 5.c++系统函数
**C++的系统库中提供了几百个函数可供程序员使用,例如:**
求平方根函数(sprt)
求绝对值函数(abs)
**使用系统函数是要包含相应的头文件,例如:**
cmath