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

 

 

 

 

转载于:https://www.cnblogs.com/fsbblogs/p/9692278.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值