Presentation is loading. Please wait.

Presentation is loading. Please wait.

Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.

Similar presentations


Presentation on theme: "Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After."— Presentation transcript:

1 Macro & Function

2 Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After they are processed, and finally the function returns a value that is assigned to a variable (except for a void function). If a function is invoked a number of times, the times add up, and compilation is delayed.

3 Macro & Function Function consumes more time For the execution of a function, the current instruction address will be store in a stack register. And execute the function call, which is specified in the instruction(another location) After the execution of the function, pop the stack register and return back to the main instruction address All these process needs time

4 Macro & Function Macro consumes less time On the other hand, the macro expansion had already taken place and replaced each occurrence of the macro in the source code before the source code starts compiling. So it requires no additional time to execute.

5 Macro & Function Function consumes less memory. All the macro-presences are replaced by their corresponding macro expansions, which consumes considerable memory. On the other hand, even if a function is invoked 100 times, it still occupies the same space.

6 Macro & Function Example for Macro: The following line defines the macro SUM, having two parameters a and b #define SUM(a,b) (a + b) This definition would cause the preprocessor to change the following statements: c = SUM(x,y); c = d * SUM(x,y); In the output of the preprocessor, these statements would appear as: c = (x + y); c = d * (x + y);

7 Macro & Function Example for function: In calling program function call would appear as, c=SUM(x,y); Where sum is the name of the function. And x, y are the arguments passed from calling program to called program. Hence x, y are copied to parameters variables a and b. And then function computes the sum and stores the value in c, which will be returned back to the calling program. int sum(int a, int b) { int c; c=a+b; return ©; }

8 Macro & Function


Download ppt "Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After."

Similar presentations


Ads by Google