Using strings in a function

Can you use strings in a function? For example:


void example( string example )
{
if( example == "fwd")
{
motor[1] = 127;
}
else if( example == "bkwd")
{
motor[1] = -127;
}
}

task main()
{
example( "fwd" );
}

I tried this but I keep getting errors. Am I doing something wrong?

Never mind I found this post.

So did the post you found really solve your problem. Because after reading it, I wouldn’t expect it to. Neither your code nor the thread you link compares strings in a meaningful way. Given that, I wouldn’t expect you to have solved your problem yet.

If you did solve it on your own, you should post how you did it to help people that find this thread in the future.

What I wanted to do was to input words into a function and then use those words for if else statements. I first thought you had to do this with strings, but after reading that post I learned that you use characters instead.

First, the thread you linked suggests using char pointers, not chars. That’s fine, but unnecessary; it doesn’t solve your actual problem.

In order to compare two strings (or arrays of characters; whichever you end up using) you have to call a string compare function. Look at strncmp on this page:

http://www.robotc.net/wikiarchive/General/Strings

Speak up if you need help beyond that.

Based on the code you posted, you’d better use enums anyway.
enum - a typed set of named constants.


typedef enum {
    fwd,
    bkwd
} action;

void example(action example) {
    if( example == fwd) {
        motor[1] = 127;
    } else if( example == bkwd) {
        motor[1] = -127;
    }
}

task main() {
    example( fwd );
}

I didn’t know about this! I’ll use this instead.