zoukankan      html  css  js  c++  java
  • Cpp Chapter 8: Adventures in Functions Part1

    //第一次在博客上发自己的C++ primer plus学习笔记,从现在进度(第8章)开始,用英文也当是练练英语写作了

    1. Inline Functions

    ) When calling a regular function, the program execution will be transfered to the called function and back, thus making it time-consuming under circumstances that call the same short function for a multiple of times. In an inline function, the compiler replaces the function call with corresponding function code, so the program execution don't need to be transfered.

    ) If the function code's execution time is short relatively to the execution-transfer process, using inline function will save a great proportion of time.

    ) To implement this feature, common practice is to omit the function prototype and put the entire definition at the beginning of the file with keyword inline.

    for example:

     1 // example of inline function comparison
     2 #include <iostream>
     3 using namespace std;
     4 double square(double);
     5 
     6 int main()
     7 {
     8 ...
     9 }
    10 
    11 double sqaure(double x)
    12 {
    13     return x * x;
    14 }

    this code uses a function called square. It has only one line of code, so transfer it to inline function:

    // example of inline function comparison
    #include <iostream>
    using namespace std;
    inline double square(double x) {return x * x;}
    
    int main()
    {
    ...
    }

    By adding the inline keyword, you mean that square() is an inline function.

    ) Back in C, it used #define methods to cope with similar problems. But #define is simply a text substitution, inline functions can pass arguments by value just like normal functions do.

    2. Reference Variables

    ) A reference is a name that acts as an alternative name for a previously defined variable. The main use is as a formal argument to a function so the function could work with the original data, unlike the passing-by-value fashion.

    ) C++ use '&' for declaring references:

    int rats;
    int & rodents = rats;

    In this way, "int &" is an identifier means reference to int, so rodents is a reference to rats and they are exactly the same, no matter value or address.

    ) One should initialize a reference variable when declare it. Unlike pointers, reference doesn't support later assignment.

    int rats;
    int & rodents = rats;
    int bunnies;
    rodents = bunnies;

    The last line above doesn't make rodents reference to bunnies, but it actually works as "rats = bunnies".

    ) Take the function swap() which swaps two values for example, using reference in functions(compared to pointers and value):

    void swapr(int & a, int & b); // function prototype for reference
    swapr(a, b) // function call for reference
    void swapp(int * a, int * b); // function prototype for pointer
    swapp(&a, &b) // function call for pointer
    void swapv(int a, int b); // function prototype for value
    swapv(a, b) // function call for value

     

  • 相关阅读:
    淘宝技术这十年(淘宝技术大学校长解密淘宝十年)
    Go Web编程(Go语言性能好、语法简单、开发效率高!)
    辨别虚假流量的十二种方法
    收获,不止Oracle(顶级专家盖国强、冯春培、黄志洪等联袂力荐)
    演说之禅:职场必知的幻灯片秘技(第2版)(全彩)
    篮球词汇(转)
    Xilinx 网站资源导读
    乔布斯2005年在斯坦福毕业典礼的演讲稿
    FPGA中BRAM初始化文件格式的总结_ALTERA/XILINX
    写给还在上班的人(转)
  • 原文地址:https://www.cnblogs.com/fsbblogs/p/9692278.html
Copyright © 2011-2022 走看看