Should I use return or print()?

I was a bit confused by this question. It says the function should print 3 lines and return 1 value - but in the “should print” part of the comments it shows all the values in one row (not just 1 value).

I did eventually get this right by just returning one value, but then it didn’t look like the suggested solution in the comments - so perhaps the phrasing was a bit odd. Or is it just me?

2 Likes

I have a question about Code challenge (Lots of math) task.

The task is the following (among others): Finally, it should return the third number printed mod a.

If it should just return, not print the 4th value, why in the exercise solution there is 4th and 8th numbers present?
IMO, there should be just 3 values printed for the first function call, and 3 for the second.

1 Like

Recall that the function has three print statements, and on return we print again. That last print will be mod a value, 0.

general question:

Can someone explain the difference between using the “print()” within your functions rather than just returning all the values?

For example using the code:

def lots_of_math(a, b, c, d):
first = a+b
second = c-d
third = first*second
fourth = third%a
return first, second, third, fourth

will give you the same answers but the will be formatted differently. Is there any advantage of using print vs. returning the values and when would you want to use one over the other?

@jacobgarwin295317990, use print() when you want to see the value on the screen.

Use return when Python needs to make use of the value, say, to assign to a variable, to send to another function as an argument, or to use as a value in an expression.

1 Like

why in this task ‘return’ does not show anything but the last task it shown?
not show return

1 Like

Should line 6 be,

fourth = third % first

?

yes, you’re right. But the question is being asked about the return statement. In a previous exercise, ‘return’ did not show any output in the terminal.
But in this exercise, it does. why is that?

return is part of function design. A function that only does something and has no meaningful data to add to the program does not need to return anything. If there is no return statement, the function returns None by default.

Say we want a function to take some information and compute some value the program will need, for instance area of a circle.

from math import pi

def area(radius):
    return pi * radius ** 2

Functions are designed with purpose in mind. They are small, simple pieces of a larger puzzle. In a lesson it’s not likely we’re working on a larger problem so it harder to see things in a big picture scenario. The projects will get bigger as you go, and more sense will come of the mechanics, the data, the objective and the overall plan.

1 Like

Hi everyone, I am new to the forum, happy to be with you!

The way I understood tag3272764692’s question he is irritated by the return statement being printed in the terminal in this exercise, while it wasn’t in the last ones unless you explicitly printed something with print() it didn’t show up on the right side. Why do 4 lines appear in this case, although there are only 3 print statements?

I asked myself the same thing, so would be really interested in the answer :wink:

There are four print statements, three inside the function, and the one that prints the return value at the caller.

print (lots_of_math(1, 2, 3, 4))

That will print the fourth value on its return.

thank you! my doubt cleared!

1 Like