“Remember always your end, and that lost time does not return” Thomas A Kempis
We mentioned in the function section that the keyword return is used when we want to return a value from a function that actually expected to return a value.
If that function doesn’t expected to return a value (the return type of the function is declared void) then there’s no need to use the return keyword.
Also you should know that the return keyword can appear within the for-loop, if-else statement, while-loop, do-while loop, switch-statement.
But it cannot appear as the value assigned to a variable like:
`int i = return 3;` //WRONG…
Also the moment CPU reaches the return keyword no-matter where this keyword is in the body of a function, it terminates that function and any instructions after this keyword will not run any more. (A few compilers even throw warning if you put code after the return keyword).
Note: before checking the example below, you might want to check if-else statement section first if you don’t know what this statement does.
Example:
#include <stdio.h>
int main() {
int inValue ;
scanf("%d",&inValue);
if (inValue> 100){
return 0;
printf("The value is larger than 100");
}else{
return 0;
printf("The value is less than 100");
}
return 0;
}
If you compile and run the example above, no-matter what integer value you enter to this program, you won’t see any message on the screen.
This is because within the body of if as well as else statements, the first instruction is to call return keyword and that means the end of the function (no-matter how many instruction appears below this keyword, those instructions will be ignored).
That’s why the call to printf functions in these two statements simply ignored.