Skip to content

Commit

Permalink
[Basics Concept/Lasagna Exercise]: Changed return keyword Examples (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
BethanyG authored May 11, 2024
1 parent 2998cda commit 4688bf8
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 20 deletions.
45 changes: 38 additions & 7 deletions concepts/basics/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Related functions and classes (_with their methods_) can be grouped together in

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


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

Functions _explicitly_ return a value or object via the [`return`][return] keyword.
Functions that do not have an _explicit_ `return` expression will _implicitly_ return [`None`][none].

Functions _explicitly_ return a value or object via the [`return`][return] keyword:


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


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

# This function will return None.
# Assigning the function call to a variable and printing
# the variable will also return the value.
>>> sum_with_return = add_two_numbers(5, 6)
>>> print(sum_with_return)
7
```

Functions that do not have an _explicit_ `return` expression will _implicitly_ return the [`None`][none] object.
The details of `None` will be covered in a later exercise.
For the purposes of this exercise and explanation, `None` is a placeholder that represents nothing, or null:


```python
# This function does not have an explicit return.
def add_two_numbers(number_one, number_two):
result = number_one + number_two


# Calling the function in the Python terminal appears
# to not return anything at all.
>>> add_two_numbers(5, 7)
>>>


# Using print() with the function call shows that
# the function is actually returning the **None** object.
>>> print(add_two_numbers(5, 7))
None


# Assigning the function call to a variable and printing
# the variable will also show None.
>>> sum_without_return = add_two_numbers(5, 6)
>>> print(sum_without_return)
None
```


Expand Down
45 changes: 38 additions & 7 deletions concepts/basics/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Statements for the _body_ of the function begin on the line following `def` and


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

Functions explicitly return a value or object via the [`return`][return] keyword.
Functions that do not have an explicit `return` expression will _implicitly_ return [`None`][none].

Functions _explicitly_ return a value or object via the [`return`][return] keyword:


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


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

# This function will return None.
# Assigning the function call to a variable and printing
# the variable will also return the value.
>>> sum_with_return = add_two_numbers(5, 6)
>>> print(sum_with_return)
7
```

Functions that do not have an _explicit_ `return` expression will _implicitly_ return the [`None`][none] object.
The details of `None` will be covered in a later exercise.
For the purposes of this exercise and explanation, `None` is a placeholder that represents nothing, or null:


```python
# This function does not have an explicit return.
def add_two_numbers(number_one, number_two):
result = number_one + number_two


# Calling the function in the Python terminal appears
# to not return anything at all.
>>> add_two_numbers(5, 7)
>>>


# Using print() with the function call shows that
# the function is actually returning the **None** object.
>>> print(add_two_numbers(5, 7))
None


# Assigning the function call to a variable and printing
# the variable will also show None.
>>> sum_without_return = add_two_numbers(5, 6)
>>> print(sum_without_return)
None
```


Expand Down
43 changes: 37 additions & 6 deletions exercises/concept/guidos-gorgeous-lasagna/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,55 @@ def add_two_numbers(number_one, number_two):
IndentationError: unindent does not match any outer indentation level
```

Functions explicitly return a value or object via the [`return`][return] keyword.
Functions that do not have an _explicit_ `return` expression will _implicitly_ return [`None`][none].

Functions _explicitly_ return a value or object via the [`return`][return] keyword:

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


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

# This function will return None.
# Assigning the function call to a variable and printing
# the variable will also return the value.
>>> sum_with_return = add_two_numbers(5, 6)
>>> print(sum_with_return)
7
```


Functions that do not have an _explicit_ `return` expression will _implicitly_ return the [`None`][none] object.
The details of `None` will be covered in a later exercise.
For the purposes of this exercise and explanation, `None` is a placeholder that represents nothing, or null:


```python
# This function does not have an explicit return.
def add_two_numbers(number_one, number_two):
result = number_one + number_two


# Calling the function in the Python terminal appears
# to not return anything at all.
>>> add_two_numbers(5, 7)
>>>


# Using print() with the function call shows that
# the function is actually returning the **None** object.
>>> print(add_two_numbers(5, 7))
None


# Assigning the function call to a variable and printing
# the variable will also show None.
>>> sum_without_return = add_two_numbers(5, 6)
>>> print(sum_without_return)
None
```


Expand Down

0 comments on commit 4688bf8

Please sign in to comment.