How to change returntype of function during runtime?

Hi all,
I am currently a beginner in C++ and I have a doubt.
Suppose I have a function which check for some conditions and if a condition is true, then it returns an integer type, otherwise returns a string. How can I do this? since we have to specify the return type of the function at time of definition. So how can we change that type during runtime?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;

int giveResults();
int main(){
    cout << giveResults() << endl;
}

int giveResults(){
    int input = 0;
    cout << "Enter a value: " << endl;
    cin >> input;
    if(input==1){
        return 1;
    }else{
        return "You didn't enter 1";
    }
}


The function giveResults() checks for a condition (input==1), if this is true, it returns an int 1. I want that if this condition is not true, then it returns a string "you didn't enter 1", is it possible to do so. (The above code shows an error). How can I change the returntype of giveResults() during runtime?

Thank you
-Himansh
It's not possible. Why not let the function return a string, always? If input is "1" it could return the string "1". That should work fine at least if you only going to print the result of giveResults().
You can't change the type of the return.

Normally, one would return a value, then write code to take appropriate action. Assign the value returned by the function to a variable, then use an if statement to print out any errors.

HTH

You might use the returned value to indicate success or failure, and then choose what action to take depending on that value.

For example the program could receive the string variable by reference like this:
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
#include <iostream>
#include <string>

using namespace std;

int giveResults(std::string & str);
int main()
{
    string s;
    int n;

    n = giveResults(s);

    if (n >= 0)
        cout << n << endl;
    else
        cout << s << endl;

    return 0;
}

int giveResults(std::string & str)
{
    int input = 0;
    cout << "Enter a value: " << endl;
    cin >> input;

    if (input==1)
    {
        return 1;
    }
    else
    {
        str = "You didn't enter 1";
        return -1;
    }
}


Or perhaps like this, using a boolean type for success or failure:
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
#include <iostream>
#include <string>

using namespace std;

bool giveResults(int & number, std::string & str);
int main()
{
    string s;
    int n;
    bool ok;

    ok = giveResults(n, s);

    if (ok)
        cout << n << endl;
    else
        cout << s << endl;

    return 0;
}

bool giveResults(int & input, std::string & str)
{
    input = 0;
    cout << "Enter a value: " << endl;
    cin >> input;

    if (input==1)
    {
        return true;
    }
    else
    {
        str = "You didn't enter 1";
        return false;
    }
}



In the latter example, this can be simplified by testing the return value of the function directly in the if condition:
1
2
3
4
    if (giveResults(n, s))
        cout << n << endl;
    else
        cout << s << endl;
Last edited on
Thank you all. Thanks Chervil for writing down this program for me. It gave me the idea of how we can overcome this problem :)
-Himansh
To start with, your function is badly named and badly behaved.
- a function's name should clearly state what it does
- it should behave consistently

(Chervil's suggestion has solved the second part.)

But reverse engineering your code to extract the requirements, I would probably implement the required behaviour something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

using namespace std;
bool getSpecificNumberFromUser(int numberWanted);
int main(){
    int numberWanted = 1;
    if( getSpecificNumberFromUser(numberWanted) )
        cout << numberWanted << endl;
    else
        str = "You didn't enter " << numberWanted  << endl;
    return 0;
}

bool getSpecificNumberFromUser(int numberWanted){
    int input = 0;
    cout << "Enter a value: " << endl;
    cin >> input;
    return (input==numberWanted);
}


Also, as Peter87 has said, while it is not possible to change the return type of the function, it is possible to return a type that can represent (or store) multiple types. In addition to strings, you could also use a variant, like the one in the Boost library:

Boost.Variant
http://www.boost.org/doc/libs/1_53_0/doc/html/variant.html

Using the variant with your original code, you get

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <boost/variant.hpp>

using namespace std;
using namespace boost;

variant< int, std::string > giveResults();
int main(){
    cout << giveResults() << endl;
}

variant< int, std::string > giveResults(){
    int input = 0;
    cout << "Enter a value: " << endl;
    cin >> input;
    if(input==1){
        return 1;
    }else{
        return "You didn't enter 1";
    }
}


(But my above comments still hold; this is not a particularlly nice solution or use of Boost.Variant.)

Andy
Last edited on
I've only seen this possible in python, but not in any non scripting language
@andywestken
Thanks for your precious advice. :)

-Himansh
Topic archived. No new replies allowed.