[Tutor] REport Card Question
alan.gauld at btinternet.com
Tue Nov 30 09:56:44 CET 2010
"Robert Sjöblom" <robert.sjoblom at gmail.com> wrote
> On a related note, do all functions implicitly contain "return None"
> in them?
All functions return a value because thats the definition of what
a function does. (Some languages have a construct known as
a procedure which is a function with no return value, but Python
does not support procedures.)
> Trying out this function (correctly) would get "None" added,
> such as:
> Enter grade:76
> B, Try Harder
That depends on how you use the function. Functions should
ideally have a single clearly defined purpose. In this case the
purpose is to determine the grade based on a score.
The caller of the function is then responsible for deciding
what to do with the resulting grade - including ignoring (or
"correcting") a None result.
> Is there a way to avoid "return None" without explicitly
> having the function return something?
Nope, because that's how functions work.
> Even an empty string will return a new line.
What the function returns doesn't have to be what the program
displays. Separating the application logic from presentation is
an important design principle in any program. In this case the
code should "return" the value to the caller rather than directly
printing it. Then the code that calls the function can decide
what to do with the result - to print or not to print... As a
benefit this also makes the function more reusable, since we
may not always want to print the result but rather store it
in a database or display it in a GUI.
Author of the Learn to Program web site
More information about the Tutor