As Duoas has said, before you attempt the conversion, you really need to check out the tutorial.
For the code you're asking about, you esp. need to see the articles on variables and data Types, operators, functions, and control structures (inc. if statements).
Variables. Data Types.
http://www.cplusplus.com/doc/tutorial/variables/
Operators
http://www.cplusplus.com/doc/tutorial/operators/
Functions
http://www.cplusplus.com/doc/tutorial/functions/
Control Structures
http://www.cplusplus.com/doc/tutorial/control/
But you should read all of the first 8 articles, plus the intro, to get yourself going. And then build upwards from there.
C++ Language Tutorial
http://www.cplusplus.com/doc/tutorial/
Then, to take one example:
DEF FNMAX(X,Y) IF (Y>X) THEN FNMAX=Y ELSE FNMAX=X
Here...
DEF
says you're about to define something (here a function). This just goes away, you don't need it
FNMAX(X,Y)
defines the signature of the function.
- Basic has a very relaxed attitude about function parameters. But C++ needs to know what type they are. This is done by providing the type before each paraeter name. Here you need an int e.g.
int X, int Y.
- C++ also requires you to specify the return type. Here it's also an int, like the parameters. So we end up with
int FNMAX(int X, int Y) // a function that takes 2 int params and returns an int
Next, the body of a C++ function must be located inside a pair of braces
{ }
1 2 3 4
|
int FNMAX(int X, int Y)
{
// TODO
}
|
And each statement must end with a
;
In C++ the if-statement doesn't have a
then. And it's all lower case. (Note here that C++ is a case-sensitive language!)
And finally, for now, the use of the function name as the return value doesn't work in C++. You need to delare a variable yourself. And it's also got to be explictly returned using return.
So we end up
1 2 3 4 5 6 7 8
|
int FNMAX(int X, int Y)
{
int ret = 0; // return value
if (Y>X) ret=Y; else ret=X; // note use of a ;
return ret;
}
|
or in an alternative style (all uppercase names are not generally used for function names in C++, only for macros)
1 2 3 4 5 6 7 8 9 10 11
|
int fnMax(int X, int Y) {
int ret = 0;
if (Y > X) { // see tutorial for rules about if-statements and braces
ret = Y;
} else {
ret = X;
}
return ret;
}
|
Of course, you would not use this function in real programming as it's already provided by assorted libraries, as mentioned earlier in this thread. I just stepped through the conversion of the Basic function into a C++ function with roughly the same structure for illustrative purposes.
Once you know C++, and for some strange reason couldn't use any of the standard versions of min(), you would prob. write the function more along the lines of:
1 2 3
|
inline int my_max(int X, int Y) {
return (X > Y) ? X : Y;
}
|
Which is somewhat different structure to the one above.
Anyway, this will all make a lot more sense after you've read the tutorial articles!!
Andy