55.0k views
0 votes
IN TODO 9 and TODO 10 you will implement two very simple functions. The two functions will be very similar to each other with one important difference. One of them will return a computed value whereas the other one will return None and it will instead print the computed value to a terminal.

In TODO 9 your task is to implement the add_numbers_return function with the first_num and second_num parameters. Implement the function to add the two numbers and return the result. Below are several examples of the expected behavior:
>>> add_numbers_return(2, 3)
5 >>> result = add_numbers_return(-1, 2) >>> result
1
In TODO 10 your task is to implement the add_numbers_print function with the first_num and second_num parameters. Implement the function to add the two numbers and print the result to the terminal. Do not return any value. Below are several examples of the expected behavior:
>>> add_numbers_print(2, 3)
5
>>> result = add_numbers_print(-1, 2)
1
>>> result
>>>
Think about the differences in the output. The add_numbers_return did not produce any output during the assignment, whereas the add_numbers_print printed 1 to the terminal. When the result variable was examined it returned 1 in case of an assignment from the_add_numbers_return. However, nothing was returned in case of the add_numbers_print which means the result has been assigned with the None value.
Congratulations, you have implemented the small library of mathematical functions.

1 Answer

4 votes

Below is the usage of the add_numbers_return and add_numbers_print functions:

Python

def add_numbers_return(first_num, second_num):

"""

Add two numbers and return the result.

Args:

first_num (int): The first number.

second_num (int): The second number.

Returns:

The sum of the two numbers.

"""

total = first_num + second_num

return total

def add_numbers_print(first_num, second_num):

"""

Add two numbers and print the result to the terminal.

Args:

first_num (int): The first number.

second_num (int): The second number.

"""

total = first_num + second_num

print("The sum of {} and {} is {}".format(first_num, second_num, total))

An examples of the way to use the functions is shown below:

Python

result = add_numbers_return(2, 3)

print(result) # Output: 5

add_numbers_print(-1, 2)

result = add_numbers_print(-1, 2)

print(result) # Output: None

So, based on the code above, the add_numbers_return function is one that returns the result of the addition, while the add_numbers_print function prints the result to the terminal and does not return anything.

User AbiusX
by
7.4k points

No related questions found