Results 1 to 2 of 2

Thread: Functions, Prototype, and defining questions, basic questions

  1. #1

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Question

    Hello everyone!

    I have a question about Functions and their prototypes! Alright well:
    1) I want to keep the code clean and readable, so do I place the Prototype in its own sperate file, and can u tell me what i put in the Prototype besdies its Return Type, Its name, and its arguments? And where do i put the Real Fucntion at? like say i want to call the fucntion were do i declare the fucntion? do i put it under the Prototype in the seperate .cpp file or what?
    2)How do i call the fucntion if its in the sperate file, and where do i name the "Header" so i can #include it in the main() or dont' i even do that? sorry for the confusion but im just a newbie


    Thanks for listening, Simple Examples would be nice 8)

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    A prototype is basically the function's name and parameter list:
    Code:
    void MyFunction(int param);
    So, you'd put this into a header file, making sure to have the multi-include-protection:
    Code:
    #ifndef __HEADER_H__ // replace with filename of your header
    #define __HEADER_H__
    
    void MyFunction(int param);
    
    #endif // __HEADER_H__
    This should be in the same folder as your source file:
    Code:
    #include <iostream.h>
    #include "header.h" // NOTE QUOTES NOT ANGLE-BRACKETS
    
    void MyFunction(int param) {
        cout << param << endl;
    }
    Then, in your main file:
    Code:
    #include <iostream.h>
    #include "header.h"
    
    void main() {
        MyFunction(5);
    }
    However, you need to make sure that both source files have been compiled or you'll get a linker error that it cannot find a definition for MyFunction.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width