Thread: Function prototypes in the header.

  1. #1
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59

    Function prototypes in the header.

    I have a pretty big project, and my main has lot of function prototypes.
    Is it wrong to put the prototypes in the header file and import it in all the functions of the project? It certainly would give ease in reading the code, but I don't know if it's a "logical" mistake.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DeliriumCordia
    Is it wrong to put the prototypes in the header file and import it in all the functions of the project?
    That sounds wrong to me. Your functions should be grouped together in some way, i.e., you will have several functions that are part of the same interface. These functions would be implemented in the same source file (or in source files in the same directory) and forward declared in the same header, similiar to say, how I/O related functions are declared in <stdio.h>. Then you should include the appropriate header in the source files (not the functions) where those functions are used.

    That said, for convenience some libraries do provide a "mother of all header files" that includes every other header file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    Ok, so you suggest me to divide the functions in "logical" modules, and maybe put all the prototypes of a certain module in a specific header that will be imported only in the functions of that module.

    It sound pretty right to me, but in my project all the functions are placed in the same directory (maybe I should make some order, but the project has no more than 20 functions, and it's pratically all handled by the main function).

    That's also because I'm not pretty able with pointers etc. so I prefer the main to use functions, but not the functions to call for other functions if I can.

    By the way, because of this certainly wrong order of the sources files, I find my main file to have lot of prototypes and pretty long code, while the other functions have very short prototypes lists, that's why I was asking this question.

    Maybe I could write a header file just for the main, with only the prototypes regarding the main function?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DeliriumCordia
    maybe I should make some order, but the project has no more than 20 functions, and it's pratically all handled by the main function
    In that case, you might not even need a separate header file. You could just forward declare all 20 or so functions, then define them in the same source file that you define the main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    So you mean to declare all prototypes in a header, maybe called "prototypes.h", and import it in each function? Or keeping this header just in the main function and declaring prototypes manually in the other functions?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, I mean that with so few functions, you might not even need a header file. But if you want to declare them all in a header file that is included in the source file containing the main function, that's fine too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function prototypes
    By kawaikx15 in forum C Programming
    Replies: 1
    Last Post: 06-28-2011, 09:20 PM
  2. Function Prototypes
    By askinne2 in forum C Programming
    Replies: 6
    Last Post: 10-01-2010, 01:01 PM
  3. Function prototypes
    By XxPKMNxX in forum C Programming
    Replies: 4
    Last Post: 10-28-2009, 02:33 AM
  4. Function Prototypes
    By Kryota in forum C++ Programming
    Replies: 11
    Last Post: 12-28-2007, 04:52 AM
  5. Header doesn't see prototypes...
    By bikr692002 in forum C++ Programming
    Replies: 11
    Last Post: 04-09-2006, 07:26 PM