Initializing variables
July 30, 2021
While in Java, for example, you get an error if you want to run a function which contains a variable that hasn't been initialized java: variable x might not have been initialized
, not so in C++ and that can be tricky.
In C++ variables which haven't been initialized get random values which can or will almost always lead to unexpected results.
C++ example:
int main() {
int x = 15;
int y;
int z = x + y;
cout << z ;
}
// leads to a result of...
82
Process finished with exit code 0
So - ALWAYS INITIALIZE variables when programming in C++ !