0

Since function overloading isn't allowed directly in C, how can it be done for these functions?

void MotorDriver_Create(float speedAddress,float frequencyAddress, float db_input);
void MotorDriver_Create(float speedAddress, float frequencyAddress, float minDB, float maxDB);
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Anku
  • 1
  • 2
  • 6
    You make two functions and call whatever is appropriate. – Eugene Sh. Jan 28 '19 at 22:00
  • That would be the easier solution but I'm not allowed to do that. – Anku Jan 28 '19 at 22:04
  • 2
    @Anku You are not allowed? What does that mean? – Sulthan Jan 28 '19 at 22:06
  • I am trying to develop hardware independent code, that can initialize the values for the variables in the parameters, with one of the functions mentioned above depending on the number of parameters. The code works on Java/ C++, but since the code is supposed to run on C, needed a way around to perform the same operation. – Anku Jan 28 '19 at 22:24
  • This doesn't make much sense. These languages are following completely different paradigms and cannot run the same code. – Eugene Sh. Jan 28 '19 at 22:27
  • Always thought that the compiler generated a different signature for each, so as long as you compile without warnings it should work, is this not so? – LeedMx Jan 28 '19 at 22:33
  • I meant in terms of C++/ Java supporting overloading, against C which doesn't, which is what I was trying to achieve. True, compiling without warnings, would work. – Anku Jan 28 '19 at 22:37
  • @LeedMx, no, it is *not* so (for C). The C language explicitly specifies that a program has undefined behavior if it contains two or more objects or functions with external linkage and the same name. You seem to be thinking of the typical way in which ***C++*** implementations support function overloading (which does not require suppressing any warnings). – John Bollinger Jan 28 '19 at 22:46
  • @JohnBollinger doesn't external linkage refers to the use of `extern` when defining a function? I seem to recall being able to do something like that. to be fair, I always declare and define my functions on the same c file. Haven't really programmed something so big that merits the use of headers. But I can see how header files migth be considered external linkage. – LeedMx Jan 28 '19 at 22:50
  • 1
    @LeedMx the `extern` keyword does specify external linkage, but that is also the default for file-scope declarations, including *all* functions. Only functions declared `static` have internal linkage. There is no context, circumstance, or scope in which C allows different objects or functions to be accessible via the same name, excepting only generic selection, described in the accepted answer, which isn't really the same thing. – John Bollinger Jan 28 '19 at 22:56
  • @JohnBollinger: `#include `? Though it is rather similar to the concept behind `_Generic`. – Jonathan Leffler Jan 31 '19 at 04:32
  • @JonathanLeffler, I think tgmath.h is *exactly* a collection of uses of `_Generic`. At least, that's how I read the first paragraph of its description: "The header includes the headers and and defines several type-generic macros." – John Bollinger Jan 31 '19 at 14:35
  • The `` header was in C99. The `_Generic` feature was added in C11. I suspect the mathematical functions can now be implemented in terms of `_Generic`, but it was not always an it. – Jonathan Leffler Jan 31 '19 at 14:38

1 Answers1

5

There is no way to achieve function overloading akin to what is available in C++ since, unlike in C++, function signatures are not mangled in a way that permits overloading. What you can do is use the _Generic keyword to map a macro into whichever function call best suits the argument type. For example:

#include <math.h>
#include <float.h>
#define sqrt(X) _Generic((X), \
            long double: sqrtl, \
            default: sqrt, \
            float: sqrtf \
)(X)
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85