函数
为了减少重复的代码, 避免程序冗余, 增加代码的可读性
定义格式
返回值 函数名(参数列表){
函数体
}
- 返回值 : 有返回(返回对应的类型, 需要使用return )/无返回(void)
- 函数名: 起名要有语义, 最好使用小驼峰命名法(第一单词首字母小写, 其余首字母大写)
调用函数格式
函数名();
#include <iostream>
using namespace std;
// 1.定义函数: 函数定义之后需要调用才能有效果
void hello(){
cout << "hello c++"<< endl;
}
//
int main() {
// 调用函数
hello();
}
函数的形式
按照参数和返回值分类, 我们分为
- 无参数无返回
- 无参数有返回
- 有参数无返回
- 有参数有返回
#include <iostream>
using namespace std;
// 1.无参无返回
void hello() {
cout << "hello c++" << endl;
}
// 2.无参有返回
int showAge() {
return 100;
}
// 3.有参数无返回 int a, int b 是参数列表,
// 我们也称 a, b 为形参, 可以理解为声明站位
void add(int a, int b) {
cout << a << " + " << b << " = " << a + b << endl;
}
// 4.有参数有返回 1 ~ n 累加和
int calculateNum(int n){
int sum = 0;
for (int i = 1; i <= n ; ++i)
sum +=i;
return sum;
}
int main() {
hello();
int age = showAge();
cout << "age = " << age << endl;
add(10, 20); // 10, 20 这里叫实参
add(100, 200);
cout << "1 ~ 100 cal = " << calculateNum(100) << endl;
cout << "1 ~ 10 cal = " << calculateNum(10) << endl;
cout << "1 ~ 50 cal = " << calculateNum(50) << endl;
// 1~50累加和, 1~20 累加和, 将得到的累加和进行相乘
int num1 = calculateNum(50);
int num2 = calculateNum(20);
int res = num1 * num2;
cout << "res = " << res << endl;
}
函数参数顺序
参数传的顺序会对计算结果产生影响(甚至会产生错误), 为了避免歧义出现,参数传递一定要注意顺序
#include <iostream>
using namespace std;
void show_info(string name , int age, string addr, string gender){
cout << "name : "<< name << endl;
cout << "gender : "<< gender << endl;
cout << "age : "<< age << endl;
cout << "addr : "<< addr << endl;
}
void show_info2(string name , int age, string addr, string gender="male"){
cout << "name : "<< name << endl;
cout << "gender : "<< gender << endl;
cout << "age : "<< age << endl;
cout << "addr : "<< addr << endl;
}
int main() {
//必须参数: 函数参数传入的顺序要和声明时保持一致, 否则会产生一起
show_info("eric", 19, "shengyang", "feamle");
cout << "======================================================================="<< endl;
// show_info("eric", 19, "male", "beijing"); 传参错误
// 默认参数: 函数可以设置默认参数, 这样这个参数可以传(传入的值)也可以不传(默认的值)
show_info2("bob", 88, "newyork"); // 此时gender="male"
cout << "======================================================================="<< endl;
show_info2("bob", 88, "newyork", "male"); // 此时gender="male"
}
函数传参的两种形式
- 值传递
- 所谓值传递,就是函数调⽤时实参将数值传⼊给形参
- 值传递时,如果形参发⽣改变,并不会影响实参
#include <iostream>
using namespace std;
//(函数需要在调用前进行声明), 如果没有, 需要使用函数的原型声明
// 函数的原型声明, 先声明, 后实现
// 没有函数体.只有函数名字和参数
void swap(int, int);
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;
}
// 在main函数后面声明
void swap(int x, int y){
int temp;
temp = x;
x = y;
y = temp;
cout << "============swap()============"<< endl;
cout << "x = "<< x << endl;
cout << "y = "<< y << endl;
cout << "++x = "<< ++x << endl;
cout << "============swap()============"<< endl;
}
- 引用传递
引用传递: 如果改变参数的值, 会对原来的值产生影响
#include <iostream>
using namespace std;
void swap(int &x, int &y);
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;
}
// 引用传递值, 如果改变参数的值, 会对原来的值产生影响
// &取地址符号
void swap(int &x, int &y){
int temp;
temp = x;
x = y;
y = temp;
cout << "============swap()============"<< endl;
cout << "x = "<< x << endl;
cout << "y = "<< y << endl;
cout << "++x = "<< ++x << endl;
cout << "============swap()============"<< endl;
}
数组作为参数
#include <iostream>
using namespace std;
int calArray(int arr[], int len){
int sum = 0;
for (int i = 0; i < len; ++i) {
sum += arr[i];
}
return sum;
}
// 数组作为函数的参数时, 是引用传递, 函数内部对数组的修改
// 会影响到原数组
void updateArray(int arr[], int index, int target){
arr[index] = target;
}
int main(){
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int len = sizeof(arr)/sizeof(arr[0]);
cout << calArray(arr, len)<< endl;
cout << "updateArray() After"<< endl;
updateArray(arr, 2, 999);
for (int i = 0; i < len; ++i)
cout << arr[i]<< "\t";
}
递归
函数递归 : 函数自己调用自己
1 ~ num 累加和
#include <iostream>
using namespace std;
// 函数递归 : 函数自己调用自己
// 好处: 会让复杂的逻辑使用递归函数看起来更简洁
// 语义
int addSum(int n){
if ( n <= 1)
return 1;
return addSum(n-1) + n;
}
int mulCal(int n){
if(n<=1)
return 1;
return mulCal(n-1)*n;
}
int main() {
cout<< addSum(100)<<endl;
cout<< addSum(5)<<endl;
cout<< mulCal(5)<<endl;
cout<< mulCal(10)<<endl;
cout<< mulCal(2)<<endl;
cout<< mulCal(3)<<endl;
}
// 100*99*98..... n!
斐波那契数列(Fibonacci sequence),又称黄金分割数列、因数学家莱昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……在数学上,斐波那契数列以如下被以递推的方法定义:F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 2,n ∈ N*)
#include <iostream>
using namespace std;
// 1、1、2、3、5、8、13、21、34
int fibo(int n){
if (n <=2)
return 1;
return fibo(n-1) + fibo(n-2);
}
int fibo2(int n){
if (n == 2)
return 1;
if (n == 1)
return 0;
return fibo2(n-1) + fibo2(n-2);
}
//0、 1、1、2、3、5、8、13、21、34 fibo2
int main() {
cout<<fibo(8)<<endl;
cout<<fibo(6)<<endl;
cout<<fibo(4)<<endl;
cout<<fibo(2)<<endl;
cout<<fibo(1)<<endl;
cout<<fibo2(3)<<endl;
cout<<fibo2(4)<<endl;
}
#include <iostream>
using namespace std;
int sum(int arr[], int size){
// arr[l, len)这个区间的和等于 arr[l] + arrl后面的
if (size == 0)
return 0; //如果数组为空,返回0
else if (size == 1)
return arr[size-1]; // 如果数组只有一个元素,返回该数组元素
else
return arr[size - 1] + sum(arr, size-1);
//数组最后索引的数值和递归调用sum方法
}
int main(){
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int len = sizeof(arr)/sizeof(arr[0]);
cout <<sum(arr, len) << endl;
}
内联函数
声明方式是返回值前加inline关键字, 目的是对一些函数体不是很大, 但又频繁被调用而且想要很高的效率的函数来进行声明
#include <iostream>
using namespace std;
inline int cube(int x){
return x*x*x;
}
int main()
{
int result;
result = cube(2);
cout<<"2*2*2 = " << result<<endl;
result = cube(3);
cout<<"3*3*3 = " << result<<endl;
return 0;
}
函数的重载 overload
什么是重载? 同名不同参
同名是函数名相同, 不同参是指 函数的参数类型和个数不相同, 这里与返回值没啥关系
#include <iostream>
using namespace std;
int add(int x, int y){
return x + y;
}
float add(float x, float y){
return x + y;
}
int add(int x, int y, int z){
return x + y + z;
}
int main()
{
float a = 10.6f;
float b = 8.7f;
cout<< add(10, 8)<< endl;
cout<< add(10, 8, 33)<< endl;
float c = add(a, b);
cout<< c << endl;
}