functions

This is a discussion on functions within the C++ Programming forums, part of the General Programming Boards category; Since you can write a Function prototype in a seperate header file to include in the main program, can you ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    132

    functions

    Since you can write a Function prototype in a seperate header file to include in the main program, can you write that functions definiton straight after the function prototype in the header file?

    like
    Code:
    int FindArea( int LengthofRoom, int WidthofRoom );
    int ReturnLength ( int L );
    
    
    int FindArea( int Length, int Width )
    {
    return L * W;
    }
    
    int ReturnLength ( int LE )
    {
    return RoomLength;
    }
    and then save this all in an header file, include in main program and call the functions in there, would it work just as if i had used the prototype before the main function, and the definition after the main function has ended?

    hugo.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    You can do that, of course, but there is a logical reason you don't.

    You can prototype a function a million times over again and it doesn't mean a thing. So long as they're all the same. You can only have one definition however. This is why definitions go in source files and prototypes go in headers.
    Sent from my iPadŽ

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    Function prototypes, inline functions, class definitions... lots of things go into header files, but unless your function is inlined you risk getting a linker error. Separate implementation from the header and give it a cpp file.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    Hmmm, you mean, once the function is used once, the definition is gone? so the function wont work again?

    its just easier to read if u put it all in one header file. but then again, adding the defination to the end of the source file one time also makes it easy to read i guess.

    I am just looking into function a bit more so i was just wondering if its a good way, but obviously not lol.

    Thankyou.

    Hugo.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Every cpp file in your project is compiled separately. If you define a function in a header, and that header is included in more than one cpp file, then you will be compiling that function more than once. This will cause linker error complaining about re-definition when you try to build your program. It has nothing to do with how often the function is used.

    That is why you would normally put a prototype in the header, then the definition in its own cpp file. That cpp file will be compiled once, and so the function will only be defined once during the building of the program.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Something like this:
    Code:
    /* myfunc.h */
    #ifndef MY_FUNC_H
    #define MY_FUNC_H
    
    void myFunc();
    
    #endif
    Code:
    /* myfunc.cpp */
    #include <iostream>
    #include "myfunc.h"
    
    void myFunc() {
       std::cout << "This is my function.";
    }
    Code:
    /* main.cpp */
    #include "myfunc.h"
    
    int main() {
       myFunc();
    
       return 0;
    }
    Then add both source files to a project, compile.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    Oh i get what you mean.

    So its best to put all functions in a header file, then make a CPP with all the definitions, then include them in the main CPP?

    is this correct, sorry guys, functions have always been my biggest downfalls in programming.

    Thanks for all the help guys.

    Hugo.

  8. #8
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Yep that's about right. All the function declarations (prototypes) go in a header.
    In my larger projects I also have an "inclusion point" (usually called Shared.h or Stdinc.h) that includes commonly used headers and maybe defines some globals.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  9. #9
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    ok thanks alot, ill check the links out. thanks guys, appreciate the help.

    Hugo.

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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21