2300 views|1 replies

1

Posts

1

Resources
The OP
 

Inline functions [Copy link]

1. Inline function In C++ we usually define the following function to find the maximum value of two integers: Copy code The code is as follows: int max(int a, int b) { return a > b ? a : b; } The benefits of defining a function for such a small operation are: ① It is much easier to read and understand the call to the function max than to read an equivalent conditional expression and interpret its meaning ② If any modifications need to be made, it is much easier to modify the function than to find and modify each equivalent expression ③ Using functions can ensure uniform behavior, and each test is guaranteed to be implemented in the same way ④ Functions can be reused without having to rewrite code for other applications Although there are so many benefits, there is a potential disadvantage of writing it as a function: calling a function is much slower than solving an equivalent expression. On most machines, calling a function requires a lot of work: registers must be saved before the call and restored when returning, actual parameters must be copied, and the program must also switch to a new location to execute. C++ supports inline functions, whose purpose is to improve the execution efficiency of functions. You can specify a function as an inline function by putting the keyword in front of the function definition (note that it is a definition rather than a declaration, which will be discussed below). An inline function is usually expanded "inline" at each call point in the program. Suppose we define max as an inline function: Copy code The code is as follows: inline int max(int a, int b) { return a > b ? a : b; } Then call: cout< b ? a : b)<b ? a : b; } Main.cpp : Copy the code The code is as follows: #include
#include "A.h"
using namespace std;

inline int A::max()
{
return a > b ? a : b;
}

int main()
{
A a(3, 5);
cout<
Inline functions are rarely used in MCU programming. Generally, they are used in some short functions on ARM, and others don’t pay much attention to them. Thanks for sharing. I have studied them in depth. Thank you!

Inline functions


This post is from Programming Basics

Latest reply

Inline functions are rarely used in MCU programming. Generally, they are used in some short functions on ARM, and others don’t pay much attention to them. Thanks for sharing. I have studied them in depth. Thank you!  Details Published on 2018-9-12 08:38
 

1368

Posts

6

Resources
2
 
Inline functions are rarely used in MCU programming. Generally, they are used in some short functions on ARM, and others don’t pay much attention to them. Thanks for sharing. I have studied them in depth. Thank you!
This post is from Programming Basics
 
Personal signature专注智能产品的研究与开发,专注于电子电路的生产与制造……QQ:2912615383,电子爱好者群: void
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list