C++快速讲解(三):函数


前言:主要讲解了函数的定义、函数的原型、分离式编译、函数的重载、函数的参数的传递、内联函数、函数的值域等。


1.函数的定义

1.1 无参数无返回

#include <iostream>
using namespace std;
//无返回值无参数
void eat(){
    cout << "apple" << endl;
}
int main(){
    eat();
    return 0;
}

在这里插入图片描述

1.2 有参数无返回

#include <iostream>
#include <string>
using namespace std;

//有返回值无参数
string eat(){
    return "eat apple";
}
int main(){

    string e = eat();
    cout<<e<<endl;
    return 0;
}

在这里插入图片描述

1.3 无参数有返回

#include <iostream>
using namespace std;

//无返回值有参数
void eat(int n){
    cout << "apple:"<<n << endl;
}
int main(){
    eat(8);
    return 0;
}

在这里插入图片描述

1.4 有参数有返回

#include <iostream>
using namespace std;
//有返回值有参数

int add(int n,int m){
    return n+m;
}
int main(){

    int sum = add(10,20);
    cout << sum<<endl;
    return 0;
}

在这里插入图片描述

2.函数的原型

c++的函数一般包含声明和定义两个部分,所以在使用函数前,必须先表示函数的存在,或者我们直接把函数的整体写在main函数上面。

#include <iostream>
using namespace std;

//使用函数原型
int add(int m,int n);
int main(){
    int sum = add(11,22);
    cout << sum << endl;
    return 0;
}

//函数定义,函数的真正的实现
int add(int m, int n){
    return m+n;
}

在这里插入图片描述

3.分离编译

一般说来,函数的声明 ( 函数原型 )通常都会放到头文件中,函数的具体实现都写在cpp里面(不是main文件),这样各司其职不会导致main文件的代码过多。

  • 先定义math.h
#ifndef DAY02_MATH_H
#define DAY02_MATH_H
int add(int a,int b);
#endif //DAY02_MATH_H

  • 在定义math.cpp
#include "math.h"

int add(int a,int b){
    return a+b;
}
  • 最后在main文件中调用
#include <iostream>
#include "math.h"//这里使用"" 表示从当前目录查找

using namespace std;

int main(){
    int sum = add(10,20);
    cout << sum <<endl;
    return 0;
}

在这里插入图片描述

4.函数的重载

在许多语言中,经常会见到两个或者两个以上的函数名称是一样的,当然他们的参数个数或者参数类型或者是参数的顺序是不一样的。
看下面的例子:

#include <iostream>
using namespace std;
//函数重载
int add(int a , int b){
    return a + b ;
}

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


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

int main(){
    int sum1 = add(3, 3);
    int sum2 = add(3, 3, 3);
    double sum3 = add(2.5 , 2.5);
    cout << sum1<<"---"<<sum2<<"---"<<sum3<<endl;
    return 0;
}

在这里插入图片描述

5.函数的参数

5.1 值传递

C++默认情况下,处理函数参数传递时,多数使用的是值的拷贝,少数部分除外。

#include <iostream>
using namespace std;
//值传递
void change(int m);
int main(){
    int number = 100;
    cout << "没有执行函数之前的值" <<number<< endl;
    change(number);
    cout << "执行函数之后的值" << number<<endl;
    return 0;
}

void change(int m){
    if (m == 100){
        m = 200;
    }
}

在这里插入图片描述
我们发现在函数里面改变了number的值,但是在main里面没有改变,我们可以使用值引用的方法来改变main里面的值。

5.2 值引用

引用实际上只是原有数据的一种别名称呼而已,使用 & 定义。

#include <iostream>
using namespace std;
//传递引用
void change(int &m);
int main(){
    int number = 100;
    cout << "没有执行函数之前的值" <<number<< endl;
    change(number);
    cout << "执行函数之后的值" << number<<endl;
    return 0;
}

void change(int &m){
    if (m == 100){
        m = 200;
    }
}

在这里插入图片描述

5.3 数组传递

#include <iostream>
using namespace std;
//传递数组

//传递数组长度
void print_array(int number[]);

int main(){
    //声明数组
    int array [] = {1,2,3,4,5};
    cout << "没有执行函数之前的数组" << endl;
    for (int i = 0 ; i < 5 ; i++){
        cout << array[i] << endl;
    }
    //打印数组
    print_array(array);
    cout << "执行函数之后的数组" << endl;
    for (int i = 0 ; i < 5 ; i++){
        cout << array[i] << endl;
    }

    return 0 ;
}

//传递数组,打印数组
void print_array(int array[]){
    for (int i = 0 ; i < 5 ; i++){
        array[i] = i+5;
    }
}

在这里插入图片描述

6.内联函数

作为特别注重程序执行效率,适合编写底层系统软件的高级程序设计语言,如果函数中只有简单的几行代码,那么可以使用inline 关键字来解决了函数调用开销的问题。

#include <iostream>
using namespace std;

//内联函数
inline int calc_Max (int a, int b)
{
    if(a >b)
        return a;
    return b;
}

int main(){

    int max = calc_Max(3, 8);
    cout << "max = " << max << endl;
    return 0 ;
}

在这里插入图片描述

7.函数的值域

7.1 单独代码块

#include <iostream>
using namespace std;
//单独代码块
int main(){

    int  num = 10;
    int  num1 = 20;
    cout << num <<"----"<< num1 << endl;
    {
        int num = 100;
        cout << "num = "<< num << endl;
        cout << "num1 = "<< num1 << endl;
    }
    return 0;
}

在这里插入图片描述

7.2 静态本地变量

注意:static只会初始化一次,重复调用函数也只会初始化一次。

#include<iostream>
using namespace std;

void static_local(){

    static int num{1};
    cout << "num ="<< num << endl;
    num+=10;
    cout << "num ="<< num << endl;
}

int main(){
    static_local();
    static_local();
    return 0 ;
}

在这里插入图片描述

7.3 全局变量

注意:可以使用::来访问全局变量,这样不会和局部的变量冲突。

#include<iostream>
using namespace std;

int age = 99;
int main(){

    int age =18 ;
    cout << "main里面的age:"<< age << endl;
    cout << "全局的age:"<<::age << endl;
    return 0 ;
}

在这里插入图片描述


结束!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等待着冬天的风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值