-
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 :D
Thanks for listening, Simple Examples would be nice 8)
-
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.