classification
Title: function definition tutorial encourages bad practice
Type: enhancement Stage: resolved
Components: Documentation Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: aisaac, berker.peksag, docs@python, terry.reedy
Priority: normal Keywords:

Created on 2014-02-03 05:09 by aisaac, last changed 2016-06-01 03:17 by berker.peksag. This issue is now closed.

Messages (3)
msg210077 - (view) Author: Alan Isaac (aisaac) Date: 2014-02-03 05:09
Section 4.6 of the tutorial introduces function definition:
http://docs.python.org/3/tutorial/controlflow.html#defining-functions

The first example defines a function that *prints* a Fibonacci series.

A basic mistake made by students new to programming is to use a function to print values rather than to return them.  In this sense, the example encourages bad practice and misses an opportunity to instruct.  Since they have already met lists in Section 3, I suggest that returning a list of the values and then printing the list would enhance the tutorial.
msg210657 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-02-08 15:00
There is a later example introduced by "It is simple to write a function that returns a list of the numbers of the Fibonacci series, instead of printing it:" that does exactly what you suggest.
msg266796 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-06-01 03:17
I agree with Terry. The print example is also useful to show the implicit return value of fib(). Quoting from the same section:

    [...] In fact, even functions without a return statement do return a value, albeit a rather boring one. This value is called None [...]
History
Date User Action Args
2016-06-01 03:17:27berker.peksagsetstatus: open -> closed

nosy: + berker.peksag
messages: + msg266796

resolution: not a bug
stage: resolved
2014-02-08 15:00:17terry.reedysetnosy: + terry.reedy
messages: + msg210657
2014-02-03 05:09:47aisaaccreate