Hello, I've got a basic knowledge of C++ curently, well atleast procedural paradigm programing, but not object-oriented. To me classes look to complex to make with all the different syntax. Is there anything in the C++ world that allows me to use make functions in an external file form, like you can do with classes, so that I can reuse the functions in multiple programs and also to divide a program into multiple files so that it is easier to edit and read. Thanks.
Sure. It's easy.
Option 1: You could put your function definitions in a header file. You won't need any additional .cpp file this way.
myFuncs.h
1 2 3 4
int add(int x, int y)
{
return x+y;
}
Option 2: If you wish to "split up" the prototypes from the definitions place the prototypes in
myFuncs.h
externint add(int x, int y);
then place the definition in myFuncs.cpp
1 2 3 4
int add(int x, int y)// no extern keyword here. That goes in the .h file
{
return x+y;
}
Either way, you then use them in main like this:
1 2 3 4 5 6 7 8 9 10
#include <iostream>
using std::cout;
using std::endl;
#include "myFuncs.h"// the path to your functions
int main()
{
cout << "3+4 = " << add(3,4) << endl;
return 0;
}
I wrote some of my 1st games this way. I think about any task could be done with global functions alone. Classes aren't strictly necessary. Do learn to use them though at some point. The OO thing is powerful and very cool.
Classes did look kind of intimidating to me, but once you actually start messing with them and figuring out how to set them up, they are very easy to use.