C++基础(三)函数

函数的声明

主要是用来解决函数写在后面 前面调用不到的情况

#include <iostream>
using namespace std;

//函数的声明
int max(int a, int b);
int main()
{
	int a = 5;
	int b = 6;
	int res = max(a,b);
	cout << res << endl;
	return 0;
}

int max(int a, int b) 
{
	return a > b ? a : b;
}

函数的分文件编写

1.创建.h的后缀名的头文件(.h)在该文件中中写函数的声明   

    同样的在该头文件中引入源文件所需的函数库   例:

    #include <iostream>

    using namespace std; 

2.创建.cpp后缀名的源文件(.cpp)在该写函数的定义

    在.cpp源文件中引入该文件的头文件(.h)    #include "swap.h"

3.在将要引入的(例如main函数)文件中引入目标头文件(.h)     #include "swap.h"

猜你喜欢

转载自blog.csdn.net/we1less/article/details/108563345