6 函数

6.1 概述

作用:将一段经常使用的代码封装起来,减少重复代码

一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能。

6.2 函数的定义

函数的定义一般主要有5个步骤:

  1. 返回值类型
  2. 函数名
  3. 参数列表
  4. 函数体语句
  5. return表达式

语法

返回值类型 函数名 (参数列表)
{
    函数体语句
    
    return表达式
}
  • 返回值类型:一个函数可以返回一个值。在函数定义中
  • 函数名:给函数起个名称
  • 参数列表:使用该函数时,传入的数据
  • 函数体语句:花括号内的代码,函数内需要执行的语句
  • return表达式:和返回值类型挂钩,函数执行完后,返回相应的数据

示例

实现一个加法函数,功能是,传入两个整型数据,计算数据相加的结果,并且返回

#include<iostream>
using namespace std;

int add(int num1, int num2)
{
    int sum = num1 + num2;
    
    return sum;
}

int main() 
{

    int c = add(10, 20);
    cout << c << endl;

    system("pause");

    return 0;

}

6.3 函数的调用

功能:使用定义好的函数

语法函数名 (参数)

示例

#include<iostream>
using namespace std;

//函数定义的时候,num1和num2并没有真实数据,只是一个形式上的参数,简称形参
int add(int num1, int num2)
{
    int sum = num1 + num2;
    
    return sum;
}

int main() 
{

    int a = 10;
    int b = 20;
    
    //a和b称为 实际参数,简称实参
    //函数调用的时候,实参的值会传递给形参
    int c = add(a, b);
    cout << c << endl;
    
    a = 11;
    b = 22;

    int c = add(a, b);
    cout << c << endl;

    system("pause");

    return 0;

}

6.4 值传递

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

示例

#include<iostream>
using namespace std;

//值传递
//定义函数,实现两个数字进行交换数字

//如果函数不需要返回值,声明的时候可以写void
void swap(int num1, int num2)
{
    cout << "num1 = " << num1 << endl;
    cout << "num2 = " << num2 << endl;

    int tmp = num1;
    num1 = num2;
    num2 = tmp;

    cout << "交换后:" << endl;
    cout << "num1 = " << num1 << endl;
    cout << "num2 = " << num2 << endl;
}

int main()
{
    int a = 10;
    int b = 20;

    cout << "交换前:" << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    //当我们做值传递的时候,函数的形参发生改变,并不会影响实参
    swap(a, b);

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;


    system("pause");

    return 0;

}

a 和 b 在内存中开辟了两块4字节的内存空间,num1 和 num2 也在内存中开辟了两块4字节的内存空间,彼此独立,所以形参的改变不会影响实参

swap1

总结:值传递时,形参是修饰不了实参的

6.5 函数的常见样式

常见的函数样式有4种

  1. 无参无返
  2. 有参无返
  3. 无参有返
  4. 有参有返

示例

#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 * a;
}

int main()
{
    //无参无返函数调用
    test01();

    //有参无返函数调用
    test02(100);

    //无参有返函数调用
    int num1 = test03();
    cout << "num1 = " << num1 << endl;

    //有参有返函数调用
    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 test04 a = 10000
num2 = 100000000
请按任意键继续. . .

6.6 函数的声明

作用:告诉编译器函数名称及如何调用函数。函数的实际主体可以单独定义。

  • 函数的声明可以多次,但是函数的定义只能有一次

示例

#include<iostream>
using namespace std;

//函数的声明
//比较函数,实现两个整型数字进行比较,返回较大的值

//提前告诉编译器函数的存在,可以利用函数的声明
//声明可以写多次,但是定义只能有一次
int getMax(int a, int b);
int getMax(int a, int b);
int getMax(int a, int b);

int main()
{
    int a = 10;
    int b = 20;

    cout << getMax(a, b) << endl;

    system("pause");

    return 0;

}

//定义
int getMax(int a, int b)
{
    return a > b ? a : b;
}
函数定义在 main 函数之前,可以不写函数声明;函数定义在 main 函数之后,必须写函数声明

6.7 函数的分文件编写

作用:让代码结构更加清晰

函数分文件编写一般有4个步骤

  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;
}

函数的分文件编写.cpp

#include<iostream>
using namespace std;
#include"swap.h"

//函数的分文件编写
//实现两个数字进行交换的函数

int main()
{
    int a = 10;
    int b = 20;

    swap(a, b);

    system("pause");

    return 0;

}

思思
1 声望1 粉丝

« 上一篇
5 数组
下一篇 »
7 指针