+ 2

Why is it wrong??

int multiply(int a, int b) { std::cout << a * b; return 0; }

4th Jan 2020, 2:56 AM
Hyst1k
Hyst1k - avatar
11 Answers
+ 10
Printing the result in that function might be considered a bad practice. Some recommend this: int mult (int a,int b){ return a*b; } int main(){ //Code cout << mult(4,5); //More code return 0; } But yours is not wrong.
4th Jan 2020, 3:33 AM
Kevin ★
+ 7
Why do you say it's wrong? What error did you got? It works for me. May I see your whole code.
4th Jan 2020, 3:13 AM
Kevin ★
+ 6
UR Welcome
4th Jan 2020, 3:45 AM
Kevin ★
+ 2
Yoo, i tried your code, and it is correct LOL
4th Jan 2020, 3:42 AM
Hyst1k
Hyst1k - avatar
+ 2
Thx
4th Jan 2020, 3:42 AM
Hyst1k
Hyst1k - avatar
+ 2
Personally, I would not consider this code correct. My question is what is this function intending to do? If the function is intended to calculate the multiplication and return it then it should be a value returning function and the multiplication should be returned - not output. int MultiplyTwoInts(int a, int b) { return a * b; } If the intent of the function is to output the results of the multiplication then it should be a void function as return 0 is not returning any relevant value. void MultiplyTwoInts(int a, int b) { cout << a * b; } Given the two options the value returning function is the better of the two choices, as it is more reusable. Let main decide what it wants to do with the result.
4th Jan 2020, 7:12 PM
Michele
Michele - avatar
+ 1
Well, in codewars that code its wrong apparently
4th Jan 2020, 3:22 AM
Hyst1k
Hyst1k - avatar
0
This is a function, not a program. Therefore, you cannot run it even hand trace to know its result(right or wrong). You need to write main funtion and call multiply function. In this case, make sure you have a prototype function with default arguments.
5th Jan 2020, 5:48 PM
feof
feof - avatar
- 1
🤔🤔🤔🤔🙄😐لفهم يفهمني
5th Jan 2020, 3:18 PM
Warda Yasmin
Warda Yasmin - avatar
- 2
facj=
5th Jan 2020, 10:13 AM
ABDURAXMON RAXIMOV
ABDURAXMON RAXIMOV - avatar
- 2
،
5th Jan 2020, 3:15 PM
Warda Yasmin
Warda Yasmin - avatar