Commit 67b571c1 authored by J. Ignacio Lucas Lledó's avatar J. Ignacio Lucas Lledó Committed by Maxim Belkin

Stress that `return` statements are not mandatory (#660)

Closes swcarpentry/python-novice-inflammation#489.
parent d07f0445
......@@ -59,7 +59,7 @@ def fahr_to_celsius(temp):
The function definition opens with the keyword `def` followed by the
name of the function (`fahr_to_celsius`) and a parenthesized list of parameter names (`temp`). The
[body]({{ page.root }}/reference/#function-body) of the function --- the
[body]({{ page.root }}/reference/#body) of the function --- the
statements that are executed when it runs --- is indented below the
definition line. The body concludes with a `return` keyword followed by the return value.
......@@ -190,6 +190,11 @@ def detect_problems(filename):
{: .language-python}
Wait! Didn't we forget to specify what both of these functions should return? Well, we didn't.
In Python, functions are not required to include a `return` statement and can be used for
the sole purpose of grouping together pieces of code that conceptually do one thing. In such cases,
function names usually describe what they do, _e.g._ `analyze`, `detect_problems`.
Notice that rather than jumbling this code together in one giant `for` loop,
we can now read and reuse both ideas separately.
We can reproduce the previous analysis with a much simpler `for` loop:
......@@ -20,7 +20,7 @@ assertion
Programmers typically put assertions in their code to check for errors;
if the assertion fails (i.e., if the expression evaluates as false),
the program halts and produces an error message.
See also: [invariant](#invariant), [precondition](#precondition),
See also: [invariant](#invariant), [precondition](#precondition),
......@@ -102,8 +102,13 @@ for loop
See also: [while loop](#while-loop).
: A group of instructions (i.e., lines of code) that transform
some input [arguments](#argument) to some output.
: A named group of instructions that is executed when the function's name is used in
the code. Occurrence of a function name in the code is a [function call](#function-call).
Functions may process input [arguments](#argument) and return the result back. Functions
may also be used for logically grouping together pieces of code. In such cases, they don't
need to return any meaningful value and can be written without the
[`return` statement](#return-statement) completely.
Such functions return a special value `None`, which is a way of saying "nothing" in Python.
function call
: A use of a function in another piece of software.
......@@ -186,7 +191,7 @@ regression
: To re-introduce a bug that was once fixed.
return statement
: A statement that causes a function to stop executing and return a value
: A statement that causes a function to stop executing and return a value
to its caller immediately.
......@@ -254,7 +259,7 @@ tuple
: The classification of something in a program (for example, the contents of a variable)
as a kind of number (e.g. [floating-point](#float), [integer](#integer)),
as a kind of number (e.g. [floating-point](#float), [integer](#integer)),
[string](#string), or something else.
type of error
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment