Solved

Dummy Funtcions

Posted on 2004-04-04
4
270 Views
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?)
0
Comment
Question by:thekng
4 Comments
 
LVL 2

Expert Comment

by:timbauer
Comment Utility
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?
0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 50 total points
Comment Utility
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,
Kent
0
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 25 total points
Comment Utility
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.

Cheers,
Stefan
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 100 total points
Comment Utility
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.



0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now