I have the following code:
#include <iostream>
using namespace std;
Sum (int a, int b)
{
int x = a - b;
//cout << x << " \n";
return x;
}
int main()
{
int s1 = Sum(3, 6);
cout << s1;
return 0;
}
System info: Win 7 Sp1 x64 Ultimate/Professional or Win 8.1 x64 Code Blocks 16.01 MinGW Debugger name and version: GNU gdb (GDB) 7.6.1 compiler: GNU GCC Compiler
This code compiles with no problems, but this IS the problem, there should be errors.
1) Function Sum, has no return value, on http://cpp.sh/ it doesn't let me compile because of this.
2) Variable's s1 value is -3 whether I write "return x" or not.
It somehow passes the value of x
everytime BUT if I uncomment the cout
statement above the "return x
" everything starts to work out as expected, what the hell :) --> s1
will have a random value when no return statement is in place (because it was not initialized prior to being used for the function call) and -3 when the return
is there.
I've tried this on 3 separate computers and they all exhibit the same behaviour. So I don't think the machine is the problem. I also tried using a different compiler but I don't know if I configured them correctly and they don't have a debugger right ? I tried Borland c++ and Digital Mars. Borland has a new version 10.1 instead of the 5.5 that codeblocks supports and I couldn't make the new one work. I don't even know if this is a compiler or program issue ?
I'm trying to learn C++ and this is very annoying. Our teacher is using the same software in class but on Linux and it works perfectly.
Off topic: Is there a way to insert code with line numbers here ? First post here so i'm still new at this :).
Thank you !