C++基础入门-06函数
6.1 概述
作用:将一段经常使用的代码封装起来,减少重复代码。
一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能。
6.2 函数的定义
6.2.1 函数定义的步骤
1、返回值类型;
2、函数名;
3、参数列表;
4、函数体语句;
5、return语句。
6.2.2 函数定义的语法结构
返回值类型 函数名(参数列表)
{
函数体语句;
return语句;
}
返回值类型:定义函数返回值(用return返回)的类型,如int型。
函数名:该所定义的函数起的名字。
参数列表:使用该函数时,传入的数据。
函数体语句:花括号内的代码,函数内需要执行的语句。
return语句:和函数返回值挂钩,函数执行完毕时,返回相应类型的数据。
6.2.3 函数定义示例
例子:定义一个函数,实现两个数相加,并且返回相加的和。
6.3 函数的调用
功能:使用定义好的函数。
语法:函数名(参数)
示例:
#include<iostream>
using namespace std;
//定义加法函数,并返回相加的和
int add(int num1, int num2)//函数定义的时候,num1和num2称为形式参数,简称形参
{
int sum = num1 + num2;
return sum;
}
//调用函数
//函数调用语法:函数名称(参数)
int main()
{
int a = 0, b = 0;
cout << "请输入a、b两个整数:";
cin >> a >> b;
int sum = 0;
sum = add(a, b);//这里a和b称为实际参数,简称实参
//当调用函数时,实参的值会传递给形参
cout << "a、b两个数之和为:" << sum << endl;
system("pause");
return 0;
}
输出结果:
总结:函数定义的时候,num1和num2称为形式参数,简称形参;函数调用的时候,a和b称为实际参数,简称实参;当调用函数时,实参的值会传递给形参。
6.4 值传递
1、值传递:函数调用时,实参将数值传入形参。
2、值传递时,如果形参发生变化,并不会影响实参。
例子:
#include<iostream>
using namespace std;
//值传递
//定义函数,实现两个数字之间进行交换的函数
//如果函数不需要返回值,声明的时候可以写void
void swap(int num1, int num2)
{
cout << "交换前:" << endl;
cout << "num1=:" << num1 << endl;
cout << "num2=:" << num2 << endl;
int temp = 0;
temp = num1;
num1 = num2;
num2 = temp;
cout << "交换后:" << endl;
cout << "num1=:" << num1 << endl;
cout << "num2=:" << num2 << endl;
}
int main()
{
int a = 10, b = 20;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
//当我们做值传递的时候,函数的形参变化并不会影响实参
swap(a, b);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
system("pause");
return 0;
}
输出结果:
总结:值传递时,形参是修饰不了实参的。
6.5 函数的常见格式
常见的函数样式有4种:无参无返、有参无返、无参有返和有参有返。
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;
}
注意:要留意函数定义时的返回值类型,没有返回值时写void,有返回值时要与return语句中的类型对应。
整体示例:
#include<iostream>
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()
{
//无参无返的函数调用
test01();
//有参无返的函数调用
test02(100);
//无参有返的函数调用
int num1 = test03();
cout << "num1=" << num1 << endl;
//有参有返的函数调用
int num2 = test04(2000);
cout << "num2=" << num2 << endl;
system("pause");
return 0;
}
6.6 函数的声明
作用:告诉编译器函数名称以及如何调用函数。函数的实际主体可以单独定义。
例子:
//函数声明(注意后面有一个分号)
int max(int a, int b);
注意:函数的声明可以是多次,但是函数的定义只能有一次。
示例:
#include<iostream>
using namespace std;
//提前告诉编译器的存在,可以利用函数声明
//函数声明
//声明可以多次,定义只能有一次
int max(int a, int b);
int main()
{
int a = 10, b = 20;
cout << max(a, b) << endl;
system("pause");
return 0;
}
//函数定义
//比较函数,实现两个整型数字进行比较,返回较大的值
int max(int a, int b)
{
return a > b ? a : b;
}
6.7 函数的分文件编写
作用:让代码结构更加清晰。
函数分文件编写的步骤:
1、创建后缀名为.h的头文件;
2、创建后缀名为.cpp的源文件;
3、在头文件中写函数的声明;
4、在源文件中写函数的声明。
示例:
//名为swap.h的头文件
#include<iostream>
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;
}
//有main()函数的文件
#include<iostream>
using namespace std;
#include"swap.h"
int main()
{
int a = 10, b = 20;
swap(a, b);
system("pause");
return 0;
}
通过分文件编写,我们可以在main()函数中加入许多的函数,结构看起来也很简洁,只需要添加头文件即可(注意,这种头文件使用的是双引号)。