Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Inline functions

Explains the need of inline functions, when to / not to use them and how to use them.

On Saturday, September 25th 2004 at 01:28 PM
By Andrew Pociu (View Profile)
***--   (Rated 2.9 with 31 votes)
Contextual Ads
More C++ Resources
Advertisement
In this tutorial I'll explain what inline functions are and after that I'll show you an example of using inline functions.



If you don't know yet, when in your code there's a function, the program jumps to the address of the function and when it reaches the end of the function it comes back. This 'jumping' actually involves much more (copying the arguments of the function on the stack for example) and takes time.



The solution is inline functions. Where an inline function is called the compiler will replace the call with the function code. So in reality in that place there will be no function call, only the code of the function. No 'jumping' needed to different addresses, no fuss.



But now you may ask...

When should I use inline functions?


You have to be careful when to use inline functions and when not. Most of the time you won't need to use inline functions as the performance gain is small.

The question actually is: When shouldn't I use inline functions?

The answer to this question is also inexact. You should think about what using an inline function involves in some situations. For example you shouldn't use inline functions when the function is called several times as this is a waste of space (think of 8 function calls, 8 copies of that function in the code).



Sometimes the compiler doesn't care about your decision, if you want the function to be inline or not. If the compiler sees that you want to make a huge function inline it may not respect your decision. Also, there are cases in which some small functions are treated as inline by some compilers.

How do I use inline functions?


The following code creates and calls an inline function:





#include "stdafx.h"

// Uncomment the below line if you don't use managed C++

//#include <iostream>

using namespace std;

inline int average(int a, int b)

{

   return (a + b) / 2;

}

int main()

{

   int result = average(12, 14);

   cout << "The average of number 12, 14 is " << result << "\n";

   return 0;

}




Besides adding the keyword inline before the function, you also have to place the function above any other function that may call this one. This also excludes the posibility of making recursive functions (functions that call each other) inline.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by dude on Monday, March 13th 2006 at 02:01 PM

ok

by nimbark paresh on Wednesday, June 9th 2010 at 08:39 AM

This tutorial is best for student
Its very easy to learn inline function
thanks......


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C++ Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources