Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Python
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Operations
Operations
Incidents
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
swc-bb
S
swc-lessons
2020-04-20-Potsdam-Berlin
Python
Commits
67b571c1
Commit
67b571c1
authored
Jul 31, 2019
by
J. Ignacio Lucas Lledó
Committed by
Maxim Belkin
Jul 31, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Stress that `return` statements are not mandatory (#660)
Closes swcarpentry/python-novice-inflammation#489.
parent
d07f0445
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
6 deletions
+16
-6
_episodes/06-func.md
_episodes/06-func.md
+6
-1
reference.md
reference.md
+10
-5
No files found.
_episodes/06-func.md
View file @
67b571c1
...
...
@@ -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:
...
...
reference.md
View file @
67b571c1
...
...
@@ -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
)
,
[
postcondition
](
#postcondition
)
.
assign
...
...
@@ -102,8 +102,13 @@ for loop
See also:
[
while loop
](
#while-loop
)
.
function
: 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.
RGB
...
...
@@ -254,7 +259,7 @@ tuple
type
: 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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment