如果我们再开发程序的时候,某一段代码,需要执行很多次,但是为了提高编写的效率以及代码的复用,所以我们把这一段代码封装成一个模块,这个就是函数
1.函数的使用

#include<iostream>
using namespace std;
void swap(int num1, int num2) //括号里的表示形参
{
int temp = num1;
num1 = num2;
num2 = temp;
cout << num1 << endl;
cout << num2 << endl;
}
int main() {
int a = 4, b = 5;
swap(a, b);/*这是函数的调用,括号里的代表实参,输出5
4*/
cout << endl;
cout << a << " " << b;//结果4,5
/*实参将变量传给形参,但形参的改变不会影响实参*/
system("pause");
return 0;
}
2.函数的分文件编写
让代码更加清晰

头文件
#include<iostream>
using namespace std;
void swap(int num1, int num2);
自定义函数源文件
#include"helloworld.h"
void swap(int num1, int num2) {
int temp = num1;
num1 = num2;
num2 = temp;
cout << num1 << endl;
cout << num2 << endl;
}
有主函数的源文件,代码要运行,一定要有且只有一个源文件
#include"helloworld.h"
int main() {
int a = 4, b = 5;
swap(a, b);
cout << endl;
cout << a << " " << b;
system("pause");
return 0;
}