David542
David542

Reputation: 110502

Difference between a macro and function (outside C)

There have been a few different answers about the difference between a function and macro within C-like languages, such as What is the difference between a macro and a function in C?. However my question is more a conceptual one about what a function gives us or can do that a macro cannot by its very nature. For example, take the following example:

# m4
define(add2, `eval($1+$2)')
add2(2,3)
# 5
# python
def add2(x,y):
    return x+y
>>> add2(2,3)
# 5
int add2(int a, int b) {
    return a+b;
}
add2(2,3)
# 5

Off the top of my head it seems the following are the limitations of using a macro:

What else can a function do that a macro cannot? For example, if I went through a large program and tried to remove all functions and use macros instead, what types of things would I not be able to do?

Upvotes: 1

Views: 6498

Answers (4)

brunoff
brunoff

Reputation: 4219

For clarity let's assume that macros stands for General-purpose macro processors (not VBA, vimscript, Lua). And also that it is being used to generate text code for a language that has functions - which we will call as the "main" language.

In the cited example, both C and the M4 are Turing Complete (M4 does have its own flavor of recursion, as stated in the docs which is important to achieve Turing completeness). By being Turing Complete, you can do with a M4 macro any computation that a C function would do. You could theorically process the pixels of a fractal in M4, compute 3d images in M4 or mine bitcoins in M4.

You for example decide to implement a sudoku puzzle solver in M4 (or in any other Turing Complete "macro" language). It takes a text input representation of the puzzle and then output the solution. It would be possible to do it only with the macro langague, without any external language. Being possible does not mean being easy, you would probably suffer with (1) weaker syntax and (2) ausence of tooling.

(1) Being Turing complete does not mean being practical. If the language has a weak syntax it will be hard to implement things that are easy on more featured syntaxes. In real scenarios, the "main" language will be more featured and so it will be easier to use it most of the time. (Your "main" language probably won't be BrainFuck!).

(2) You probably won't have many tooling at your disposal. Probably nobody will have implemented a type checker, a debugger and other tools for your "macro" or combination of languages (not counting GDB that would debug the whole pre-processor engine, here meaning a specific debugger for the macro language).

So responding to the question: "What can a function do that a macro cannot? For example, if I went through a large program and tried to remove all functions and use macros instead, what types of things would I not be able to do?"

Answer - It depends on which "main" and "macro" languages are being used. Probably the "main" language will be Turing complete and more featured than "macro", which itself calls for avoiding "macro", but if "macro" if Turing complete you would theorically do any computation doable in "main", providing it necessary IO access, as said on the sudoku solver example (where in fact the "macro" language assumes the role of a "main").

[edited answer]

Upvotes: 0

Fabio Bonfante
Fabio Bonfante

Reputation: 5198

Given the "conceptually wider question", a function could be a "real thing" like an argument of another function. In Javascript a function is actually a "callable" object, and it's common to pass function as arguments. Since Java 8 you have lambda functions and method references that help to compose behavior of your objects.

Considering the "macro" (in C-like languages) essentially a pre-processor, I can't imagine it supporting these features, given also the need of making closure of variables in the scope of the functions.

Upvotes: 0

Abdul ahad
Abdul ahad

Reputation: 1433

A macro is a name given to a block of C statements as a pre-processor directive. Being a pre-processor, the block of code is communicated to the compiler before entering into the actual coding (main () function).

A macro is defined with the pre-processor directive. Macros are pre-processed which means that all the macros would be processed before your program compiles. However, functions are not preprocessed but compiled.

//////////////////// Benefits of using a function //////////////////

  1. In macros, no type checking(incompatible operand, etc.) is done and thus use of macros can lead to errors/side-effects in some cases. However, this is not the case with functions.

  2. Macros do not check for compilation error (if any).

  3. Macros are usually one liner. However, they can consist of more than one line.There are no such constraints in functions.

  4. Difficult to debug as they cause simple replacement.

  5. Macro don’t have namespace, so a macro in one section of code can affect other section

For Example:

Macros:

#include<stdio.h>
#define CUBE(b) b*b*b
int main()
{
     printf("%d", CUBE(1+2));
     return 0;
}

Functions:

#include<stdio.h>
int cube(int a)
{
     return a*a*a;
}
int main()
{
    printf("%d", cube(1+2));
    return 0;
}

However there are special type of functions called In-line functions that are substitute of macros because they are faster than ordinary functions.

Compiler interprets the inline keyword as a mere hint or request to substitute the code of function into its function call. Usually people say that having an inline function increases performance by saving time of function call overhead (i.e. passing arguments variables, return address, return value, stack mantle and its dismantle, etc.)

Example of In-line functions:

#include <stdio.h>

void inline test_inline_func1(int a, int b) {
    printf ("a=%d and b=%d\n", a, b);
}

int inline test_inline_func2(int x) {
    return x*x;
}

int main() {

    int tmp;

    test_inline_func1(2,4);
    tmp = test_inline_func2(5);

    printf("square val=%d\n", tmp);

    return 0;
}

Upvotes: 2

NeoTheNerd
NeoTheNerd

Reputation: 656

A Macro is a series of statements that instructs a program how to complete a task. Macros allow users to automate routine, repetitive, or difficult tasks in application software such as word processing, spreadsheet, or database programs.

Functions are sets of codes that are designed to accomplish a specific task. Functions often “take in” data, process it, and “return” with a result. When function is once written, you can use it again and again. Or you can also call a function within another function. Many codes are combined into a single line of code to create functions.

enter image description here

Upvotes: 2

Related Questions