作用:将一段经常使用的代码封装起来,减少重复代码
一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能
一、函数的定义及调用
函数的定义一般主要有5个步骤:
- 返回值类型
- 函数名
- 参数表列
- 函数体语句
- return表达式
语法:
返回值类型 函数名(参数列表)
{函数体语句;return 表达式;
}
1、实现一个加法的函数
功能:传入两个整型数据,计算数据相加的结果,并且返回
- 返回值类型
- 函数名
- 参数列表
- 函数体语句
- return表达式
#include<iostream>
#include<string.h>
using namespace std;int add(int num1, int num2) //形参num1,num2
{int sum = num1 + num2;return sum;
}int main()
{//函数的定义//语法://返回值类型 函数名 (参数列表) {函数体语句 return 表达式}//加法函数,实现两个整型相加,并且将相加的结果进行返回int a = 10; //实参int b = 20; //实参int sum = add(a, b); //调用函数cout << sum << endl;sum=add(1, 2); //调用函数cout << sum << endl;system("pause");return 0;
}
运行结果:
3
30
注:当调用函数时,实参的值回传递给形参
二、函数的值传递
#include<iostream>
#include<string.h>
using namespace std;void swap(int num1, int num2)
{cout << "交换前:" << endl;cout << "num1 = " << num1 << endl;cout << "num2 = " << num2 << endl;int temp = num1;num1 = num2;num2 = temp;cout << "交换后:" << endl;cout << "num1 = " << num1 << endl;cout << "num2 = " << num2 << endl;//return;返回值不需要的时候可以不写returnreturn;
}int main()
{//值传递//定义函数,实现两个数字进行交换函数//如果一个函数不需要返回值,声明时可以写voidint a = 10; //实参int b = 20; //实参cout << "a = " << a << endl;cout << "b = " << b << endl;//当我们做值传递的时候,函数的形参发生改变,并不会影响实参swap(a, b); //调用函数system("pause");return 0;
}
运行结果:
a = 10
b = 20
交换前:
num1 = 10
num2 = 20
交换后:
num1 = 20
num2 = 10
三、函数的常见样式
常见的函数样式有4种:
- 无参无返
- 有参无返
- 无参有返
- 有参有返
#include<iostream>
#include<string.h>
using namespace std;
//函数常见样式
//1、无参无返
void test01()
{cout << "this is test01" << endl;
}//2、有参无返
void test02(int a) //参数
{cout << "this is test02 a = " << a << endl;
}
//3、无参有返
int test03() //参数
{cout << "this is test03 " << endl;return 1000;
}//4、有参有返
int test04(int a)
{cout << "this is test04 a= " << a << endl;return a;
}int main()
{//1、无参无返函数调用test01();//2、有参无返函数调用test02(100);//3、无参有返函数调用int num1 = test03();cout << "num1 = " << num1 << endl;{cout << "this is test03" << endl;}//4、有参有返函数调用int num2 = test04(10000);cout << "num2 = " << num2 << endl;system("pause");return 0;
}
运行结果:
this is test01
this is test02 a = 100
this is test03
num1 = 1000
this is test03
this is test04 a= 10000
num2 = 10000
四、函数的声明
作用:告诉编译器去函数名称以及如何调用函数。函数的实际主体可以单独定义
函数的声明可以多次,但是函数的定义只能有一次
#include<iostream>
#include<string.h>
using namespace std;
//函数的声明
//比较函数,实现两个整型数字进行比较,返回较大的值
int max(int a, int b); //函数的声明int main()
{int a = 10;int b = 20;cout << max(a, b) << endl;system("pause");return 0;
}int max(int a, int b)
{return a > b ? a : b; //三目运算符
}
运行结果:
20
五、函数的文件编写
作用:让代码结构更加清晰
函数分文件编写一般有4个步骤:
- 创建后缀为.h的头文件
- 创建后缀名为.cpp的源文件
- 在头文件中写函数的声明
- 在源文件中写函数的定义
1、交换两个数字
创建swap.h头文件
#include<iostream>
#include<string.h>
using namespace std;
void swap(int a, int b); //函数的声明
创建swap.cpp源文件,此文件中写函数的定义
//函数的定义
#include "swap.h" //与头文件进行关联
void swap(int a, int b)
{int temp = a;a = b;b = temp;cout << "a = " << a << endl;cout << "b = " << b << endl;
}
主函数文件test.cpp
//函数的分文件编写
//实现两个数字进行交换的函数,不需要返回值//1、创建.h后缀名的头文件
//2、创建.cpp后缀名的源文件
//3、在头文件中写函数的声明
//4、在源文件中先函数的定义#include "swap.h" //使用函数需引用头文件
int main()
{int a = 10;int b = 20;swap(a, b);system("pause");return 0;
}