Like Tree1Likes
  • 1 Post By AndrewHunter

Am I missing something? (Tutorial #13: Inline Functions)

This is a discussion on Am I missing something? (Tutorial #13: Inline Functions) within the C++ Programming forums, part of the General Programming Boards category; I just read Tutorial #13: Inline Functions on this website, and I think I might be missing something. It seemed ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    8

    Am I missing something? (Tutorial #13: Inline Functions)

    I just read Tutorial #13: Inline Functions on this website, and I think I might be missing something.

    It seemed like inline functions are completely useless. Isn't that the point of a function in the first place? So that you only have to write the code once? Let me use the example used in the tutorial:

    Code:
    #include <iostream>
    using namespace std;
    
    inline void helloWorld(){
         cout<<"Hello World!";
    }
    int main(){
         helloWorld();
         cin.get();
    }
    Outputs
    Code:
    Hello World!
    Code:
    #include <iostream>
    using namespace std;
    
    void helloWorld(){
         cout<<"Hello World!";
    }
    int main(){
         helloWorld();
         cin.get();
    }
    Outputs
    Code:
    Hello World!
    Again, am I missing something here? What's the point of inline functions?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Speeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeed.

    You still only write the code once, just like any other function. But the compiler can (if it wants) simply replace the line of the function call with the actual body of the function, to save the overhead of doing a "real" function call.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    So without the "inline" keyword, the compiler tells the program to look at whatever code that function has, and run it, whenever the function is called, but with the "inline" keyword, the compiler literally replaces helloWorld(); with the code that is in the "helloWorld" function? If so, that makes sense, I suppose. :P

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by Bonnet View Post
    So without the "inline" keyword, the compiler tells the program to look at whatever code that function has, and run it, whenever the function is called, but with the "inline" keyword, the compiler literally replaces helloWorld(); with the code that is in the "helloWorld" function? If so, that makes sense, I suppose. :P
    Yes and no. Technically, the compiler will do whatever it believes to be the fasted way to execute the code, depending on the level of optimization selected.

    There is overhead in making a function call, e.g. setting up the stack, placing parameters on the stack, calling the function, executing code, returning from the function, resetting the stack, ect. This things take real time and hence can produce possible bottlenecks in code execution.

    The inline specificier is a suggestion to the compiler to make the function call as fast as possible. How it actually makes that call fast is up to the implementation. In some cases it will replace the function call with the actual code, in others it won't. The point of functions is to modularize your code, whether or not that results in a speed increase varies on a case by case basis.
    Salem likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    Okay, thanks for the reply. I knew I was missing something, because it sounded like inline functions were completely useless.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    To tell the truth, they are. When a lot of people write inline functions they're usually following a rule of thumb like "one liners are candidates for inlining" and don't worry about the details, since the compiler basically does what it wants.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    I believe (don't quote me on this) modern versions of GCC actually ignores the inline keyword altogether.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Sorry I remembered wrong. Inline is used, though at -O3, all functions are considered for inlining (not just those marked "inline"). It's the "register" keyword that's ignored.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    46
    Are inline functions important for working with classes? Since I'm new to C++ I find it hard to follow the flow of the code.

  10. #10
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    inline is a suggestion to the compiler which may or may not be taken into account and which may result only in a slight increase in processing speed. My guess is that you could never use inline and still be a great programmer.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Quote Originally Posted by tabl3six View Post
    Are inline functions important for working with classes? Since I'm new to C++ I find it hard to follow the flow of the code.
    Yes. Template classes are usually completely defined and implemented in header files.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    I would say that so far this thread has totally overlooked what is nodways arguably the more important use of inline.
    It is for preventing multiple definitions. When it comes to code in header files, which is the norm for templated code, then without the explicit inline keyword being there or the function being implicitly inlined by being inside the class definition, then link errors would be a massive problem.
    Regardless of whether the compiler does or does not actually inline a function call is one thing, but it always prevents multiple definitions.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Q about inline functions
    By homer_3 in forum C Programming
    Replies: 3
    Last Post: 05-26-2011, 03:44 AM
  2. Inline functions
    By Sharke in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2009, 06:18 AM
  3. Inline functions
    By Kempelen in forum C Programming
    Replies: 3
    Last Post: 03-13-2008, 06:00 AM
  4. Inline Functions
    By EvilGuru in forum C Programming
    Replies: 4
    Last Post: 11-07-2005, 03:03 PM
  5. What should I really use inline functions?
    By GrNxxDaY in forum C++ Programming
    Replies: 6
    Last Post: 07-27-2002, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21