Difference Between Inline and Macro
Inline vs Macro
C++ has gone way too far in the field of computer programming these days. Being a powerful language, it sometimes gets a little bit confusing to the programmers because of its huge library of functions. Inline function is one of them which can easily be confused with macros in the language.
It is not that there is a minor difference between the two, but they can be a bit of confusing because of the properties they hold. In this article, we will be discussing more about the differences between inline function and macros in C++.
Inline
An inline function is just like any other function in C++ and is also called in the regular way. The function it performs is that it creates a copy of the compiled function definition. That is, it creates a copy of the defined items to compile. An example can be taken if we are adding any two integers and call it the inline function, the compiler will create a copy of the integers to be compiled.
Example:
Inline int sum (int x, int y)
{
Return (x+y);
}
Macro
Macros in C++ implement text replacement in a program line. That is, they replace text according to the change defined in the function. Unlike inline as a function, a macro manipulates the code using a function. For example:
#define DOUBLE(X) X*X
int y = 5;
int j = DOUBLE (++y);
Here, we will get the value as 30! As the calling has been done via a macro, “X” has been replaced with ++y which makes ++y to be multiplied by another ++y. This makes a total of 5*6 that is 30 not 6. Six would be the basic but a wrong answer.
Now, macros might be causing a bug here. So an inline function comes to the rescue by copying the values to the compilers’ memory and then compiling it.
Summary:
1.An inline function creates a copy of the function definition.
2.A macro replaces the text as identified and defined within the function.
3.An inline function is also applied when a macro is supposed to cause a bug in the program.
- Difference Between Data Mining and Data Warehousing - October 21, 2012
- Difference Between 7-Keto DHEA and DHEA - October 20, 2012
- Difference Between Tamil and Malayalam - October 18, 2012
Search DifferenceBetween.net :
Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.