What happens if a function that is defined is never called? Will it be deleted or just waste space after the program is compiled?
What happens if a function that is defined is never called? Will it be deleted or just waste space after the program is compiled?
Most likely the latter unless you have an aggressive compiler that destroys it.
If the function has internal linkage (static keyword) or is in an anonymous namespace, you'll probably get a warning from the compiler or linker. In addition, the tool will probably elide the function.
If the function has external linkage, you won't get a warning, and the thing won't be eliminated.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Why would you write a function that is never called?
>> Why would you write a function that is never called?
There are many reasons. Off the top of my head, you're writing (and implementing) a complete interface but the rest of the app is not yet ready to use it.
Or what about a library you're linking into your app, do you really use all the functions in every library you include?
Libraries are a special case. The linker picks only those parts of a library that are actually required. (Granularity differs. The MS linker is per-function, while typical *nix linkers are per-object file.)
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law