An explanation of the inline function

Source: Internet
Author: User

What is an inline function?
Inline (caution, not online), translated into "inline" or "inline". Use to replace the "macro"
Meaning: When the compiler discovers that a piece of code calls an inline function, it does not call the function, but instead inserts the code of the function into the current position.
Benefits: Eliminates the call process and speeds up the program. (the function calls the procedure, because has previously said the parameter into the stack and so on operation, therefore must occupy some time more often).
Not good: Because whenever the code calls into an inline function, you need to insert the code of the function directly at the call, so the volume of the program increases.


Life Example:
Take the life phenomenon analogy, like the TV is broken, through the telephone to find the repairman, you will be too slow, so simply at home to keep a repairman. This is, of course, fast, but the repairman is going to have to take the floor when he lives in your house. An inline function is not required, it is just a modification to improve speed. To modify a function to be inline, use the following format:
declaration or definition of an inline function
In a simple sentence, add an inline modifier before the function declaration or definition.
inline int max (int a, int b)
{
Return (A>B)? A:B;
}




Ii. rules of the inline function and considerations


(1), (recursive not) the function containing the recursive call cannot be set to inline;


(2), (the program is not complex) the use of complex process Control statements: Loop statements and switch statements, can not be set to inline;


(3), (the program should be small) due to the increase in volume of inline features, so the proposed inline function code should be very short. Preferably no more than 5 lines.


(4), (special case conversion display warning) inline only as a "request", in particular cases, the compiler will disregard the inline keyword, and forcing the function to become a normal function. When this occurs, the compiler gives a warning message.


(5), (Program execution order) before you call an inline function, the function must be previously declared or defined as inline, otherwise it is not called.


As in the following code snippet:
The function is not declared inline at first:
void Foo ();
Then there is the code that calls it:
Foo ();
Only after the call has the function defined as inline:
inline void foo ()
{
......
}
The code is the Foo () function does not finally implement inline;


(6), for debugging convenience, when the program is in the debugging phase, all inline functions are not implemented.


(7), inline functions defined in one file cannot be used in another file. They are usually shared in the header file.
Reference URL:


What about the above in C + + and in JavaScript?
Here's a quick introduction:


General JavaScript functions:
<script>
function init () {
Alert ("I am saying something important.");
}
Window.onload () = init;
</script>
The above is a normal function, assigning it to the OnLoad () event, one acting as the handler for the event, but we can use an inline function, which avoids the definition of the function specifically:
eg
<script>
Window.onload = function ()
{
Alert ("I am saying something important.");
}
</script>
Advantage: Referencing (local) variables directly within a specific scope without having to pass the function as an argument to the function.


http://blog.csdn.net/forarrow/article/details/6394931

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

An explanation of the inline function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.