• C

Dummy Funtcions

Please explain to me what is a dummy function, what is it useful for and what the difference between the other methods (if there are, what are they?)
Who is Participating?
PaulCaswellConnect With a Mentor Commented:
As Kent suggests, the most common use of dummies is in tables of functions. Stephan's callbacks are also common uses, but this is more usually a function pointer rather than a function itself. E.G.:

// Callback before each expansion.
char * (*Expanding) (char * before,char * after) = NULL;


if ( Expanding != NULL ) (*Expanding) ( before, after );

A third use is for what I call link-time coding:

void Expanding ( char * before, char * after ) {};


Expanding ( before, after );


You can then put this code in a library and, with careful use of link parameters, you can write your own 'Expanding' without modifying the library code.

Not sure what you mean about a "dummy function".
A macro is a pure text substitution done by the preprocessor, but kind looks like a function.

Can you rephrase are give more of the context where you heard of a dummy function?
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:
Hi thekng,

A dummy function is usually just an empty function.  At first glance it might seem silly to call such a function, but it can actually make sense in some circumstances.

Function tables are one place where you're likely to see dummy functions.  The following table could be used to process characters based on the characters value.  

int (*ExecuteFunction[256])(void) = {NULL, NULL, NULL, ...,  IsA, IsB, IsC, ...};

  if (ExecuteFunction[Character])
    ExecuteFunction[Character] (ParameterList);

int (*ExecuteFunction[256])(void) = {Ignore, Ignore, Ignore, ...,  IsA, IsB, IsC, ...};

  ExecuteFunction[Character] (ParameterList);

In the first example, ExecuteFunction[Character] must be explicitly checked to make sure that there is a function to execute.  The second example inserts a dummy function (Ignore) into the table.

Hope this helps,
stefan73Connect With a Mentor Commented:
Hi thekng,
Besides the function table use that Kent already mentioned, dummy functions are often used in callback scenarios, such as GUI events. Unlike in Kent's example, in such an environment the API user doesn't have a choice of different implementations, since the calling code is not his (it's the OS/GUI/class lib, etc).

Imagine a situation like this:

struct AllPossibleThingsAUserCanDo{
    event_ptr key_pressed;
    event_ptr left_mouse_click;
    event_ptr right_mouse_click;
    event_ptr window_resized;

Now you want to ignore events -> you need a dummy handler.

Dummy methods (as opposed to functions) are often used in C++ class hierarchies to have a pre-defined interface where some parts of the classes implement some methods as ... dummies.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.