+ 2
Why is it wrong??
int multiply(int a, int b) { std::cout << a * b; return 0; }
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.
+ 7
Why do you say it's wrong? What error did you got? It works for me. May I see your whole code.
+ 6
UR Welcome
+ 2
Yoo, i tried your code, and it is correct LOL
+ 2
Thx
+ 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.
+ 1
Well, in codewars that code its wrong apparently
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.
- 1
🤔🤔🤔🤔🙄😐لفهم يفهمني
- 2
facj=
- 2
،