returning the value of a string in a function

i need to return the value of a string and a integer in a function, but the compiler wont let me. i know why but i need a way to get around this.

space1,2,3,4,5,6,7,8,9 are strings.
this function will check the ai score for tic tac toe based on the spaces the player has picked and place it accordingly. but i need to change the value of the string so the display on the board changes. and i need to make this a function because i use this code so many times in the program it would shorten it greatly just having to call the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
                  if (AIscore < 10)
                    {

                        Random = (rand()%4+1);

                              switch(Random)
                             {
                                    case 1: space2 = "O"; break;
                                    case 2: space4 = "O"; break;
                                     case 3: space6 = "O"; break;
                                     case 4: space8 = "O"; break;
                             }

                    }

                        if (AIscore > 10)
                          {
                                   if(AIscore < 20)

                                        {
                                                       space5 = "O";
                                         }
                             }
           if (AIscore > 20)
               {

                               Random = (rand()%4+1);

                               switch(Random)
                                  {
                                        case 1: space1 = "O"; break;
                                        case 2: space3 = "O"; break;
                                        case 3: space7 = "O"; break;
                                        case 4: space9 = "O"; break;
                                   }

                            }
             }
Last edited on
Whoa that's some bad indenting. I've seen worse (as some will remember) but oog. In addition I would not recommend stacking up statements on one line.
It is legible however so I will try:
You cannot ever return two values simultaneously. I'm sure you know that. In order to make a change in the function be reflected in the original object you must pass by reference. (I'm sure you knew that too.) So pass in the strings by reference. Then you can return the integer while the changes that you make in the function to the passed-in-strings will be made in the original strings you passed in. Where's the integer you need to pass anyway?
AIscore is the integer being returned. i have returned 2 values before wthout any noticable problems. is that a no no or something?(ya sorry for the bad identing. copy and paste messed up my indents). but now that i think about it i dont need to return aiscore, i just need to put it in the function to be read.
Last edited on
You can' t return two values. The compiler will reach one return and it will never reach the other.
1
2
return int1;
return int2; // Because of the return above this code is never reached, the function is already exited by the first return 

You cannot receive two values from a function at the same time ever. That's a simple fact. Here's what would happen if you could:
foo = functhatreturns2vars(int1, int2); // which return to use?
So yes it is wrong and no you will not get any syntax errors but you will get logic errors. It's veritably impossible unless you create an object to contain a set of values to return, or an array.
Last edited on
can you explain the first post a little better. im decent a c++ but i am 100% self taught and have had no formal training so it takes me a little longer to fully understand everything(especially since functions are kind of new to me, even though the basics are fairly straight foward)
When a function sees a return that returns an appropriate type, it exits. IMMEDIATELY. That means that the returns below it are never executed, along with all code that follows the return that worked. So if both the returns in my first code example were valid, the second one would still never execute because the function would end the minute it saw the first return - not sticking around to call the second one, which would be a serious language error. See the documentation tutorial on functions.
EDIT: Sorry, if you're talking about pass by reference you should still check the tutorial on functions. Basically it's passing an object or variable like so:
int passbyref(string& s1, string& s2);
You can pass it normal objects but it will receive a reference to those objects. You can handle the parameters as though they are normal objects but all changes will be happening to the original object you passed.
If you pass an object normally it creates a copy of the object. All changes within the function are not reflected in the variables upon which the function was called. It's like doodling on someone's photograph; their face doesn't change to reflect your doodle.
Last edited on
so even with
 
return x, y;


y wont be returned?
NO. That should be a syntax error! How can you even compile that?
i have no clue. i did that with 5 variables in the same line and it said nothing(Code::Blocks v8.02)
Compiler extension I'd bet. Don't do it, it's wrong. That's all there is to the matter. No discussion to be had.
It's because that's not a syntax error. You can run several expressions in a single statement by separating them with commas. The statement as a whole evaluates to the value of the last expression.

EDIT: To be more accurate, you can compound several expressions into one, and the new expression evaluates to the last one, such that (... , x)==x.
Last edited on
Really, it isn't?
Wow, that really lends to ambiguity.... I'm right in saying that only one of the returns will occur though right? (I'm becoming dangerously self-doubting...)

To be more accurate, you can compound several expressions into one, and the new expression evaluates to the last one, such that (... , x)==x.


cool i was looking for how to do that but i never figured out how.

@tummychow - ok
Keep in mind that if you throw a crapload of expressions on to one line because you want to combine them together that's crap programming. Combine things only if there is a relevant cause to do so. Otherwise you risk cluttering up your program with statements stacked onto one line.
The comma operator is not meant to be used just because. Most statements in C and C++ are expressions, so if you used it at every chance, your functions would end up looking rather wide and being unreadable.
The most common uses for it are to perform several increments or operations after each for loop, and to write complicated macros:
#define SWAP(a,b,temp) ((temp)=(a),(a)=(b),(b)=(temp))
hes what i got so far. im getting a few errors and a majority is because im trying to return strings...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
int AImove(int &x, string &a1, string &a2, string &a3, string &a4, string &a5, string &a6,string &a7,string &a8,  string &a9,)
{
    int score;
    string a01, b2,c3,d4,e5,f6,g7,h8,i9;
    srand((unsigned)time(0)); //seed
    int Random; //declare creature type

      if (score < 10)
                    {

                        Random = (rand()%4+1);

                        switch(Random)
                        {
                            case 1: 
                                b2 = "O";
                                return b2;
                             
                            case 2: 
                                d4 = "O";
                                 return d4;
                            case 3: 
                                f6 = "O"; 
                                return f6;
                            case 4: 
                                h8 = "O"; 
                                return h8;
                        }

                    }

                    if (score > 10)
                    {

                        if(score < 20)
                            {
                                e5 = "O";
                                return e5;
                            }

                                        if (score > 20)
                                        {

                                                    Random = (rand()%4+1);

                                                    switch(Random)
                                                    {
                                                        case 1: 
                                                            a01 = "O"; 
                                                            return a01;
                                                        case 2: 
                                                            c3 = "O";
                                                             return c3;
                                                        case 3:
                                                            g7 = "O";
                                                             return g7;
                                                        case 4: 
                                                            i9 = "O";
                                                             return i9;
                                                    }

                                        }
                    }



}
That would be because the return type is int.
Also I would advise against calling srand in your function. Call srand once, in main, at the very beginning and don't call it again.
Last edited on
does that mean i could put it as string? or maybe void?
I always say "if you find yourself using numbered variables, it's time to use arrays".
thanks for your advice everyone, ive got it to compile and run. but its got a run time error ill have to figure out. trial and error here i come!
Topic archived. No new replies allowed.