Thread: Helper functions: design issue

  1. #1
    The larch
    Join Date
    May 2006
    Posts
    3,573

    Helper functions: design issue

    Suppose I have a generally useful function declared in a header that relies on a number of helper functions which are not meant to be called by other functions. Where and how would it be recommended to declare the helper functions? Would there be any issues if they are declared in the implementation file (of the useful function) and not in the header?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would generally declare and define the helper functions in the unnamed namespace of the implementation file, and not mention them at all in the header. I'm not entirely sure if that is best practice, though.
    Last edited by Daved; 02-20-2008 at 04:02 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't see anything wrong with leaving out the function declarations if you don't expect/don't want the functions to be called from outside the file; I've done it myself. And if it those functions turn out to be "useful" too, then you can add the function declarations to your header file later.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Normally, I would use the "Pascal" principle of putting the helper functions above the "external" function, e.g:
    Code:
    #include <somefunction.h>
    
    static int helper1(...)
    {
      ...
    }
    
    static int helper2(...)
    {
    ...
    }
    
    int somefunction(...)
    {
    ...
       x = helper1();
    ...
       y = helper2();
    ...
       return soemthing;
    }
    This is of course both a style issue, and other people may have other ideas of which order you place functions, etc. And if helper1 and helper2 are mutually recursive, then you will of course have to forward declare one to call it from the other.

    By making the functions static, they are not "visible" to other functions.

    --
    Mats
    Last edited by matsp; 02-20-2008 at 03:46 PM. Reason: Adding static to the helper functions.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-16-2006, 04:23 AM
  2. any comments about a cache design?
    By George2 in forum C Programming
    Replies: 6
    Last Post: 09-14-2006, 12:53 PM
  3. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  4. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  5. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 06:46 PM