Thread: functions

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    266

    functions

    What happens if a function that is defined is never called? Will it be deleted or just waste space after the program is compiled?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Most likely the latter unless you have an aggressive compiler that destroys it.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Why would you write a function that is never called?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM