Saturday, April 24, 2010

Difference between Normal function and Macro Funcion

Normal function is defined :
return_type function_name(arguments)
{
//coding
}
Macro function:
#define funtion_name(arguments) coding

Difference:
while compiler expanding the code, the macro function is replaced in called place, but normal function is just called by . 


Macro function example source code and expanded code:

Source Code:
#define tech() printf("hi")
int main()

{
tech();
return 0;
}

Expanded Code:
int main()
{
printf("hi);
return 0;
}

Normal Function example source code and Expanded Code:

Source code:
 void tech()
{

printf("hi");
}
int main()
{

tech():
return 0;
}

Expanded Code:

 void tech()
{

printf("hi");
}
int main()
{

tech():
return 0;
}

From above coding ,you can see while compiler expand the code macro function is replaced in called placed.  But normal function is just called ,no replace is happen.


Macro Function Advantage:
Macro function will reduce the execution time because it is just replaced in called position.  But normal function is just called (calling take more time ).


Limitations of Macro Function:
Macro function should be used while the code of function is small.  if size of the function is high, you should neglect the macro function.  Because it will  cause to redundancy problem and increase the size of file.

0 comments:

Post a Comment