Akshay, please
UseCodeTags (← click this). I added them for you this time. Doesn't it look better?
You code still needs some indenting, though.
As for your question. All methods that are declared to return something (that means all except declared
void) must do one of the two things:
return a value of declared type,throw an exception.
Your method has nothing to do with exceptions so I'll focus on returning a value.
In your first example, method
m returns 6 when
k is equal to 2.
What will be returned if it is not? You don't have any instruction for the compiler what to return.
And remember, a non-void method must always return a value.
So, the compiler is complaining about missing return statement.