macro & function. function consumes more time when a function is called, the copy of the...

8
Macro & Function

Upload: reynard-thornton

Post on 23-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

Macro & Function

Page 2: 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

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 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.

Page 3: 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

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

Page 4: 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

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.

Page 5: 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

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.

Page 6: 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

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);

Page 7: 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

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 ©;}

Page 8: 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

Macro & FunctionMacro Function

Macro is Preprocessor Function is Compiled

Speed of Execution is faster Speed of Execution is Slower

Code length increases Code length remains sameNo Data type Checking Data type checking is done

Before compilation macro name is replaced by macro value During function call, Transfer of

control takes place

Macro does not check compile errors Function checks Compile errors

Macro do not extend beyond one line Function can be of any number of lines

Useful where small code appears many time Useful where large code appears

many times