Define name and function

DEF FN define & name a function written by user DEF FNAB(X,Y)=X^3/Y^2

How do you write this in C++?
Are you trying to write something that will interpret user input as a function?

Or do you just need to spend more time studying C++?
http://www.cplusplus.com/doc/tutorial/
I am converting some GWBasic programs to C++ I simply need an example of how to do the following:


10 DEF FNMAX(X,Y) IF (Y>X) THEN FNMAX=Y ELSE FNMAX=X
20 DEF FNMIN(X,Y) IF (A<B) THEN FNMIN=A ELSE FNMIN=B
30 DEF FNLOG10(X) LOG10=LOG(x)/LOG(10)

What I have read does not seem to give me what I need or at least is not clear enough to me.
If you don't need to write functions for min, max and log yourself as an assignment then C++ has these built in for you.

Look at http://www.cplusplus.com/reference/algorithm/max/ or http://en.cppreference.com/w/cpp/algorithm/max

and http://www.cplusplus.com/reference/clibrary/cmath/log/ or http://en.cppreference.com/w/cpp/numeric/math/log
He needs to read the tutorial.
My WinDef.h (called from Windows.h) looks like this:
1
2
3
4
5
6
7
8
9
10
11
#ifndef NOMINMAX

#ifndef max
#define max(a,b)            (((a) > (b)) ? (a) : (b))
#endif

#ifndef min
#define min(a,b)            (((a) < (b)) ? (a) : (b))
#endif

#endif  /* NOMINMAX */ 


Adopting this for your C++ program, try this:
1
2
3
#define FNMAX(X,Y)        (((X) > (Y)) ? (X) : (Y))
#define FNMIN(X,Y)        (((X) < (Y)) ? (X) : (Y))
#define FNLOG10(X)        (log(X)/log(10))  

Don't forget to #include <math.h> to get the log(x) function.

Note that there IS a problem with doing this. the compiler directly replaces the define call in your source with the string above so if you do something like this:
cout << FNMAX(i++, 0);
The compiler will compile this:
cout << (((i++) > (0)) ? (i++) : (0));
i got incremented twice! It's almost always better to use a function call to avoid this sort of problem, but it's up to you.

If it were me, I'd use a template function which is like a normal function but it will accept any type like a define:
1
2
template <typename T>
  T max(T a, T b) { return (a>b)?a:b; }

Last edited on
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
Last edited on
Topic archived. No new replies allowed.