From 4688bf8f1ee46a53190df13b91ce2719cd6e82fe Mon Sep 17 00:00:00 2001 From: BethanyG Date: Fri, 10 May 2024 17:32:46 -0700 Subject: [PATCH] [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. --- concepts/basics/about.md | 45 ++++++++++++++++--- concepts/basics/introduction.md | 45 ++++++++++++++++--- .../.docs/introduction.md | 43 +++++++++++++++--- 3 files changed, 113 insertions(+), 20 deletions(-) diff --git a/concepts/basics/about.md b/concepts/basics/about.md index 6a6fca716b..dd636219ab 100644 --- a/concepts/basics/about.md +++ b/concepts/basics/about.md @@ -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 @@ -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 ``` diff --git a/concepts/basics/introduction.md b/concepts/basics/introduction.md index 34e2a8804d..dc062bc70d 100644 --- a/concepts/basics/introduction.md +++ b/concepts/basics/introduction.md @@ -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) @@ -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 ``` diff --git a/exercises/concept/guidos-gorgeous-lasagna/.docs/introduction.md b/exercises/concept/guidos-gorgeous-lasagna/.docs/introduction.md index 6c8de4e69f..6c6812f6d8 100644 --- a/exercises/concept/guidos-gorgeous-lasagna/.docs/introduction.md +++ b/exercises/concept/guidos-gorgeous-lasagna/.docs/introduction.md @@ -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 ```