+ Reply to Thread
Results 1 to 2 of 2

Thread: Inline function

  1. #1
    Chinmoy's Avatar
    Chinmoy is offline Programming Expert
    Join Date
    Feb 2008
    Location
    where heaven meets earth
    Posts
    410
    Rep Power
    18

    Lightbulb Inline function

    In this tutorial we wil see the use of inline functions.


    It sometimes so happens that we have functions scattered all over the program.In this case a function call causes the program to jumps to the address of the function and come back when the function call terminates. This takes away some precious time.


    The above problem can be resolved with the use of inline functions. This causes the compiler to call the code directly from the source. No new memory instruction set is created for the inline function code. This gives the compiler an option to read from the source and there is no need for the call trace to jump around. Thus they are similar to macro definitions and are as fast as macros.


    Although the inline declaration in c++ is free and occurs automatically when the function is defined in the declaration, in c it is restricted by the following rules ::

    1. In C, any function with internal linkage can be declared inline, but a function with external linkage is has restrictions on inline.

    2. If the inline keyword is used in the function declaration, then the function definition should be present in the same translation unit.



    Most of the advantage of inline functions comes from avoiding the overhead of calling an actual function. Thus there is a saving of registers and no setting up stack frames. But inline functions present a problem for debuggers and profilers, because the function is expanded at the point of call and loses its identity.


    Heres how you can use the inline functions.

    The general syntax of using an inline function is :
    Code:
    inline datatype function_name(arguments)
    A general c++ code to square a number would loook like this ::
    Code:
    #include <iostream.h>
    
    class inline_
    {
    public:
    int square(int);
    };
    
    void main()
    {
    	inline_ i1;
    	int x;
    	cout <<"\nEnter a number ::";
    	cin>>x;
    	cout<<"\n The square is ::" << i1.square(x);
    }
    
    int ::square(int x1)
    {
    	return x1*x1;
    }

    Now using inline functions this would become::

    Code:
    #include <iostream.h>
    
    class inline_
    {
    public:
    int square(int);
    };
    
    void main()
    {
    	inline_ i1;
    	int x;
    	cout <<"\nEnter a number ::";
    	cin>>x;
    	cout<<"\n The square is ::" << i1.square(x);
    }
    
    int inline_::square(int x1)
    {
    	return x1*x1;
    }
    This code runs upto 30% fastert than a non inline function, the rest depending on the prcessor speed.

    Now comes the strategy part. You may use inline functions at your will but keeping in mind that inline functions can take much less time to execute but they have a high memory occupancy on the run. Also the compiler has always an option to overlook your inline declaration if the code declared inline is abnormally large compared to the code size.


    Inline declaration although destroys the order of evaluation, does not make the function internal. The function is still external.
    God is real... unless declared an integer

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Inline function

    Nice one. Cheers!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Inline vs Code Behind
    By Blue Indian in forum ASP, ASP.NET and Coldfusion
    Replies: 1
    Last Post: 11-08-2010, 08:10 PM
  2. Inline Functions
    By roboticforest in forum C and C++
    Replies: 9
    Last Post: 10-28-2010, 08:52 AM
  3. Inline vs code behind
    By Ronin12 in forum ASP, ASP.NET and Coldfusion
    Replies: 0
    Last Post: 05-23-2006, 10:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts