函数的参数
交换算法
输入两个整数并交换
#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;
swap(x,y);
cout<<"x="<<x<<" y="<<y<<endl;
return 0;
}
引用传递:&使得a是x的别名,a就是x
- 定义时必须马上初始化,且不能再改变
for:范围的for循环,定义容器,依次遍历所有元素
不能重复定义默认参数值
函数的内联、重载和系统函数的调用
内联函数
内联函数的应用举例
#include<iostream>
using namespace std;
const double PI=3.14159265358979;
inline double calArea(double radius){
return PI*radius*radius;
}
int main(){
double r=3.0;
double area=calArea(r);
cout<<area<<endl;
}
函数的重载
C语言中不支持
重载函数的参数类型或个数不同
系统函数