Macros Vs Functions? Are They The Same???

Macros Vs Functions? Are They The Same???

Hello guys, I am here again writing this article when I am supposed to be reading for my exams lol... I was actually reading for my compiler construction exam when I came across something called macro and I when I read what it means, I thought to myself if functions and methods can be referred to as macros. I went online to research it and here I am telling you all about it. But before we start, I would like to ask you a question. Jollof Rice and Fried Rice are they the same? I know they are both rice but are they the same??

What are Macros?

Macros are used to make a sequence of computing instructions available to the programmer as a single program statement making the programming task less tedious and less error-prone. (Well that is Wikipedia's definition).

A macro is a block of statements written as a pre-processor directive. They are communicated to the compiler before its starts going through the code to start the actual compilation hence they aren't compiled but processed. They are defined using the pre-processor directive. They are defined once and can be used in many places in the code.

An example of a macro in C programming language is given below:

#include<stdio.h>
#define LOOPTIME 100 // This here is a macro that assigns 100 to LOOPTIME.

int main(){
    print("%d", LOOPTIME) // using the macro that was defined.
}

What are Functions?

Functions are blocks of code that are defined and written to be used in other parts of our code. Functions consist of a group of code blocks that solve a particular problem. Functions are used to enforce the DRY principle (Don't Repeat Yourself). It is a write once use many times principle. It provides us with reusability and modularity.

An example of a function in C programming language is given below:

#include<stdio.h>

int add (a, b) {
    return a + b;
} // A function called add.

// Main Function
int main(){

// getting the return value of the function and assigning it to sum
int sun = add(10, 20) 
    printf("%d", sum); // prints 30
    return 0;
}

Now that we have defined what macros and functions are, let us talk about the differences between them.

Differences between Macros and Functions

MACROS.FUNCTIONS.
They are Pre-processed.They are compiled.
Speed of execution is faster.Speed of execution is slower.
No type-checking as they aren't compiled.Type-checking is done as they are compiled.
They are usually one-liners.They contain multiple lines of code.
Useful for small lines of code that will be repeated multiple times.Useful for large codes that will be repeated multiple times.

Conclusion.

Macros are not Functions although it may look like they do the same thing. Macros are no longer in use like they used to be but functions are an integral part of codes that will forever be in use.

Thank you for reading up to this point. I hope you have enjoyed reading this and I promise I'll be back with more.

Banner Image Credit: Unsplash