This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Why is the phrase: "undefined behavior means the compiler can do anything it wants" true?
(2 answers)
warning as Control reaches at the end of a non-void function when implementing binary search
(2 answers)
Closed 3 months ago.
enter image description here
i don't know that why this cpp program is returning the value without defining the return value
i was expecting that this program will return will print the error value
#include <iostream>
using namespace std;
int sum(int a)
{
int c = a + 1; // this fucntion is returning value of c without defining
}
int main()
{
cout << sum(25);
return 0;
}
Welcome to the world of undefined behavior if you declare a function to return a value, but do not explicitly do so. The function may appear to work, or it may do something else.
This also applies if a function has a return, but control flow means it isn't reached. For instance, in the following simple function.
int foo(bool b) {
if (b) {
return 42;
}
}
Turn on and pay attention to compiler warnings.
Related
This question already has answers here:
g++ "calling" a function without parenthesis (not f() but f; ). Why does it always return 1?
(2 answers)
Closed 2 years ago.
#include <iostream>
int returnFive()
{
return 5;
}
int main()
{
std::cout << returnFive << '\n';
return 0;
}
Since this compiles without error, how does the system determine what value is actually sent and printed to console?
Imagine if the code written is something like
if(returnFive)
returnFive();
Here the expectation is that the compiler checks if the function pointer for returnFive is nullptr or not.
The compiler here is evaluating the function pointer as a boolean expression of whether it is NULL or not and printing the output.
https://godbolt.org/z/Psdc69. You can check that the cout is being passed a (bool).
This question already has answers here:
Omitting return statement in C++
(3 answers)
Why does this C++ snippet compile (non-void function does not return a value) [duplicate]
(7 answers)
Closed 2 years ago.
In the given C++ code when the statement n isn't present the function abc returns 10 .I.e def(10) acts as if it is a return statement but it isn't.
The output of the function is always 10 and doesn't lead to undefined behaviour when statement n as given in code isn't present in the code.
I am using sublime text 3.
So why it is behaving in such a manner or am I missing any concept
#include<bits/stdc++.h>
using namespace std;
int def(int x){
return x;
}
int abc(int x){
def(10);
return x;//statement: n
}
int main(){
cout<<abc(5);
}
This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
What happens when a function that returns an object ends without a return statement
(3 answers)
Closed 4 years ago.
In a test, one of my classmates wrote the following function, to flip a number:
int tukor(int n)
{
int k=0;
while(n!=0)
{
k=k*10+n%10;
n=n/10;
}
n=k;
}
You will notice a complete lack of any return statements, but when cout<<tukor(1234); is run (namespaaace std is used), it outputs 4321. Now the entire class is confused as to how this is possible, even after the teacher added the lines n=0;n=12; at the end of the function. It has worked for all test cases so far.
Is this caused by undefined behavior or something similar?
EDIT: changing k before the n=k statement changes the return value, and if it is removed, the return value is 0.
This question already has answers here:
C++ return value without return statement
(6 answers)
Closed 6 years ago.
I am confused with the following output in C++
int add()
{
int c = 2+3;
}
int main()
{
int x = add();
cout << x;
return 0;
}
This prints 5.even if we do not write return statement.
How this is managed in C++.
Please help.
This is UB. You're right to be confused - this can work one day and fail the next. Don't rely on undefined behavior.
If you want to know why it works, it's because parameters & return values are passed on a data structure called stack (well - usually; sometimes passed in the same register). Similarly, most implementations use this same stack for locals. Therefore, the int in add will be located in the same place as where the return value is expected (by your specific implementation) and your implementation doesn't invalidate memory when your int there is destructed. But it's still destructed, it's still UB and it might break in any second.
As the comments wrote, you might turn on warnings to avoid this kind of error.
This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 6 years ago.
This function returns a value even when it shouldn't.
#include<iostream>
using namespace std;
int foo(int a,int b)
{
if(a>b)
return a;
else if(a<b)
return b;
}
int main()
{
int x=7,y=7;
cout<<foo(x,y);
return 0;
}
The output is:
7
Also it produces proper output only on a GCC compiler (I used Dev C++). Turbo C produced garbage value.
Can someone explain how this happens?
The behaviour on not returning a value on all program control paths is undefined.
The compiler is allowed to do anything.
Didn't your compiler warn you of this? (GCC ought too, Turbo C possibly not on account of its age).