Search code examples
c++functioncall

Calling a function


Pardon me, this must be one of the silliest questions ever asked especially since I've already called one function. I have called one function with one return value and set that return value equal to a variable but with another function that returns 2 variables; I just want to run the function and return the values.

my declaration:

string diagraph ( string mono1, string mono2);

calling the function:

cout << diagraph (mono1,mono2);

The function itself:

string diagraph(string mono1, string mono2) {
    string encoded1,encoded2;
    int a,b,c,d,e,f;
    a = 0;
    b = 0;
    while( mono1 != cipherarray[b][c]){
        b++;
        if (b == 5) {
            a = 0;
            b++;
        }
    }
    a = c;
    b = d;


    a = 0;
    b = 0;

    while (mono2 != cipherarray[b][c]){ 
        b++;
        if (b == 5) {
            a = 0;
            b++;
        }
    }

    a = e;
    b = f;
}

The errors(having to do with calling the function):

C++\expected constructor, destructor, or type conversion before '<<' token 
 expected `,' or `;' before '<<' token 

the function is not finished but it will return 2 strings


Solution

  • First off, I don't see a single return statement in that function. Second, you can't return two values from a function. You can either return a single string (as your function definition says it will) or you can modify passed in values (as long as they are references or pointers).

    EDIT: To elaborate

    If you want to modify passed in values they will need to be references or pointers. This is because the default behavior in C++ is to pass arguments by value (copy), so any change to the functions parameters will not be visible from outside of the function. However, if the arguments are references/pointers you can mutate what they already point to (or pointers to pointers if you want to change what the original pointer points to, i.e., not a mutation, but an assignment to a new value/object).