BoostC Inline Functions

From OpenCircuits
Revision as of 17:03, 17 March 2009 by Russ hensel (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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 of Creating Inline Functions

  • Using a #define
  • Using a #define with arguments
  • Using a inline qualifier in the function definition
  • Using assembler inline -- this is not an inline function, at least as I am using the term here.

Reasons to Use Inline Functions

  • 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 -- Some Details

all of a #define must be contained on one line, but a function typically requires several lines. The lines can be simulated by the required ";" the end of statement marker, but you can still run out of line length. You can combine different lines by using the line xxxx characer "\" at the end of a line. This lets you change:

   if ( a == 0 ) { x = 1; y = 2; }


to

    if ( a == 0 ) { \
        x = 1; \ 
        y = 2; \
    }
   

Much more readable and you do not run out of line length.

--- all this needs to be completed and verified in boostc

#define with Arguments -- Some Details

Still to be written

Inline C -- Some 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. UPDATE: Actually this is not correct. Debug of inline functions is disabled by default but can be enabled using compiler command line argument -i

Comment:

I tried to use an inline function in a .c file and call it from another .c file. It was declared in the .h file. This would not compile. I had to move the entier inline function to the .h file. This caused other reference problems. So if I did this right just adding the inline qualifier may not be enough in some cases. -- This should be checked more carefully.

Inline Assmebler Code -- Some Details

This type of inline code is not a function it is simply a way of including assembler in the middl of, or inline with C code. Not explained here yet. The manual is pretty good on this topic.

Source Code Using Inline Functions

Links

2.3. The Textual Structure of Programs

The C Preprocessor

The C Preprocessor

7.2. How the preprocessor works

C preprocessor From Wikipedia, the free encyclopedia

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox