栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c/c++函数详解

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c/c++函数详解

函数详解

概述

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

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

//小知识,函数英文function,既有函数也有功能的意思,其实更偏向于功能

函数的定义

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

1、返回值类型

2、函数名

3、参数表列

4、函数体语句

5、return 表达式

语法:

返回值类型  函数名  (参数列表)

 {

函数体语句;

return表达式;

}

返回值类型 :一个函数可以返回一个值。返回值类型决 定返回值的数据类型

函数名:给函数起个名称

参数列表:使用该函数时,传入的数据

函数体语句:花括号内的代码,函数内需要执行的语句 return表达式: 和返回值类型挂钩,函数执行完后,返回 相应的数据

示例:定义一个加法函数,实现两个数相加 //函数定义

int add(int num1, int num2)

 {

int sum = num1 + num2;

return sum;

}

函数的调用

功能:使用定义好的函数

语法:函数名(参数);

例如:

#include

using namespace std;

//实现,得出两个数字之和

//函数的定义:

int add(int num1, int num2) //定义中的num1, num2成为形式参数,简称形参

{

    int sum = num1 + num2;

    return sum;

}

int main()

{

    int a = 10;

    int b = 20;

    //函数的使用(调用):

    //函数名(参数);

    int c = add(a, b);//调用时的a,b称为实际参数,简称实参

    cout << c << endl;

    return EXIT_SUCCESS;

}

注意:函数写在主函数main前则不需要声明。

总结:函数定义里小括号内称为形参,函数调用时传入的参数称为实参。

值传递

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

#include

using namespace std;

//数据交换

void swap(int num1, int num2)

{

    cout << "交换前:" << endl;

    cout << "num1 = " << num1 << endl;

    cout << "num2 = " << num2 << endl;

    int temp = num1;

    num1 = num2;

    num2 = temp;

    cout << "交换后:" << endl;

    cout << "num1 = " << num1 << endl;

    cout << "num2 = " << num2 << endl;

    //return;

}

int main0604()

{

    int a = 10;

    int b = 20;

    swap(a, b);

    cout << "主函数里的a = " << a << endl;

    cout << "主函数里的b = " << b << endl;

    //所谓值传递,如果形参发生改变,并不会影响实参

    return EXIT_SUCCESS;

}

函数的常见样式

常见的函数样式有4种

1. 无参无返

2. 有参无返

3. 无参有返

4. 有参有返

#include

using namespace std;

//1、无参无返

void test01()

{

    //void a = 10;//err,无类型不可以创建变量,原因是无法分配内存

}

//2、有参无返

void test02(int a)

{

}

//3、无参有返

int test03()

{

    return 10;

}

//4、有参有返

int test04(int a, int b)

{

    int sum = a + b;

    return sum;

}

int main0605()

{

    return EXIT_SUCCESS;

}

函数的声明

作用:告诉编译器函数名称及如何调用函数。函数的实际主体可以单独定义。 注意:函数的声明可以多次,但是函数的定义只能有一次

#include "6.7 函数的分文件编写.h"

//函数的声明

//int max(int a, int b);

//声明可以多次,定义只能一次

int main0606()

{

    int a = 100;

    int b = 200;

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

    return EXIT_SUCCESS;

}

//函数的定义

int max(int a, int b)

{

    return a > b ? a : b;

}

函数的分文件编写

作用:让代码结构更加清晰 函数分文件编写一般有4个步骤

1. 创建后缀名为.h的头文件

2. 创建后缀名为.cpp的源文件

3. 在头文件中写函数的声明

4. 在源文件中写函数的定义

#pragma once

#include

using namespace std;

int max(int a, int b);

实践时间(10分钟)

1. 根据案例需求,用C或C++语言编写代码,并编写相应功能函数,完成程序。

案例:

输入两个正整数m和n(m

#include

using namespace std;

bool isPrime(int x)

{

    int i = 0;

    for (i = 2; i < x; i++)

    {

         if (x % i == 0)

         {

             break;

         }

    }

    if (x == i)

    {

         return true;

    }

    else

    {

         return false;

    }

}

int main0608()

{

    int num = 0;

    int m = 0;

    int n = 0;

    int sum = 0;

    cin >> m >> n;

    for (num = m; num <= n; num++)

    {

         if (isPrime(num))

         {

             sum += num;

         }

    }

    cout << sum << endl;

    return EXIT_SUCCESS;

}

函数提高

3.1 函数默认参数

在C++中,函数的形参列表中的形参是可以有默认值的。

语法:返回值类型 函数名  (参数=  默认值){}

#include

using namespace std;

//返回值类型 函数名 (参数 = 默认值) {}

int func(int a, int b = 10, int c = 10)

{

    return a + b + c;

}

//1、如果某个位置参数有默认值,那么这个位置往后,从左到右,必须都要有默认值

//2、如果函数参数声明有默认值,那么函数实现时候不能有默认值

int func2(int a, int b = 10);//声明

int func2(int a, int b)

{

    return a + b;

}

int main0301()

{

    cout << func(100) << endl;

    cout << func(10, 20, 30) << endl;

    return EXIT_SUCCESS;

}

3.2 函数占位参数

C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置 语法:  返回值类型 函数名  (数据类型){}

在现阶段函数的占位参数存在意义不大,但是后面的课程中会用到该技术

#include

using namespace std;

//返回值类型 函数名 (数据类型) {}

void func(int a, int)

{

}

int main0302()

{

    func(10, 10);

    return EXIT_SUCCESS;

}

3.3 函数重载

3.3.1 函数重载概述

作用:函数名可以相同,提高复用性

函数重载满足条件:

1. 同一个作用域下

2. 函数名称相同

函数参数类型不同 或者 个数不同 或者 顺序不同

注意: 函数的返回值不可以作为函数重载的条件

#include

using namespace std;

void func()

{

    cout << "void func()的调用" << endl;

}

void func(int a)

{

    cout << "void func(int a)的调用" << endl;

}

void func(double a)

{

    cout << "void func(double a)的调用" << endl;

}

void func(int a, double b)

{

    cout << "void func(int a, double b)的调用" << endl;

}

void func(double a, int b)

{

    cout << "void func(double a, int b)的调用" << endl;

}

//函数返回值不可以作为函数重载的条件

//int void(double a, double b)

//{

//  cout << "int void(double a, double b)的调用" << endl;

//  return 1;

//}

int main030301()

{

    func();

    func(10);

    func(3.14);

    func(10, 3.14);

    func(3.14, 10);

    //func(3.14, 3.14);

    return EXIT_SUCCESS;

}

3.3.2 函数重载注意事项

1. 引用作为重载条件

2. 函数重载碰到函数默认参数

#include

using namespace std;

//1、引用作为重载条件

void func2(int& a)

{

    cout << "void func(int& a)的调用" << endl;

}

void func2(const int& a)

{

    cout << "void func(const int& a)的调用" << endl;

}

//2、函数重载碰到了函数默认参数

void func3(int a, int b = 10)

{

    cout << "void func3(int a, int b = 10)的调用" << endl;

}

void func3(int a)

{

    cout << "void func3(int a)的调用" << endl;

}

int main()

{

    int a = 10;

    func2(a);

    const int b = 20;

    func2(b);

    //func3(10); //碰到默认参数产生歧义,需要避免

    return EXIT_SUCCESS;

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/779398.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号