+ 4
What's wrong with this code? Can anyone explain ?
output is having some additional numbers why?? #include <iostream> using namespace std; int timesTwo(int x) { cout <<x; } int main() { cout << timesTwo(8) << endl; cout <<timesTwo(5) << endl; cout <<timesTwo(42) << endl; } output 84620160 54620160 424620160
27 Respostas
+ 5
Firstly, the function is declared as
int timesTwo(int x)
which means it returns an integer. But, there is no return statement.
This is what's happening according to me.
cout<<timesTwo(8)<<endl;
timesTwo gets called. It prints x (cout<<x) and then returns a garbage value (maybe the 'cout' object turning itself into number).
This the case for each of the 3 lines inside main().
+ 6
You're output is not correct.
The correct result from your function should be
80
50
420
What you're code does is to print the number sent to it in the parameter (within the function body itself) and then print the return value from the function at the end (in main), which is 0 since it executed correctly.
If you do not want it to add a zero at the end, don't send the result of the function to cout or set the return type of the function to void. In the later case nothing will be printed in main.
(you might as well skip cout and just call the function. The result would be the same)
If you really expect the function to print the parameter twice (as the name implies) you have to set the return value to that of the parameter:
return = x
in your case.
Hope this helps
+ 6
int timesTwo(int x) {
return x * 2;
}
This is if you want to cout the function and output a number multiplied by two on to whatever is in the parameter.
+ 4
I don't know c++ but I guess that you have to return the value of x in the function timestwo
+ 4
According to its signature, the function 'timesTwo' MUST return an interger value and was originaly designed for that purpose ( else the keyword 'void' or another one would have be replaced the 'int' in front of function name...
It's a mistake to not do. So, you maybe cannot determine how C++ handle this, implicitly, since it doesn't raise a compilation error ^^
+ 2
change return x; statement in your function timesTwo()
+ 2
The return type of your function is int that's why the output is additional inclusion of "4620160".To get the desired output change cout statement to return x. It will work surely.
Hope this helps.
+ 2
as I look at your code... it seems as if your code was intended for displaying the value of X....in that case you should use void to indicate that your function returns no value.... Or if you want to return the value of X times 2 in that case your should have return x*2
+ 1
returning a value will show that number at last means,
if i am returning 0
then it shows 0 at last
+ 1
why 4620160 only is coming not another number
+ 1
There's surely an explanation of what return compilator in case of no return statement, maybe a default value, but I don't know...
+ 1
When the functions doesn't return a value..
It outputs.. Prints something..
We use void in front of the functionsāŗļø
+ 1
In regards of use of the function ( in a console out stream ), its signature is correct, and should return a value for the output, even a output is done in its body...
+ 1
instead of printing the value in function
return the value to main
+ 1
function int timesTwo(int x)
must be return an integer value
i.e.,
return x*=x;
+ 1
Hey, guys! Here comes:
)--[ THE MAGIC ASSIGNMENT ]--(
Expected behaviour is found with just append this line to the function bofy:
int y=x;
Don't still know what's happening, but looks like the number is a kind of hash to provide internally an unique identifier for variables: Attempting to 'cout' declared but not assigned variables, output the same kind of non random number... if declaration was made in a scope: with variables declared in global implicit scope, the number output by 'cout" is 0, whatever the variable name, as if an implicit assignement was made at global level ^^
Other strangeness, we need to declare a new variable ( not try with a variable from another scope ) for the magic assignemet be working: attempt to modify value of 'x' don't modify the output...
Well, finaly the code working ( with the magic line added ):
#include <iostream>
using namespace std;
int timesTwo(int x) {
cout << x;
int y=x;
}
int main() {
cout << timesTwo(8) << endl;
cout << timesTwo(5) << endl;
cout << timesTwo(42) << endl;
}
[ EDIT ]
output:
88
55
4242
( I assume the purpose of the code was to print 'two times' the value passed at parameter: one in function, the second by implicit return )
+ 1
wow..there are many think i can hardly imaging....that is a that how both of the fuction (main & timesTwo) are working without a return command hence both are int type so they should return something..isnt it?
+ 1
function timesTwo should return a value....
0
to relate the implementation of the function with it's name, replace "cout<<x" with "return x+=x" . and it will work.
0
Your function must return an integer value, try changing the name of the function to
'void timesTwo (int x)' or just add 'return x;' at the end of it