go to previous page   go to home page   go to next page

Answer:

NO!

The fundamental idea, that a module should have one entry point and one exit point has been violated! This function has several exit points.


Early Returns

Often a function is more clear if it returns the result of computation as soon as it is available. This might be done at several locations in the code. This notion is called early returns and is usually permitted.

Ideally, early returns are not deeply nested in the code and occur near the top of the function. Often, early returns occur when there are some easily tested conditions that detect special return values.

But... but... what happened to the idea of structuring?

You can think of the flowchart on the right as an abbreviation for the flowchart on the left. The idea of structuring is still there, and should be foremost in your mind when you design code. Modularity should still work: if the pre-conditions are true then the exit conditions will be met when control leaves the block (or function.)

Max of Three Flowchart Max of Three Flowchart

QUESTION 4:

Now say that you need a function that returns the maximum of its nine integer arguments. The prototype is:

int maxNine( int a, int b, int c, int d, int e, int f, int g, int h, int i );

How would you design this function?