return Exits The Function

Let's check your solution for the max problem on the previous page. I would expect you probably wrote something like this:

int max(int a, int b) {
	if (a > b)
		return a;
	else
		return b;
}

If so, this is correct! There is one more bit of information about the return statement that I want to tell you now: return exits the function.

What that means is: the function is executed statement by statement, and if any return statement is reached, the function stops executing, its result is set to whatever is written in the return, and the function exits.

This is a useful feature, and allows us to change the code this way:

int max(int a, int b) {
	if (a > b)
		return a;

	/* we only reach this if the condition is false */
	return b;
}

If a > b, we would return a; and exit, so we will only reach the next statement, return b;, if the condition is false. Remember this trick, as it will prove useful later.