Unfortunately the question has been changed and you are now asking something completely different from the original, I will leave the original answer below
Even understand that you are learning but what you want to do requires state and can not do so trivial. Guilherme Nascimento's response code works, if you run only this isolated example. If it will be used within a more complex application, you will have problems.
You will have to save the situation in some variable and this variable will have to be known by its function in isolation. If you use a global variable you'll have problems (see more here , here and here ).
You can create a class or at least one structure to do this but in fact as it is something that does not quite fit a new data structure does not need so much, can do something simpler, something like this:
#include <iostream>
int getNumber(bool &check) { //recebe o estado por referência
if (check) {
check = false;
return 10;
} else {
check = true;
return 20;
}
}
int main() {
bool numero = false;
std::cout << getNumber(numero) << endl;
std::cout << getNumber(numero) << endl;
std::cout << getNumber(numero) << endl;
}
See running on ideone .
I know you will find it more complicated. But what's the use of something simple and wrong? This way you can have as many states as you want. If you do not do this or the function happens to have very little utility or you will have to coordinate the use of it, which is much harder than doing this.
I used the parameter by reference so any change in the variable check
within the function also changes the variable that sent the value to the function, so the variable numero
is always in the proper state for that moment.
If you checked to see if a variable was created or not, its logic would not work unless you had the variable erased, which does not make sense. The very title of your question does not make sense with the actual current question.
Please note that I have not improved your code.
A more sophisticated example running on ideone . I do not recommend using the third form since it uses a static variable, that is, global state. But at least it's encapsulated in one function, it helps.
C ++ is a compiled and static typing language. All variables must exist at compile time. Contrary to what you are accustomed to with dynamic languages, you can not treat variables as data directly.
Remember of this question ? There is a hint to simulate the dynamic variables. In the background dynamic languages make more or less that map that I indicated for you. When you are programming you have the illusion that you are using variables but in the background you are accessing positions in these maps also called dictionaries or hashes .
You see why these variables are dynamic and may or may not exist? Because they are given in key-value pairs, where the key is the name of the variable and the value is what is stored in that "variable".
Even some dynamic languages can have real variables. In general, local variables are considered real. These variables must always exist. These variables are preferable because they are more secured by the compiler and are faster.
If you really have a problem that the variable may or may not exist, use a map and simulate the variable. In C ++ it does not have syntax that facilitates access as if it were a truthful variable.
But you can probably do the same thing differently. When programming in static languages you have to think in a different way.
Guilherme Nascimento's answer is correct, but these variables he is talking about only exist in the compilation process, they have no use in the application itself. They are used to help compile the application and not to run it. And its use is discouraged whenever possible (it is not always). Think of two different languages that are in the same code but do not communicate. Preprocessor variables are not even part of C ++.
If you still think you need this, ask a question saying where you want to go, maybe we have a better solution.