Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use define keyword to define a function

I'm reading some source code. And not only this source code, but also many others, use this kind of programming:

#define DO_BINOP(FN_NAME,TFLAG,OPER) \
   void FN_NAME (bigint_stack &stack) { \
      bigint right = stack.front(); \
      stack.pop_front(); \
      TRACE (TFLAG, "right = " << right); \
      bigint left = stack.front(); \
      stack.pop_front(); \
      TRACE (TFLAG, "left = " << left); \
      bigint result = left OPER (right); \
      TRACE (TFLAG, "result = " << result); \
      stack.push_front (result); \
   }

DO_BINOP(do_add, '+', +   )

They use define to define a long-long string that acts as a function. I don't know what the benefit of doing this is, because this way makes the program harder to read, and harder to debug. I'm new to C, so I think this way must have some benefit, right?

Thanks.

like image 376
Trần Kim Dự Avatar asked Apr 23 '14 17:04

Trần Kim Dự


People also ask

Why we use define function?

Quite often when carrying out a project, some set of tasks will need to be repeated with different input. To ensure that the analyses are carried out the same way each time, it is important to wrap the code into a named function that is called each time it's needed.

Can #define be used for functions?

You can use #define anywhere you want. It has no knowledge of functions and is not bound by their scope. As the preprocessor scans the file from top-to-bottom it processes #define s as it sees them.

Why we use define function in Python?

Functions that readily comes with Python are called built-in functions. Python provides built-in functions like print(), etc. but we can also create your own functions. These functions are known as user defines functions.

What keyword is required to define a function?

Defining a Function Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ). Any input parameters or arguments should be placed within these parentheses.


1 Answers

Because otherwise you'd have a source code that looks like this:

void do_add (bigint_stack &stack) {
    bigint right = stack.front();
    stack.pop_front();
    TRACE ('+', "right = " << right);
    bigint left = stack.front();
    stack.pop_front();
    TRACE ('+', "left = " << left);
    bigint result = left + (right);
    TRACE ('+', "result = " << result);
    stack.push_front (result);
}

void do_subtract (bigint_stack &stack) {
    bigint right = stack.front();
    stack.pop_front();
    TRACE ('-', "right = " << right);
    bigint left = stack.front();
    stack.pop_front();
    TRACE ('-', "left = " << left);
    bigint result = left - (right);
    TRACE ('-', "result = " << result);
    stack.push_front (result);
}

Etcetera...

Now, if you want to add another TRACE, for example, you'd have to again copy-paste it to all of them.

What the author wanted, really, is to define a way to generate functions from a set of parameterized inputs, in such a way that the resulting functions are all similar but they behave in slightly different ways depending on the input given to generate them. It's called meta-programming. Very common in coding parsers, which I suspect is where this snippet came from.

Now, in other languages, a construct specific to that language might exist to do meta-programming like this in a cleaner way (templates, metaclass, etc). But for C, macro is it.

like image 159
Santa Avatar answered Sep 19 '22 07:09

Santa