One possibility is to put just the function in a separate file, let's say justmyfunction.c:
#ifdef SOMEFEATURE
void myfunction_withfeature()
#else
void myfunction_withoutfeature()
#endif
{
printf("Always doing this.\n");
#ifdef SOMEFEATURE
printf("Doing it one way, with the feature.\n");
#else
printf("Doing it another way, without the feature.\n");
#endif
printf("Always doing this too.\n");
}
And then #include it in the file with the other functions:
#include <stdio.h>
#include "justmyfunction.c"
#define SOMEFEATURE
#include "justmyfunction.c"
int main(void) {
printf("Doing it twice...\n");
myfunction_withfeature();
myfunction_withoutfeature();
printf("Done.\n");
return 0;
}
Or you can do horrible things with macros:
#include <stdio.h>
#define DEFINE_MYFUNCTION(function_name, special_code) \
void function_name() \
{ \
printf("Always doing this.\n"); \
\
special_code \
\
printf("Always doing this too.\n"); \
}
DEFINE_MYFUNCTION(myfunction_withfeature, printf("Doing it one way, with the feature.\n");)
DEFINE_MYFUNCTION(myfunction_withoutfeature, printf("Doing it another way, without the feature.\n");)
int main(void) {
printf("Doing it twice...\n");
myfunction_withfeature();
myfunction_withoutfeature();
printf("Done.\n");
return 0;
}
Or generate the code for the different functions, using a script.