1

compiler used-: code blocks

without returning the "c" how is variable "d" getting its value?

#include<iostream>
using namespace std;

int add(int x,int y) {
    int c;
    c=x+y;
}

int main() {
    int a,b;
    cin>>a>>b;
    int d=add(a,b);
    cout<<d;
}
김선달
  • 1,485
  • 9
  • 23
Noobcoder
  • 31
  • 7
  • Not returning a value from a function that is supposed to return a value is undefined behavior. There is nothing to reason from undefined behavior -- anything could happen. – PaulMcKenzie Jun 13 '20 at 04:27
  • still not getting it. – Noobcoder Jun 13 '20 at 04:32
  • @Noobcoder Undefined behavior means anything can happen. Don't know how to put it more simpler than that. – PaulMcKenzie Jun 13 '20 at 04:34
  • Side note: Code::Blocks is an [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment) that normally sits atop the g++ compiler. – user4581301 Jun 13 '20 at 04:41
  • [Please see this](http://coliru.stacked-crooked.com/a/bf536984839ceeaa). What happened to the output? See what I mean? Assigning the values, no output. Change a compiler setting, maybe something else is output. Again, all because your program chose to invoke undefined behavior. – PaulMcKenzie Jun 13 '20 at 04:42
  • There are language rules where in the name of potential for optimization, difficulty in definitively trapping at compile time, or even the classic "Holy $#!+. Never expected anyone to do THAT!" exactly what will happen is NOT DEFINED. If you do it, the compiler can generate whatever code (often no code whatsoever because no code is easy and blisteringly fast) it wants to handle the problem. – user4581301 Jun 13 '20 at 04:47
  • If a function that is declared to return a value does not return a value, the compiler can do anything from use whatever happened to be at the right spot in memory at the time (and may be the expected result) to obviously fail. – user4581301 Jun 13 '20 at 04:48
  • Thanx for your help guys now i understand that its an undefined behavior and its not working on mobile compilers but only on code blocks maybe compiler is handling it in his own way using a set of rules – Noobcoder Jun 13 '20 at 05:31
  • It's actually a bit worse than that. It's not just different rules and different handling by different compilers. There are no rules. Say you accidentally release some memory too soon and part of your program has a pointer to it and is still using it. That other part of the code might not even notice it's not supposed to have that memory any more until it gets taken away by another process. Most of the time the program runs to completion without you seeing the problem, but sometimes the computer is busy and reclaims the memory. Crash. – user4581301 Jun 13 '20 at 06:08
  • Ok i got it thanks for your help – Noobcoder Jun 13 '20 at 06:43

2 Answers2

0

Doesn't compile in visual C++, but it does indeed give the results when using onlinedgb.

JaMiT's comment above links to the correct answer with explanation.

0

Write this code:

#include<iostream>
using namespace std;

int add(int x,int y)
{
    return x+y;//CHANGE THIS
}

int main()
{
     int a,b;
    cin>>a>>b;
    int d=add(a,b);
    cout<<d;
}
GobeRadJem32
  • 102
  • 3
  • 14
  • I know this would work but my main query was why it returns a value while i am not returning anything. Because i was showing my friend to how to use return in different case and what happens if I Don't return the way. But it showed an undefined behavior as mentioned above in comments – Noobcoder Jun 13 '20 at 05:29