Dummy Funtcions

Posted on 2004-04-04
Medium Priority
Last Modified: 2010-04-15
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?)
Question by:thekng

Expert Comment

ID: 10753845
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?
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 200 total points
ID: 10754388
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,
LVL 12

Assisted Solution

stefan73 earned 100 total points
ID: 10755504
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.

LVL 16

Accepted Solution

PaulCaswell earned 400 total points
ID: 10755659
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.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question