3.值传递
*函数调用时实参将数值传入给形参
*如果函数不需要返回值,声明的时候可以写void
void swap(int num1,int num2){
//返回值不需要的时候可以不写return
}
*当我们做值传递时,函数的形参发生改变,并不会影响实参
4.函数的常见样式
*无参无反
*void test01(){ cout << "this is test01" << endl; }
*有参无反
*void test02(int a){ cout << "this is test02 a = "<< a << endl; }
*无参有反
*int test03(){ cout << "this is test03" << endl; return 1000; //返回值1000 }
*有参有返
*int test04(int a){ cout << "this is test04 a=" << a << endl; return a; }
5.函数的声明
*提前告诉编译器函数的存在,可以利用函数的声明
*声明可以有多次,定义只能有一次
*int max(int a,int b); //声明 int main(){ ... } int max(int a,int b){ //定义 ... }
6.函数的分文件编写
*让代码结构更加清晰
1.创建后缀名为.h的头文件
2.创建后缀名为.cpp的源文件
3.在头文件中写函数的声明
4.在源文件中写函数的定义
*若要调用头文件,则在源文件中加#include "xxx.h" *头文件中记得加#include <iostream>