1

What are some use cases of do nothing function (in C) like:

dummy() {}

I am reading "The C programming Language" by K&R and in chapter 4 (Functions & Program Structures) , it mentions that

A do-nothing function like this (shown above) sometimes useful as a place holder during program development.

Can anyone explain what author means by that and uses of these types of functions?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
foxtrot9
  • 324
  • 5
  • 14

4 Answers4

1

A function that does nothing (yet) is an indicator that something should be done, but hasn't been implemented yet.

Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48
1

Have a look at programming idioms like "Skeleton", "dummy code", "mock objects", etc. It is a very common technic that let you test the whole architecture of your program without having implement every details.

Suppose you have an application that is able to save results in a file by calling a function save(), you would like to be able to test the application without the necessity to really save results into a file. So the function save() can be, in a first approximation an empty one! It is very common to write like (pseudo-code here):

save() {
    print "save() not yet implemented";
}

to be able to track the calls.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
1

You already have received the answer(s), but just to elaborate on why part, let me add my two cents.

If you try to use a function (call a function) which does not have a definition, linker will throw undefined reference error, because it will not able able to find the actual function definition anywhere in the given object file(s) to link to.

So, sometimes in the development phase, when you need to call an API which is yet to be implemented (or not available till time), a dummy function definition is added, which

  • either does nothing meaningful
  • or returns a fixed value

just to compile and check (test) the working of the caller function (module). Without the dummy function definition, the code for the caller function will throw the error.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

When you're writing code to explain something, it is better to omit secondary functions, so the reader focuses on the problem and not on the details.

Instead when you write a real program I prefer to create the secondary functions first, test them and alter set all focus on the mainstream.

danilonet
  • 1,757
  • 16
  • 33