Warning: Control reaches end of non-void function
Warning: Control reaches end of non-void function--Reason and Solution
The reason for this warning is that the compiler thinks your function may never return something at the end of the function, though you are 100% sure that it can return what you expect.
The solution is simple, just add a statement returning something at the end of you function body. And the warning or error will disappear.
UPDATE!
Last time I said that the solution the the problem is to add a expression "return 0;" to the end of the function. However, when I was having class, our teacher gave us a better solution! That is: REMOVE THE ELSE SENTENCE ! If the condition is satisfied, the following expression will be executed and an integer will be returned. If the condition is not satisfied, directly return a -1 and the function is ended. Here is the demonstration:
This way is succinct and more logical. But it seems it can't be applied in the first example.
评论
发表评论