zoukankan      html  css  js  c++  java
  • uda 1.C++ 函数

    函数:Python vs C++

    在 Python 和 C++ 中,函数的作用相同;函数把语句组合在一起,执行某种任务。函数可以帮助你避免重复地复制和粘贴相同的代码。

    函数编写的语法有些不同,主要有三个原因:

    1. Python 通过查看回车符和换行符来检测代码行的结束。C++ 使用分号实现这一功能。
    2. Python 使用缩进将代码语句组合在一起,但 C++ 使用大括号。
    3. Python 是动态类型的,而 C++ 是静态类型的。和声明变量的方法一样,你同样需要声明你的函数。
     

    我们从一个简单的函数开始,并且并排比较 Python 和 C++ 代码。

    这个函数代入速度和时间。两者相乘计算出距离。除了语法上的差异,还要特别注意:

    • 函数声明
    • 变量声明
    • main() 内部是什么代码,外部是什么代码。
     
     

    剖析代码

    C++ 代码看起来比 Python 代码长得多,因为 C++ 有一些额外的部分。我们逐句解析这段代码。

    代码开始是

    # include <iostream>
    

    这里是导入 C++ 标准库的 iostream 部分。需要这行代码才能使用 cout

    导入必要的库之后,你可以看到一个函数声明。

    float distance(float velocity, float time_elapsed);
    

    这行代码通知你的 C++ 程序,有一个名为 distance 的函数。这个函数接收两个单精度浮点数并返回一个单精度浮点数。第一个单精度浮点数是 velocity,第二个是 time_elapsed。

    然后是main函数。所有 C++ 程序都需要 main() 函数,这个函数返回 0。main() 调用 distance 函数并把结果输出到终端。

    int main() {
    
        std::cout << distance(5, 4) << std::endl;
        std::cout << distance(12.1, 7.9) << std::endl;
    
        return 0;
    }
    

    最后是函数定义

    float distance(float velocity, float time_elapsed) {
        return velocity * time_elapsed;
    }
    
     

    你之前已经看到过 main() 函数,因此这不是你第一次看到函数如何在 C++ 中发挥作用。注意,main 函数和 distance 函数的语法非常相似。唯一的区别在于,main 函数不接受任何参数,并返回值为 0 的整数;而 distance 函数接受两个单精度浮点数并返回一个单精度浮点数。

    此外,main 函数不需要单独声明。

    函数剖析

    你已经看过如何用 C++ 编写函数。一般而言,C++ 函数由函数声明和函数定义组成。

    因为 C++ 是静态类型的,所以你需要指定函数输入变量的数据类型和函数返回的数据类型。

    //function declaration
    returndatatype functionname(datatype variable_a, datatype variable_b, etc.);
    //function definition
    returndatatype functionname(datatype variable_a, datatype variable_b, etc.) {
         statement_1;
         statement_2;
         .... etc
    
       return returndatatype
    }

    小测试:编写函数

    写一个名为 distance 的函数,该函数有 3 个输入和 1 个输出。输入为速度、加速度和时间。输出为随时间推移经过的距离。距离的计算公式为: distance = velocity imes elapsedtime + 0.5 imes acceleration imes elapsedtime imes elapsedtimedistance=velocity×elapsedtime+0.5×acceleration×elapsedtime×elapsedtime

    //TODO: include the iostream part of the standard library
    #include <iostream>
    
    //TODO: declare your function called distance
    float distance(float velocity, float acceleration, float time_elapsed);
    
    // Leave the main function as is
    int main() {
        
        // TODO: The following are examples you can use to test your code.
        // You will need to uncomment them to get them working.
        
        std::cout << distance(3, 4, 5) << std::endl;  
        std::cout << distance(7.0, 2.1, 5.4) << std::endl;
        
        return 0;   
    }
    
    //TODO: define your function
    float distance(float velocity, float acceleration, float time_elapsed) {
        return velocity*time_elapsed + 0.5*acceleration*time_elapsed*time_elapsed;
    }

    多输出函数

    在Python中,你可以编写多输出函数。比如下面这个例子:

    ## Python Code
    def distance(velocity, time_elapsed):
        return velocity * time_elapsed, velocity / 2
    

    它将会输出 elocity time_elapsed以及 velocity/2 (速度 时间和速度 / 2)。

    在C++中,函数只可以有一个输出。当然我们也有一些变通方案,但这些方案不在本模块的讨论范围之内。

     

    C++ 小贴士:函数声明

    你并不需要将函数声明放在代码开头来获取有效的解决方案。正像你可以同时声明和定义一个变量那样,int x = 5;,你也可以同时声明和定义一个函数。

    下面这段代码同样可以运行:

    float distance(float velocity, float time_elapsed) {
        return velocity * time_elapsed;
    }
    
    int main() {
    
        std::cout << distance(5, 4) << std::endl;
        std::cout << distance(12.1, 7.9) << std::endl;
    
        return 0;
    }

    不过请注意,你需要在 main() 函数之前定义你的函数,而不是在它之后,否则你的代码将会尝试调用 distance() 函数,却没有它的定义。

  • 相关阅读:
    巴菲特最推崇的10本书
    如何锻炼剑术基本功
    Ubuntu 20.04 LTS, CUDA 11.2.0, NVIDIA 455 and libcudnn 8.0.4
    缘起性空
    Mac 每次都要执行source ~/.bash_profile 配置的环境变量才生效
    Calcite分析 -- Register
    go超时控制有4种写法,你知道吗?
    npm install node-sass报错处理
    IDEA + maven热部署及自动编译不生效问题
    1-STM32+CH395Q(以太网)远程升级篇(自建物联网平台)-STM32通过ch395使用http下载程序文件,升级程序(单片机程序轮训检查更新)
  • 原文地址:https://www.cnblogs.com/fuhang/p/9077488.html
Copyright © 2011-2022 走看看