// the file name of this header file is MyHeader.h
#ifndef _MYHEADER_H // if not defined
#define _MYHEADER_H // define MyHeader
#include <stdio.h> // we can now include our extern files here
#include <iostream>
using namespace std;
// declare function 1 in our new header file
void myFunc_1();
// declare function 2 with an argument in our new header file
void myFunc_2(int y);
// declare function 3 with an argument in our new header file, returns an integer
int myFunc_3(int z);
#endif // end if not defined, and end the header file
/*
This is how I setup a standard header files in c/c++
this method makes shure the header file is only defined once
#ifndef _NAME_H
#define _NAME_H
// header file content
#endif
*/
// important remember to include our new header file
// (we use "" because the file is in our program folder.)
#include "myheader.h"
int mySum1; // declare variables integer (global for this cpp file)
int mySum2; // when a variable is declared on the top, outside the functions
int x; // it means that all the functions in this cpp-file have access it.
int main()
{
x = 5;
myFunc_1(); // call function 1
cout << mySum1 << endl; // display mySum1
myFunc_2(5); // call function 2
cout << mySum2 << endl; // display mySum2
// display the returned value of function 3
cout << myFunc_3(5) << endl;
getchar(); // wait until a key is pressed
return 0; // return zero;
}
// define function 1
void myFunc_1()
{
mySum1 = x + 5;
}
// define function 2 with an argument
void myFunc_2(int y)
{
mySum2 = y + 3;
}
// define function 3 with an argument, must return an integer
int myFunc_3(int z)
{
return z + 2;
}
/*
Learn to use a header file in c/c++.
This program is the same program as in the Function Tutorial.
The only difference is that this program uses an extra header file.
The header file can be used to gain global access to your functions.
(The Source Tutorial will demonstrate this.)
It can also be used to obtain more structured programming as
your program grow larger.
*/
Download Visual C++ Source Code
I hope you found this c++ tutorial useful!
Don't forget to mention Apron Tutorials in your References!