Many of the starter programmers might 'forget' a very small point in their programs when writing the return statements. For instance,
We don't actually need to do this, instead we can write,
This is because, when you write if
The return statement makes the cursor to go back (return to home!) which means it no longer executes the statements after the return statement. One more important point is that if there is no condition to return a value, you cannot return another value. For instance,
These are some of the good programming practices, and i am going to introduce this label.
if(some_condition) return 0;
else return 1;
We don't actually need to do this, instead we can write,
if(some_condition) return 0; return 1;
This is because, when you write if
some_condition
is satisfied 0 is returned and the cursor is no more in the method to execute the next statement. So the else will never be reached if the condition is satisfied. However, in the same way if the condition is not satisfied 0 is never returned, and the next statement return 1; is executed and the cursor will no longer be in the method.The return statement makes the cursor to go back (return to home!) which means it no longer executes the statements after the return statement. One more important point is that if there is no condition to return a value, you cannot return another value. For instance,
if(true) return 0; return 1;
makes no sense. Because true is always true and 0 will be returned always. The statement return 1 is never reached which means that it is unnecessary.return 0; return 1;
will also make no sense. You'll get an error stating that the statement return 1; is never reached.if(some_condition) return 0; else return 1; return -1;
This is also sense less because, if some_condition
is not satisfied, the cursor moves to else and returns 1, even the else here is not needed as said before. And the next one, return -1; is never reached. Because else does mean that for any other condition.These are some of the good programming practices, and i am going to introduce this label.
No comments:
Post a Comment