Using the inline function in C++ programming

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Doubling the given integer to demonstrate the inline function in C++ programming

To show: How to declare, define and use the inline functions in C++ programming

 

// demonstrates the inline functions

#include <iostream>

using namespace std;

 

// inline function, no need a prototype here, we directly declares and defines the function

inline int Doubler(int target){return (2*target);}

 

int main(void)

{

int target;

 

cout<<"Enter a number to work with: ";

cin>>target;

cout<<"\n";

 

// function call

target = Doubler(target);

cout<<"\nFirst time function call, Target: "<<target;

 

// function call

target = Doubler(target);

cout<<"\nSecond time function call, Target: "<<target;

 

// another call

target = Doubler(target);

cout<<"\nThird time function call, Target: "<<target<<endl;

 

return 0;

}

 

Output example:

 

Enter a number to work with: 100

First time function call, Target: 200

Second time function call, Target: 400

Third time function call, Target: 800

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index