You are on page 1of 2

If no two ints satisfy the requirement, this function has no return value.

So the
compiler complains.

For certain inputs, it may be possible for the if statement in the second for to
never evaluate to true, meaning that the function reaches the last line without
returning.
If your implementation is correct, this will never happen for the inputs described
by this problem,
however it is still necessary to have a 'default' return value at the end just in
case different inputs are given. So you should add a return -1;
at the end (since the input can't be negative, this will indicate failure).

Well, I think this type of error is only happening in leetcode, I found this
information from another site:
"If you have a C++ program missing a return statement from a function that is
supposed to return a value,
g++ will compile it happily with no errors (or even a warning, unless -Wreturn-type
or -Wall is used)."
I guess leetcode uses certain compiler which take that warning as an error, and if
you compile your code in visual studio with -Wall you might see the warning but not
error.

although sometimes we have a return in the same program, but not at the end of the
code, leetCode will not compile.

add a return statement at the end of the function, as long as the return data type
pair! Remember, always return, even if you never need it.
So we also have to return at the end of the function block. When this error occurs,
LeetCode usually has a red highlight on the last line of the function block.

Although sometimes we already have a return in a block of code, but not at the end
of this block of code, leetCode will not compile.

So we also have to return at the end of the code block of the function. When this
kind of error occurs,
generally LeetCode will also have a red highlight in the last line of the function
code block.
// Just added a return statement that returns a suitable return value at the end of
the function twoSum

no man, last return v is only a dummy sentence that sentence never been executed
for valid input(as says in the description)

Alternatively, I treat all warnings however trivial like errors

To avoid compiler warning

its returning empty vector otherwise it would show warning

"not all paths return a value" warning as an error

////////////////////
To avoid compiler warning (leetcode's compiler always treat warning as error). I
have added a dummy return statement at the end of the block of code.
return {} is just a dummy statement that never been exceuted for given inputs.
Leetcode compiler will not compile the code
Basically, the compiler needs to make sure that this function always returns a
vector.
I guess leetcode uses certain compiler which take that warning as an error,

To avoid compiler warning (leetcode's compiler always treat warning as error). I


have added a dummy return statement at the end of the block of code.
return {} is just a dummy statement that will never execute for given inputs.

You might also like