GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  Concepts  >  OOPS

 Print  |  
Question:  what is the similarities between macro and function?



March 03, 2006 06:13:45 #3
 Ranjith   Member Since: Visitor    Total Comments: N/A 

RE: what is the similarities between macro and functio...
 

Macro works similar to a function, but it's execution time is faster than function. Also, macro simply substitutes the value without performing calculatons where as function does calculations. The following example describes the difference between macro and function:

Macro:

#define sq(x) x*x

if you call sq(5+5), the result will be 35 becos macro directly substitutes the value. i.e, in this case, 5+5*5+5 = 35.

Function:

int sq(x)

{

       return x*x;

}

if you call sq(5+5) now, the result will be 100, because function calculates the argument value (if it has some calculations) and passes to the operation stack. i.e, in this case sq(5+5) will become sq(10) before passing to return x*x.

 

Hope this clears. Please let me know, if anybody know more about this

     

 

Back To Question