阅读耗时 2 分钟

函数定义

你以为你会,你确实会

  1. 返回值 函数名 (参数列表){
  2. 函数体
  3. reutrn 表达式
  4. }

那我们先来写一个加法函数,并执行

#include<iostream>
using namespace std;

int main(){
    cout << "add ->" << add(10,10) << endl; 
    return 0;
}

int add(int a, int b){
    return a + b;
}

你猜,会不会报错,run一下,哇,

test.cpp: In function 'int main()':
test.cpp:13:25: error: 'add' was not declared in this scope
     cout << "add ->" << add(10,10) << endl;
                         ^~~
test.cpp:13:25: note: suggested alternative: 'rand'
     cout << "add ->" << add(10,10) << endl;
                         ^~~
                         rand

报错了,神奇,一般我们开发都习惯在代码后面添加新方法,对不对。C++不一样,编译器编译的时候,执行到main函数例,会发现没有add方法,你没有告诉编译器有这个方法,所以我们需要声明一下。或者也可以把方法实现放在main函数之前。

#include<iostream>
using namespace std;
int add(int a ,int b);

int main(){
    cout << "add ->" << add(10,10) << endl; 
    return 0;
}

int add(int a, int b){
    return a + b;
}

值传递

所谓值传递,就是函数调用时实参将数值传入给形参,形参发生改变,并不会影响实参

#include<iostream>
using namespace std;

void swap(int a, int b){
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    int temp = a;
    a = b;
    b = temp;
    cout << "swap doned a = " << a << endl;
    cout << "swap doned b = " << b << endl;
}

int main(){
    int a = 10;
    int b = 20;
    swap(10,20);
    cout << "global a = " << a << endl;
    cout << "global b = " << b << endl; 
    return 0;
}

执行结果

a = 10
b = 20
swap doned a = 20
swap doned b = 10
global a = 10
global b = 20

形参会分配内存,存的值时 实参的值。至于后面引用传递,在指针篇幅中进行描述。

函数的常见种类

你会问,这有什么好讲的,C++和 java不一样,和Kotlin或者Dart又类似,方法形参都可以设置有默认值。
我们常用的函数样式有,无参无返、有参无返、无参有返、有参有返,但这些说白了,各种语言都通用,爱怎么写就怎么写。
主要说一些默认值的写法
默认值写法

#include<iostream>
using namespace std;

//b默认10
void fun(int a,int b = 10){
    cout << "a + b = " << a+b << endl;
}

int main(){
    fun(10);
    fun(10,20);
    return 0;
}
结果:
a + b = 20
a + b = 30

但是有一个需要注意的,下面写法运行会报错,b写了默认值,c一定要写默认值,否则调用的时候,谁知道给哪个形参赋值。

void fun(int a,int b = 10, int c){
    cout << "a + b = " << a+b << endl;
}

函数声明

函数声明是什么?
针对一个代码里写的函数进行声明。它的作用是告诉编译器函数的名称及如何调用函数。
函数的声明可以多次,但是函数的定义只有一次。
函数声明放在main函数之前,也就是在调用之前,在编译期执行到函数调用的时候,你需要提前告知编译器。

#include<iostream>
using namespace std;
//可以有多次,也不知道开发人员在什么情况下才会写多次,可能是代码copy?
void fun(int a, int b);
void fun(int a, int b);
void fun(int a, int b);
int main(){

    fun(10,20);
    return 0;
}
void fun(int a, int b){

}

上面函数声明和下面等同

#include<iostream>
using namespace std;

void fun(int a, int b){

}
int main(){

    fun(10,20);
    return 0;
}

函数分文件编写

什么是函数分文件编写?
如果你写过java 可以理解为工具类,就是把共性的一些方法放到一个文件中。其他文件想使用,直接include
如何编写?

  1. 创建后缀为.h的文件,写好函数声明
  2. 创建cpp文件,写好函数的定义
    #include<iostream>
    using namespace std;
    //函数声明
    void swap(int a,int b);
    
    ```cpp

    include

    include “swap.h” //我们自定义的头文件

    using namespace std;

void swap(int a,int b){ cout << “swap” << endl; }

我们在其他文件中使用就可以这样做
```cpp
#include<iostream>
#include "swap.h" //我们自定义的头文件
using namespace std;

int main(){
    swap(10,20);
    return 0
}

如果运行失败,请确认下你是不是只是编译了 test.cpp文件,导致链接不到swap.h swap.capp