当前位置: 首页 > news >正文

C++中函数的调用

*************

C++

topic:call functions

*************

1、为什么要用函数

In every codes, functions are the crucial parts. There are many advantages of the functions. But I introduce two of them.

The first usage of the functions is reuse. And the second usage of the functions is separating the whole progect to some modules.

2、写一个简单的函数

Functions are easy. The first thing to write a function is naming the function. A good habbit is that each function have to realize one function.

before writing a function, follow the c++ rules of the functions.

返回类型 函数名(参数类型 参数名, 参数类型 参数名,...) 
{
    // 函数体
    // 包含具体的实现逻辑
    // 可以有变量声明、表达式、语句等
    // 可能包含 return 语句用于返回值
}
int add(int a, int b) 
{ // 返回类型为 int,函数名为 add,参数列表包含两个 int 类型的参数
    int result = a + b; // 函数体
    return result;      // 返回结果
}
double calculateAverage(int a, double b) 
{ // 参数类型分别为 int 和 double,返回类型为 double
    return (a + b) / 2.0; // 返回计算后的平均值
}
void display(int value, bool newline = true) 
{ // 参数 newline 有默认值 true
    std::cout << value;
    if (newline) 
    { // 如果 newline 为 true
        std::cout << std::endl; // 换行
    }
}

Here take add function as an example. The name is add and the return type is void.

void add(int a, int b)
{
    int result = a + b;
}

3、调用add函数

Introduse a new concept, which is Namespace and Class. Namespace is to organize the function names. Class is to package the methods and data.

We have two beauties who share the same name. If you call Zhang Ruonan to be your girlfriend, two of them are puzzled. Which one you are calling? 

Namespace is to distinguish two Zhang Ruonan. 

namespace ElseWhere's girlfriend
{
    beauty ZhangRuonan
}

namespace Your girlfriend
{
    beauty ZhangRuonan
}

if you call ElseWhere:: ZhangRuonan, that is my girlfriend.

if you call Your girlfriend:: ZhangRuonan, she is your girlfriend.

class is the method.

namespace ElseWhere's girlfriend
{
    class Kiss
    {
        kiss myLips;
    };

    class Hug
    {
        hug myArms;
    };
}

how to let ZhangRuonan kiss you? 

Call the function.

namespace ElseWhere's girlfriend
{
    class Kiss
    {
        kiss myLips;

        void myLips();

    };

    class Hug
    {
        hug myArms;
    };

    // call the function hug() from the class Hug
    void hug()
    {
        Hug HugElseWhere;
        HugElseWhere.myArms();
    }
}

kiss mylips are called in the Kiss class. Hug are called out of the class Hug. Some attention pleasec. If you call the function out of the calss, make an object first. And then the object call the function. Like Zhang Ruonan and I walks in the street, I said hey, kiss me. Maybe Liu Yifei will come up and kiss me, Zhang Ruonan will annoy at me. So you will call hey, Zhang RUonan kiss me. If in the same class, it likes I and Zhang Ruonan are in home and only two of us are in the room. When I say hey kiss me, Zhang will come up and give me a sweety kiss kiss.

namespace ElseWhere // namespace 名字可以自定义
{
    class kiss // class 名字可以自定义
    {
        kiss myLips(beauty Zhang, handsome ElseWhere) // 函数的名字是myLips
        {
            kiss myLips// 在这里写函数的具体内容
        }

        // 在kiss内部调用myLips函数
        void home(void) // 在家里让章若楠亲我
        {
            myLips(); // 调用myLips函数
        }
    };

    // 在kiss外部调用myLips函数
    class street // 在街道上让章若楠亲我
    {
    public: // 公有成员变量和函数
        kiss myKiss; // 创建一个kiss类的对象myKiss
        myKiss.myLips(); // 调用myKiss对象的myLips函数
    };
}

4、总结

That is main usage of the skills in c++, the rest of the skills are mathematic tricks. 

相关文章:

  • 【论文笔记】Attentive Eraser
  • 利用Java爬虫获取1688店铺所有商品信息:实战指南
  • 探秘基带算法:从原理到5G时代的通信变革【七】FFT/DFT
  • C++11之右值引用
  • Pycharm操作(二)设置字体大小
  • 基于深度学习的网络摄像头图像实时分类实践:从理论到完整实现
  • 集群、分布式与微服务架构 区别
  • 常用 nvm 命令指南
  • 懒加载能够解决Spring循环依赖吗
  • golang lumberjack 日志包
  • 【Linux】之【Bug】VMware 虚拟机开机 一直卡在黑屏左上角下划线闪烁界面
  • PostgreSQL中的模式(Schema)
  • 基于值函数的强化学习算法之SARSA详解
  • 02 HarmonyOS Next仪表盘案例详解(一):基础篇
  • Python执行脚本并捕获输出
  • linux下手动升级ollama
  • Windows逆向工程入门之MASM过程调用机制深度解析
  • 【前端css】position定位
  • 虚拟机IP的配置,让它上网
  • [BUUCTF]web--wp(持续更新中)
  • 上海网站建设 乐云seo/网站数据分析
  • 郑州哪家专业做淘宝网站/网络营销评价的名词解释
  • 房产网站 模板/公司企业网站模板
  • 罗湖附近公司做网站建设哪家好/网络营销案例及分析
  • 建设工程项目管理网站/软文发稿网站
  • 建设方案包括哪些内容/怎么做关键词优化排名