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

What is tri-state logic, What is Tri-state logic ? Ans. Tri-state Logi...

What is Tri-state logic ? Ans. Tri-state Logic: In common logic circuits, there are two states of the output, as LOW and HIGH. If the output is not in the LOW state, this

#artificial intelligence, does matlab contain procedures for knoledge repre...

does matlab contain procedures for knoledge representation? if yes where can i find it?

Example of processor arrangements, Q. Example of processor arrangements? ...

Q. Example of processor arrangements? !HPF$ PROCESSORS P (10) This initiates a group of 10 abstract processors assigning them combined name P. !HPF$ PROCESSORS Q (4, 4)

Synchronous, What are differences between Synchronous, Asynchronous and I s...

What are differences between Synchronous, Asynchronous and I synchronous communication? Sending data encoded in your signal needs that the sender and receiver are both by using

How can you have an accession number, Accession number (bioinformatics), a ...

Accession number (bioinformatics), a unique identifier given to a biological polymer sequence (DNA, protein) when it is given to a sequence database.

Explain the client-server interaction using messages, Explain the Client-Se...

Explain the Client-Server Interaction Using Messages As we have learned, client- server interaction may be managed in many ways. A message- based interaction is perhaps the bes

Define encapsulation- object oriented technology, Define Encapsulation- obj...

Define Encapsulation- object-oriented technology Encapsulation also called as information hiding, is the characteristic of separating the external aspects of an object, from t

How can we access the correction and transport system, How can we access th...

How can we access the correction and transport system? Each time you make a new object or change an existing object in the ABAP/4 Dictionary, you branch automatically to the W

Differentiate aggregation and containment, Aggregation is the relationship ...

Aggregation is the relationship among the whole and a part. We can add/subtract some properties in the part (slave) side. It won't affect the entire part. Best example is Car,

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