7 added 119 characters in body
source | link

Note

I would like to add the following clarification after observing the polarising voting pattern on this answer.

The answer was not written keeping technical accuracy and broad generalization in mind. It was a humble attempt to explain in simple language, the difference between macros and functions to a programming newbie, without beingtrying to be complete andor accurate (in fact it's far from being accurate). C programming language was in my mind when drafting the answer, but my apologies for not mentioning it clearly and causing any (potential) confusion.

I really regard the answer shared by Jörg W Mittag. It was insightful reading it (how less I know) and I upvoted it soon after it was posted. I just got started on Software Engineering Stack Exchange, and the experience and discussions so far have been really insightful.

I will leave this answer here as it may be helpful for other software development newbies, trying to understand a concept without getting bogged down into technical accuracies.


Both macro and function represent self contained unit of code. They both are tool which aid in modular design of a program. From the point of view of the programmer who's writing the source code, they appear quite similar. However, they differ in how they are handled during the program execution lifecycle.

A macro is defined once and used in a lot of places in a program. Macro gets expanded inline during the pre-processing stage. Thus, it technically doesn't remain a separate entity once the source code is compiled. The statements in macro definition becomes part of program instructions, just like other statements.

The motive behind writing a macro is to make writing and managing source code easier for the programmer. Macros are generally desired for simpler tasks where writing a full-fledged function would be a performance overhead/runtime penalty. Examples of situations where a macro is preferable over function are:

  • Using constant values (such as mathematical or scientific values), or some program specific parameter.

  • Printing log messages or handling assertions.

  • Performing simple calculations or condition checks.

When using macro, it's easy to make changes/corrections in one place which are instantly available everywhere the macro is used in the program. A simple recompilation of program is required for the changes to take effect.

Function code on the other hand is compiled as a separate unit within the program and gets loaded in the memory during program execution only if it's required. The function code retains it's independent identity from the rest of the program. The loaded code gets reused if the function is called more than once. When the function call is encountered in the running program, the control is passed to it by the runtime subsystem and the context of the running program (return instruction address) is preserved.

However, there's a slight performance penalty that needs to be encountered when calling a function (context switching, preserving return address of the main program instructions, passing parameters and handling return values etc.). Hence, the use of function is desired only for complex blocks of code (against macros which handles simpler cases).

With experience, a programmer makes a judicious decision as whether a piece of code will be a good fit as a macro or function in the overall program architecture.

Note

I would like to add the following clarification after observing the polarising voting pattern on this answer.

The answer was not written keeping technical accuracy and broad generalization in mind. It was a humble attempt to explain in simple language, the difference between macros and functions without being complete and accurate (in fact it's far from being accurate). C programming language was in my mind when drafting the answer, but my apologies for not mentioning it clearly and causing any (potential) confusion.

I really regard the answer shared by Jörg W Mittag. It was insightful reading it (how less I know) and I upvoted it soon after it was posted. I just got started on Software Engineering Stack Exchange, and the experience and discussions so far have been really insightful.

I will leave this answer here as it may be helpful for software development newbies.


Both macro and function represent self contained unit of code. They both are tool which aid in modular design of a program. From the point of view of the programmer who's writing the source code, they appear quite similar. However, they differ in how they are handled during the program execution lifecycle.

A macro is defined once and used in a lot of places in a program. Macro gets expanded inline during the pre-processing stage. Thus, it technically doesn't remain a separate entity once the source code is compiled. The statements in macro definition becomes part of program instructions, just like other statements.

The motive behind writing a macro is to make writing and managing source code easier for the programmer. Macros are generally desired for simpler tasks where writing a full-fledged function would be a performance overhead/runtime penalty. Examples of situations where a macro is preferable over function are:

  • Using constant values (such as mathematical or scientific values), or some program specific parameter.

  • Printing log messages or handling assertions.

  • Performing simple calculations or condition checks.

When using macro, it's easy to make changes/corrections in one place which are instantly available everywhere the macro is used in the program. A simple recompilation of program is required for the changes to take effect.

Function code on the other hand is compiled as a separate unit within the program and gets loaded in the memory during program execution only if it's required. The function code retains it's independent identity from the rest of the program. The loaded code gets reused if the function is called more than once. When the function call is encountered in the running program, the control is passed to it by the runtime subsystem and the context of the running program (return instruction address) is preserved.

However, there's a slight performance penalty that needs to be encountered when calling a function (context switching, preserving return address of the main program instructions, passing parameters and handling return values etc.). Hence, the use of function is desired only for complex blocks of code (against macros which handles simpler cases).

With experience, a programmer makes a judicious decision as whether a piece of code will be a good fit as a macro or function in the overall program architecture.

Note

I would like to add the following clarification after observing the polarising voting pattern on this answer.

The answer was not written keeping technical accuracy and broad generalization in mind. It was a humble attempt to explain in simple language, difference between macros and functions to a programming newbie, without trying to be complete or accurate (in fact it's far from being accurate). C programming language was in my mind when drafting the answer, but my apologies for not mentioning it clearly and causing any (potential) confusion.

I really regard the answer shared by Jörg W Mittag. It was insightful reading it (how less I know) and I upvoted it soon after it was posted. I just got started on Software Engineering Stack Exchange, and the experience and discussions so far have been really insightful.

I will leave this answer here as it may be helpful for other software development newbies, trying to understand a concept without getting bogged down into technical accuracies.


Both macro and function represent self contained unit of code. They both are tool which aid in modular design of a program. From the point of view of the programmer who's writing the source code, they appear quite similar. However, they differ in how they are handled during the program execution lifecycle.

A macro is defined once and used in a lot of places in a program. Macro gets expanded inline during the pre-processing stage. Thus, it technically doesn't remain a separate entity once the source code is compiled. The statements in macro definition becomes part of program instructions, just like other statements.

The motive behind writing a macro is to make writing and managing source code easier for the programmer. Macros are generally desired for simpler tasks where writing a full-fledged function would be a performance overhead/runtime penalty. Examples of situations where a macro is preferable over function are:

  • Using constant values (such as mathematical or scientific values), or some program specific parameter.

  • Printing log messages or handling assertions.

  • Performing simple calculations or condition checks.

When using macro, it's easy to make changes/corrections in one place which are instantly available everywhere the macro is used in the program. A simple recompilation of program is required for the changes to take effect.

Function code on the other hand is compiled as a separate unit within the program and gets loaded in the memory during program execution only if it's required. The function code retains it's independent identity from the rest of the program. The loaded code gets reused if the function is called more than once. When the function call is encountered in the running program, the control is passed to it by the runtime subsystem and the context of the running program (return instruction address) is preserved.

However, there's a slight performance penalty that needs to be encountered when calling a function (context switching, preserving return address of the main program instructions, passing parameters and handling return values etc.). Hence, the use of function is desired only for complex blocks of code (against macros which handles simpler cases).

With experience, a programmer makes a judicious decision as whether a piece of code will be a good fit as a macro or function in the overall program architecture.

6 added 52 characters in body
source | link

Note

Note: This I would like to add the following clarification after observing the polarising voting pattern on this answer.

The answer was not written keeping technical accuracy and broad generalization in mind. It was a humble attempt to explain in simple language, the difference between macros and functions without being complete and accurate (in fact it's far from being accurate). C programming language was in my mind when drafting the answer, but my apologies for not mentioning it clearly and causing any (potential) confusion.

I really regard the answer shared by Jörg W Mittag. It was insightful reading it (how less I know) and I upvoted it soon after it was posted. I just got started on Software Engineering Stack Exchange, and the experience and discussions so far have been really insightful.

I will leave this answer here as it may be helpful for software development newbies.


Both macro and function represent self contained unit of code. They both are tool which aid in modular design of a program. From the point of view of the programmer who's writing the source code, they appear quite similar. However, they differ in how they are handled during the program execution lifecycle.

A macro is defined once and used in a lot of places in a program. Macro gets expanded inline during the pre-processing stage. Thus, it technically doesn't remain a separate entity once the source code is compiled. The statements in macro definition becomes part of program instructions, just like other statements.

The motive behind writing a macro is to make writing and managing source code easier for the programmer. Macros are generally desired for simpler tasks where writing a full-fledged function would be a performance overhead/runtime penalty. Examples of situations where a macro is preferable over function are:

  • Using constant values (such as mathematical or scientific values), or some program specific parameter.

  • Printing log messages or handling assertions.

  • Performing simple calculations or condition checks.

When using macro, it's easy to make changes/corrections in one place which are instantly available everywhere the macro is used in the program. A simple recompilation of program is required for the changes to take effect.

Function code on the other hand is compiled as a separate unit within the program and gets loaded in the memory during program execution only if it's required. The function code retains it's independent identity from the rest of the program. The loaded code gets reused if the function is called more than once. When the function call is encountered in the running program, the control is passed to it by the runtime subsystem and the context of the running program (return instruction address) is preserved.

However, there's a slight performance penalty that needs to be encountered when calling a function (context switching, preserving return address of the main program instructions, passing parameters and handling return values etc.). Hence, the use of function is desired only for complex blocks of code (against macros which handles simpler cases).

With experience, a programmer makes a judicious decision as whether a piece of code will be a good fit as a macro or function in the overall program architecture.

Note: This answer was not written keeping technical accuracy and broad generalization in mind. It was a humble attempt to explain in simple language, the difference between macros and functions without being complete and accurate (in fact it's far from being accurate). C programming language was in my mind when drafting the answer, but my apologies for not mentioning it clearly and causing any (potential) confusion.

I really regard the answer shared by Jörg W Mittag. It was insightful reading it (how less I know) and I upvoted it soon after it was posted.

I will leave this answer here as it may be helpful for software development newbies.


Both macro and function represent self contained unit of code. They both are tool which aid in modular design of a program. From the point of view of the programmer who's writing the source code, they appear quite similar. However, they differ in how they are handled during the program execution lifecycle.

A macro is defined once and used in a lot of places in a program. Macro gets expanded inline during the pre-processing stage. Thus, it technically doesn't remain a separate entity once the source code is compiled. The statements in macro definition becomes part of program instructions, just like other statements.

The motive behind writing a macro is to make writing and managing source code easier for the programmer. Macros are generally desired for simpler tasks where writing a full-fledged function would be a performance overhead/runtime penalty. Examples of situations where a macro is preferable over function are:

  • Using constant values (such as mathematical or scientific values), or some program specific parameter.

  • Printing log messages or handling assertions.

  • Performing simple calculations or condition checks.

When using macro, it's easy to make changes/corrections in one place which are instantly available everywhere the macro is used in the program. A simple recompilation of program is required for the changes to take effect.

Function code on the other hand is compiled as a separate unit within the program and gets loaded in the memory during program execution only if it's required. The function code retains it's independent identity from the rest of the program. The loaded code gets reused if the function is called more than once. When the function call is encountered in the running program, the control is passed to it by the runtime subsystem and the context of the running program (return instruction address) is preserved.

However, there's a slight performance penalty that needs to be encountered when calling a function (context switching, preserving return address of the main program instructions, passing parameters and handling return values etc.). Hence, the use of function is desired only for complex blocks of code (against macros which handles simpler cases).

With experience, a programmer makes a judicious decision as whether a piece of code will be a good fit as a macro or function in the overall program architecture.

Note

I would like to add the following clarification after observing the polarising voting pattern on this answer.

The answer was not written keeping technical accuracy and broad generalization in mind. It was a humble attempt to explain in simple language, the difference between macros and functions without being complete and accurate (in fact it's far from being accurate). C programming language was in my mind when drafting the answer, but my apologies for not mentioning it clearly and causing any (potential) confusion.

I really regard the answer shared by Jörg W Mittag. It was insightful reading it (how less I know) and I upvoted it soon after it was posted. I just got started on Software Engineering Stack Exchange, and the experience and discussions so far have been really insightful.

I will leave this answer here as it may be helpful for software development newbies.


Both macro and function represent self contained unit of code. They both are tool which aid in modular design of a program. From the point of view of the programmer who's writing the source code, they appear quite similar. However, they differ in how they are handled during the program execution lifecycle.

A macro is defined once and used in a lot of places in a program. Macro gets expanded inline during the pre-processing stage. Thus, it technically doesn't remain a separate entity once the source code is compiled. The statements in macro definition becomes part of program instructions, just like other statements.

The motive behind writing a macro is to make writing and managing source code easier for the programmer. Macros are generally desired for simpler tasks where writing a full-fledged function would be a performance overhead/runtime penalty. Examples of situations where a macro is preferable over function are:

  • Using constant values (such as mathematical or scientific values), or some program specific parameter.

  • Printing log messages or handling assertions.

  • Performing simple calculations or condition checks.

When using macro, it's easy to make changes/corrections in one place which are instantly available everywhere the macro is used in the program. A simple recompilation of program is required for the changes to take effect.

Function code on the other hand is compiled as a separate unit within the program and gets loaded in the memory during program execution only if it's required. The function code retains it's independent identity from the rest of the program. The loaded code gets reused if the function is called more than once. When the function call is encountered in the running program, the control is passed to it by the runtime subsystem and the context of the running program (return instruction address) is preserved.

However, there's a slight performance penalty that needs to be encountered when calling a function (context switching, preserving return address of the main program instructions, passing parameters and handling return values etc.). Hence, the use of function is desired only for complex blocks of code (against macros which handles simpler cases).

With experience, a programmer makes a judicious decision as whether a piece of code will be a good fit as a macro or function in the overall program architecture.

5 added 52 characters in body
source | link

Note: This answer was not written keeping technical accuracy and broad generalization in mind. It was a humble attempt to explain in simple language, the difference between macros and functions without being being complete and accurate (in fact it's far from being accurate). C programming language was in my mind when drafting the answer, but my apologies for not mentioning it clearly and causing any (potential) confusion.

I really regard the answer shared by Jörg W Mittag. It was insightful reading it (how less I know) and I upvoted it soon after it was posted.

I will leave this answer here hopingas it may be helpful for software development newbies.


Both macro and function represent self contained unit of code. They both are tool which aid in modular design of a program. From the point of view of the programmer who's writing the source code, they appear quite similar. However, they differ in how they are handled during the program execution lifecycle.

A macro is defined once and used in a lot of places in a program. Macro gets expanded inline during the pre-processing stage. Thus, it technically doesn't remain a separate entity once the source code is compiled. The statements in macro definition becomes part of program instructions, just like other statements.

The motive behind writing a macro is to make writing and managing source code easier for the programmer. Macros are generally desired for simpler tasks where writing a full-fledged function would be a performance overhead/runtime penalty. Examples of situations where a macro is preferable over function are:

  • Using constant values (such as mathematical or scientific values), or some program specific parameter.

  • Printing log messages or handling assertions.

  • Performing simple calculations or condition checks.

When using macro, it's easy to make changes/corrections in one place which are instantly available everywhere the macro is used in the program. A simple recompilation of program is required for the changes to take effect.

Function code on the other hand is compiled as a separate unit within the program and gets loaded in the memory during program execution only if it's required. The function code retains it's independent identity from the rest of the program. The loaded code gets reused if the function is called more than once. When the function call is encountered in the running program, the control is passed to it by the runtime subsystem and the context of the running program (return instruction address) is preserved.

However, there's a slight performance penalty that needs to be encountered when calling a function (context switching, preserving return address of the main program instructions, passing parameters and handling return values etc.). Hence, the use of function is desired only for complex blocks of code (against macros which handles simpler cases).

With experience, a programmer makes a judicious decision as whether a piece of code will be a good fit as a macro or function in the overall program architecture.

Note: This answer was not written keeping technical accuracy and broad generalization in mind. It was a humble attempt to explain in simple language the difference between macros and functions without being being complete and accurate. C programming language was in my mind when drafting the answer, but my apologies for not mentioning it clearly and causing (potential) confusion.

I really regard the answer shared by Jörg W Mittag. It was insightful reading it and I upvoted it soon after it was posted.

I will leave this answer here hoping it may be helpful for software development newbies.


Both macro and function represent self contained unit of code. They both are tool which aid in modular design of a program. From the point of view of the programmer who's writing the source code, they appear quite similar. However, they differ in how they are handled during the program execution lifecycle.

A macro is defined once and used in a lot of places in a program. Macro gets expanded inline during the pre-processing stage. Thus, it technically doesn't remain a separate entity once the source code is compiled. The statements in macro definition becomes part of program instructions, just like other statements.

The motive behind writing a macro is to make writing and managing source code easier for the programmer. Macros are generally desired for simpler tasks where writing a full-fledged function would be a performance overhead/runtime penalty. Examples of situations where a macro is preferable over function are:

  • Using constant values (such as mathematical or scientific values), or some program specific parameter.

  • Printing log messages or handling assertions.

  • Performing simple calculations or condition checks.

When using macro, it's easy to make changes/corrections in one place which are instantly available everywhere the macro is used in the program. A simple recompilation of program is required for the changes to take effect.

Function code on the other hand is compiled as a separate unit within the program and gets loaded in the memory during program execution only if it's required. The function code retains it's independent identity from the rest of the program. The loaded code gets reused if the function is called more than once. When the function call is encountered in the running program, the control is passed to it by the runtime subsystem and the context of the running program (return instruction address) is preserved.

However, there's a slight performance penalty that needs to be encountered when calling a function (context switching, preserving return address of the main program instructions, passing parameters and handling return values etc.). Hence, the use of function is desired only for complex blocks of code (against macros which handles simpler cases).

With experience, a programmer makes a judicious decision as whether a piece of code will be a good fit as a macro or function in the overall program architecture.

Note: This answer was not written keeping technical accuracy and broad generalization in mind. It was a humble attempt to explain in simple language, the difference between macros and functions without being complete and accurate (in fact it's far from being accurate). C programming language was in my mind when drafting the answer, but my apologies for not mentioning it clearly and causing any (potential) confusion.

I really regard the answer shared by Jörg W Mittag. It was insightful reading it (how less I know) and I upvoted it soon after it was posted.

I will leave this answer here as it may be helpful for software development newbies.


Both macro and function represent self contained unit of code. They both are tool which aid in modular design of a program. From the point of view of the programmer who's writing the source code, they appear quite similar. However, they differ in how they are handled during the program execution lifecycle.

A macro is defined once and used in a lot of places in a program. Macro gets expanded inline during the pre-processing stage. Thus, it technically doesn't remain a separate entity once the source code is compiled. The statements in macro definition becomes part of program instructions, just like other statements.

The motive behind writing a macro is to make writing and managing source code easier for the programmer. Macros are generally desired for simpler tasks where writing a full-fledged function would be a performance overhead/runtime penalty. Examples of situations where a macro is preferable over function are:

  • Using constant values (such as mathematical or scientific values), or some program specific parameter.

  • Printing log messages or handling assertions.

  • Performing simple calculations or condition checks.

When using macro, it's easy to make changes/corrections in one place which are instantly available everywhere the macro is used in the program. A simple recompilation of program is required for the changes to take effect.

Function code on the other hand is compiled as a separate unit within the program and gets loaded in the memory during program execution only if it's required. The function code retains it's independent identity from the rest of the program. The loaded code gets reused if the function is called more than once. When the function call is encountered in the running program, the control is passed to it by the runtime subsystem and the context of the running program (return instruction address) is preserved.

However, there's a slight performance penalty that needs to be encountered when calling a function (context switching, preserving return address of the main program instructions, passing parameters and handling return values etc.). Hence, the use of function is desired only for complex blocks of code (against macros which handles simpler cases).

With experience, a programmer makes a judicious decision as whether a piece of code will be a good fit as a macro or function in the overall program architecture.

4 Added a note clarifying the intent behind writing this answer, after observing the recent voting patterns.
source | link
3 added 1397 characters in body
source | link
2 added 1397 characters in body
source | link
1
source | link