Inline function??

Hi, can I know what's Inline function and how to use them?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <conio>

inline float cube(const float a) {return a*a*a;}

main(){
float side;
cout<<"Enter the side length of your cube: ";
cin>>side;
cout<<"Volumn of cube with side "<<side<<" is "<<cube(side)<<endl;
getch ();
return 0;
}
Keyword inline gives the compiler a hint that a certain kind of optimization, called inlining, might be a good idea. Whether to perform it is for the compiler to decide. See http://en.wikipedia.org/wiki/Inline_function

inline functions can (and should) be put into headers as the compiler needs to see the body of a function to inline it.
inline functions can (and should) be put into headers as the compiler needs to see the body of a function to inline it.


1. which one is header?

2. and is this inline float cube(const float a) {return a*a*a;}
same as this
1
2
3
4
5
6
inline float cube(const float a)
{
	int r;
	r=a*a*a;
	return r;
}


3. why do we need to put "const" in line 4? Thanks...
1. header is a file where you hold declarations of classes and functions. If you're only working on small programs (one cpp file), you can ignore this, you'll learn later.

2. yes. edit: whoops.

3. we don't.
Last edited on
Here's a quick related question:

I've seen people use const like that. Is there any benefit to doing this other than just write protection?
@atjm88:
2. No! Line 4 will truncate a*a*a to an integer. Your compiler will probably generate a warning ("possible loss of precision"). The shorter version doesn't have this problem because it doesn't use an intermediate variable to store the result. If you declare 'r' as a float, the two code snippets will be identical in behaviour.

@Stewbond: Theoretically, there could be some optimizations through constness, but since const isn't a guarantee of constness, this is probably not the case. Personally, I find 'const' sends a clear message about the meaning. If a function shouldn't change an argument, then it shouldn't be able to.

(There's a post in the FAQ-lite about how you shouldn't const for performance. I'll look it up if I have a moment to spare.)
@Gaminic, ya...u r right...I miss out the float...But my compiler(Borland C++) did not give any warning about it, it'll just shows the answer in int, that's why I miss out...Now problem solve with float...thanks...

but for the const...I still don't understand when we need it and when not...Because when I delete that const inside the code, it still works fine...
Last edited on
One more question is, I just tested on VSC++ 2010, it can't works without int inside line 12 below...May I know why do we need "int" in some compiler and some no need? And can I use float main () instead of int main () in line 12?

Error shown when no int in line 12
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h>
using namespace std;

inline float cube(float a)
{
	int r;
	r=a*a*a;
	return (r);
}

int main(){
float side;
cout<<"Enter the side length of your cube: ";
cin>>side;
cout<<"Volumn of cube with side "<<side<<" is "<<cube(side)<<endl;
getch ();
return 0;
}
Last edited on
main() is the first function called by your machine when your program is excecuted. It has to be of type int by definition. void used to be acceptable, but not anymore.
const is never really necessary (unless some other const enforces it). It's useful for optimization and documentation.
Topic archived. No new replies allowed.