Difference between revisions of "BoostC Inline Functions"
Russ hensel (talk | contribs) |
Russ hensel (talk | contribs) |
||
Line 8: | Line 8: | ||
== Reasons == | == Reasons == | ||
− | *Smaller faster code -- sometimes, sometimes not. | + | *Smaller faster code -- sometimes, sometimes not. ( the first time it is used, your save the code for subroutine linkage, the second time you call you have two copies of the function ) |
− | * | + | *Avoid too deep a call return stack. |
Since there is no call/return the stack usage is zip. | Since there is no call/return the stack usage is zip. | ||
*Make changes to code easier. | *Make changes to code easier. | ||
Line 17: | Line 17: | ||
*Remove the chance of calling a function from both the main line and the interrupt | *Remove the chance of calling a function from both the main line and the interrupt | ||
Because there are actually two different versions of the function. | Because there are actually two different versions of the function. | ||
− | |||
− | |||
== #DEFINE Details == | == #DEFINE Details == |
Revision as of 18:35, 7 March 2009
Inline functions are statements that look like functions but do not really call anyting or return, the code is generated right "inline" with the statements before and after the "function call". There are a couple of different ways to do this, and a couple of different reasons to do it.
Contents
Ways
- Using a #DEFINE
- Using a inline qualifier in the function definition.
Reasons
- Smaller faster code -- sometimes, sometimes not. ( the first time it is used, your save the code for subroutine linkage, the second time you call you have two copies of the function )
- Avoid too deep a call return stack.
Since there is no call/return the stack usage is zip.
- Make changes to code easier.
If the code would normally have to be changed in many places, changing the definition of the "function" can allow all the changes to be made in one place.
- Make code more readable.
If a subroutine is called only in one place, it may still make the code more readable by using an inline function to hide or "chunk" low level details.
- Remove the chance of calling a function from both the main line and the interrupt
Because there are actually two different versions of the function.
#DEFINE Details
Inline Details
For this method you qualify the function with the keyword inline. According to the manual you can do anything you would with any other function. Perhaps.
Pros:
- Looks just like any other function.
- Easy to change back and forth with a one word change.
- Can be used with arguments and returned values.
Cons:
- You can not debug ( source code ) into the function.