Explain the differences between macros and subroutines, Computer Engineering

Assignment Help:

Explain the differences between macros and subroutines.

Macros Vs Subroutines

(i) Macros are pre-processor directives which are processed before the source program is passed to the compiler.

Subroutines are blocks of codes along with a exact task, to be performed and are directly passed to the compiler.

(ii) In a macro call the pre-processor replaces the macro template along with its macro expansion, in a literal manner.

As against this, into a function call the control is passed to a function with exact arguments, some computations are performed in the function and a helpful value is returned back from the function.

(iii) Macro raises the program size. For illustration, if we use a macro hundred times in a program, the macro expansion goes in our source code at hundred various places. Because the function makes the program smaller and compact, for example, if a function is used, the even if this is called from hundred various places in the program, this would take similar amount of space in the program.

(iv) Macros compose the program run faster as they have already been expanded and placed into the source code before compilation. While, passing arguments to a function and obtaining back the returned values does take time and would thus slow down the program.

(v)   Illustration of macro

#define AREA(x) (3.14*x*x) // macro definition main(){

float r1=6.25, r2=2.5, a;

a=AREA(r1); // expanded to (3.14 * r1 * r1)

printf("\n Area of circle =%f",a);

a=AREA(r2); // // expanded to (3.14 * r2 * r2)

printf("\n Area of circle= %f",a);} Example of subroutine

main(){

float r1=6.25, r2=2.5, a;

a=AREA(r1); // calls AREA() printf("\n Area of circle =%f",a); a=AREA(r2); // calls AREA()

printf("\n Area of circle= %f",a);}

float AREA(float r) // subroutine{

return 3.14*r*r;}


Related Discussions:- Explain the differences between macros and subroutines

Pipe, Your shell should accept and execute the pipe "|"  operator. This wil...

Your shell should accept and execute the pipe "|"  operator. This will look like the following: | The functionality of this operator is to execute command1 and send i

What is ai backgammon, This part looks at Berliner's program, two backprop ...

This part looks at Berliner's program, two backprop versions by Tesauro and a temporal difference process by Tesauro. This latter program is VERY good quality and has found strateg

Linux, Explain about unix file system architecture

Explain about unix file system architecture

Show layout 84-key keyboards, Q. Show layout 84-key Keyboards? PC 83-ke...

Q. Show layout 84-key Keyboards? PC 83-key was the earliest keyboard provided by IBM with its first Personal Computers (PC). This had 83 keys. Afterwards IBM added one more key

Mobile computing, Explain the key characteristics of mobile computing appli...

Explain the key characteristics of mobile computing applications

Name the processes of oom, Name the processes of OOM In OOM the modelli...

Name the processes of OOM In OOM the modelling passes through the given processes: System Analysis System Design Object Design, and Final Implementation.

Explain single instruction and multiple data stream (simd), Normal 0 ...

Normal 0 false false false EN-US X-NONE X-NONE Figure: SIMD Organisation

Handling a page in memory - computer architecture, Handling a Page: ...

Handling a Page: Typical page size today are 4 kb to 16 kb ,having tendency to use even larger page sized Organization that reduce the page fault rate are striking (comp

What do you mean by system calls, What do you mean by system calls? Sys...

What do you mean by system calls? System calls give the interface among a process and the operating system. When a system call is executed, it is treated as by the hardware as

Explain branch instruction with control signals, Explain BRANCH instruction...

Explain BRANCH instruction with Control Signals. and explain SHIFT instruction with Control Signals.  Explain Register Transfer Language. What do you mean by Addressing Techn

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd