Skip to content

Commit 4688bf8

Browse files
authored
[Basics Concept/Lasagna Exercise]: Changed return keyword Examples (#3689)
* Changed examples for use of the return keyword in Python functions. See discussion at http://forum.exercism.org/t/missing-print-in-python-basics-functions-return/11025/4 for additonal information. * Further massaging of examples and adding variable assignment example.
1 parent 2998cda commit 4688bf8

File tree

3 files changed

+113
-20
lines changed

3 files changed

+113
-20
lines changed

concepts/basics/about.md

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Related functions and classes (_with their methods_) can be grouped together in
109109

110110
The `def` keyword begins a [function definition][function definition].
111111
Each function can have zero or more formal [parameters][parameters] in `()` parenthesis, followed by a `:` colon.
112-
Statements for the _body_ of the function begin on the line following `def` and must be _indented in a block_.
112+
Statements for the _body_ of the function begin on the line following `def` and must be _indented in a block_:
113113

114114

115115
```python
@@ -134,24 +134,55 @@ def add_two_numbers(number_one, number_two):
134134
IndentationError: unindent does not match any outer indentation level
135135
```
136136

137-
Functions _explicitly_ return a value or object via the [`return`][return] keyword.
138-
Functions that do not have an _explicit_ `return` expression will _implicitly_ return [`None`][none].
137+
138+
Functions _explicitly_ return a value or object via the [`return`][return] keyword:
139+
139140

140141
```python
141-
# Function definition on first line.
142+
# Function definition on first line, explicit return used on final line.
142143
def add_two_numbers(number_one, number_two):
143-
result = number_one + number_two
144-
return result # Returns the sum of the numbers.
144+
return number_one + number_two
145+
145146

147+
# Calling the function in the Python terminal returns the sum of the numbers.
146148
>>> add_two_numbers(3, 4)
147149
7
148150

149-
# This function will return None.
151+
# Assigning the function call to a variable and printing
152+
# the variable will also return the value.
153+
>>> sum_with_return = add_two_numbers(5, 6)
154+
>>> print(sum_with_return)
155+
7
156+
```
157+
158+
Functions that do not have an _explicit_ `return` expression will _implicitly_ return the [`None`][none] object.
159+
The details of `None` will be covered in a later exercise.
160+
For the purposes of this exercise and explanation, `None` is a placeholder that represents nothing, or null:
161+
162+
163+
```python
164+
# This function does not have an explicit return.
150165
def add_two_numbers(number_one, number_two):
151166
result = number_one + number_two
152167

168+
169+
# Calling the function in the Python terminal appears
170+
# to not return anything at all.
171+
>>> add_two_numbers(5, 7)
172+
>>>
173+
174+
175+
# Using print() with the function call shows that
176+
# the function is actually returning the **None** object.
153177
>>> print(add_two_numbers(5, 7))
154178
None
179+
180+
181+
# Assigning the function call to a variable and printing
182+
# the variable will also show None.
183+
>>> sum_without_return = add_two_numbers(5, 6)
184+
>>> print(sum_without_return)
185+
None
155186
```
156187

157188

concepts/basics/introduction.md

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Statements for the _body_ of the function begin on the line following `def` and
5050

5151

5252
```python
53-
# The body of a function is indented by 2 spaces, & prints the sum of the numbers.
53+
# The body of this function is indented by 2 spaces,& prints the sum of the numbers.
5454
def add_two_numbers(number_one, number_two):
5555
total = number_one + number_two
5656
print(total)
@@ -71,24 +71,55 @@ def add_two_numbers(number_one, number_two):
7171
IndentationError: unindent does not match any outer indentation level
7272
```
7373

74-
Functions explicitly return a value or object via the [`return`][return] keyword.
75-
Functions that do not have an explicit `return` expression will _implicitly_ return [`None`][none].
74+
75+
Functions _explicitly_ return a value or object via the [`return`][return] keyword:
76+
7677

7778
```python
78-
# Function definition on first line.
79+
# Function definition on first line, explicit return used on final line.
7980
def add_two_numbers(number_one, number_two):
80-
result = number_one + number_two
81-
return result # Returns the sum of the numbers.
81+
return number_one + number_two
82+
8283

84+
# Calling the function in the Python terminal returns the sum of the numbers.
8385
>>> add_two_numbers(3, 4)
8486
7
8587

86-
# This function will return None.
88+
# Assigning the function call to a variable and printing
89+
# the variable will also return the value.
90+
>>> sum_with_return = add_two_numbers(5, 6)
91+
>>> print(sum_with_return)
92+
7
93+
```
94+
95+
Functions that do not have an _explicit_ `return` expression will _implicitly_ return the [`None`][none] object.
96+
The details of `None` will be covered in a later exercise.
97+
For the purposes of this exercise and explanation, `None` is a placeholder that represents nothing, or null:
98+
99+
100+
```python
101+
# This function does not have an explicit return.
87102
def add_two_numbers(number_one, number_two):
88103
result = number_one + number_two
89104

105+
106+
# Calling the function in the Python terminal appears
107+
# to not return anything at all.
108+
>>> add_two_numbers(5, 7)
109+
>>>
110+
111+
112+
# Using print() with the function call shows that
113+
# the function is actually returning the **None** object.
90114
>>> print(add_two_numbers(5, 7))
91115
None
116+
117+
118+
# Assigning the function call to a variable and printing
119+
# the variable will also show None.
120+
>>> sum_without_return = add_two_numbers(5, 6)
121+
>>> print(sum_without_return)
122+
None
92123
```
93124

94125

exercises/concept/guidos-gorgeous-lasagna/.docs/introduction.md

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,55 @@ def add_two_numbers(number_one, number_two):
8787
IndentationError: unindent does not match any outer indentation level
8888
```
8989

90-
Functions explicitly return a value or object via the [`return`][return] keyword.
91-
Functions that do not have an _explicit_ `return` expression will _implicitly_ return [`None`][none].
90+
91+
Functions _explicitly_ return a value or object via the [`return`][return] keyword:
9292

9393
```python
94-
# Function definition on first line.
94+
# Function definition on first line, explicit return used on final line.
9595
def add_two_numbers(number_one, number_two):
96-
result = number_one + number_two
97-
return result # Returns the sum of the numbers.
96+
return number_one + number_two
9897

98+
99+
# Calling the function in the Python terminal returns the sum of the numbers.
99100
>>> add_two_numbers(3, 4)
100101
7
101102

102-
# This function will return None.
103+
# Assigning the function call to a variable and printing
104+
# the variable will also return the value.
105+
>>> sum_with_return = add_two_numbers(5, 6)
106+
>>> print(sum_with_return)
107+
7
108+
```
109+
110+
111+
Functions that do not have an _explicit_ `return` expression will _implicitly_ return the [`None`][none] object.
112+
The details of `None` will be covered in a later exercise.
113+
For the purposes of this exercise and explanation, `None` is a placeholder that represents nothing, or null:
114+
115+
116+
```python
117+
# This function does not have an explicit return.
103118
def add_two_numbers(number_one, number_two):
104119
result = number_one + number_two
105120

121+
122+
# Calling the function in the Python terminal appears
123+
# to not return anything at all.
124+
>>> add_two_numbers(5, 7)
125+
>>>
126+
127+
128+
# Using print() with the function call shows that
129+
# the function is actually returning the **None** object.
106130
>>> print(add_two_numbers(5, 7))
107131
None
132+
133+
134+
# Assigning the function call to a variable and printing
135+
# the variable will also show None.
136+
>>> sum_without_return = add_two_numbers(5, 6)
137+
>>> print(sum_without_return)
138+
None
108139
```
109140

110141

0 commit comments

Comments
 (0)