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);
}
Related
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 (actually) happens, when a function with the warning "control reaches end of non-void function" is called?
(7 answers)
Closed 4 days ago.
the code is printing abnormal value `// C++ Program to demonstrate working of a function
#include <iostream>
using namespace std;
int max(int x, int y)
{
if (x > y)
cout<<x;
else
cout<<y;
}
// main function that doesn't receive any parameter and
// returns integer
int main(){
int a = 10, b = 20;
// Calling above function to find max of 'a' and 'b'
cout<<max(a, b);
return 0;
}
i am expecting the answer to be 20 can explain why this is happening
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.
This question already has answers here:
What should main() return in C and C++?
(19 answers)
Return type of main function [duplicate]
(5 answers)
Closed 11 months ago.
I am always returning a value of 0 from the main function. I was wondering if we could change the return type of the C++ main method to short int.
#include <iostream>
using namespace std;
short int main() {
cout << "Hello World" << endl;
return 0;
}
Something like this, but the above code returns an error saying that the return type should be 'int'. Can someone please guide me?
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:
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).