difference between macro and function?
Answers were Sorted based on User's Feedback
Answer / subrat pattnaik
1.In Inline function if the codes are too much it acts as
normal function(without expanding in a line it gets jumps to
fun defination) but this is not with macro(it gets inserted
were it is calleed without checking its bulkness).
2.Suppose
#define square(x) (x*x)
inline void SQUARE(int x)
{
x*x;
}
void main()
{
a=5;
cout<<"The square is "<<square(++a);
cout<<"The SQUARE is "<<SQUARE(++a);
}
Here in macro it will b ++a * ++a(a incrementing 2 times)
In inline it will get incremented and is squared(++a)^2
Thanks
| Is This Answer Correct ? | 10 Yes | 0 No |
Answer / soruabh
When you call a function your compiler enters a
call-sequence (which takes
time) and allocates a new stack frame for that function
(whcih takes text
stack space) so that the function's body can be executed.
After it's done
you enter a returning-sequence phase (which takes time).
A macro does not need anything of the above, because it's
preprocessor's job
to expand a macro, it's only about text replacement, not
about compiler
stuff or code-generating issues. So you don't expend time
and space doing
what a function would need in order to be executed.
| Is This Answer Correct ? | 4 Yes | 0 No |
Answer / utpal kumar kashyap
Hi to Everyone,
See, inline function is a function whose code get inserted instead of jump to the function where it was called.
We can make any function as inline but there are few conditions in which compiler will not treat it as inline function....
If
1. Function contains any static variable.
2. It recursive.
3. Function code is large.
However, if function body is large and in this case if we try to make it as inline, then compiler wont give an error, but compiler would treat it as normal function. So idea is this, function code should be small for making it inline.
| Is This Answer Correct ? | 4 Yes | 0 No |
What are multiple inheritances (virtual inheritance)? What are its advantages and disadvantages?
How much is c++ certification?
iam a fresher to Qt(GUI a c++ based framework software). i need to develop the basic applications on designer by drag and dropping mechanism...so pls send me the procedure to design applications?
Can you think of a situation where your program would crash without reaching the breakball, which you set at the beginning of main()?
What is protected inheritance?
Search for: what is pair in c++?
How does c++ structure differ from c++ class?
Is c++ pass by reference or value?
What are smart pointers?
What is ofstream c++?
Write a C++ Program to Generate Random Numbers between 0 and 100
Explain the concept of inheritance in C++.