+ 2

Return or print in function ?

Hi, In those two functions in Python , what's the best way to return something ? function 1: def my_discount(): price = float(input("put your price please : ")) discount=int(input("put your discount please:: ")) new_price = price-((discount/100)*price) return new_price function 2: def my_discount(): price = float(input("put your price please : ")) discount=int(input("put your discount please:: ")) new_price = price-((discount/100)*price) print ( new_price) In the first function I need print function to display the value of new price , the seconde function , I don't need to print function , I just to call the function to display the value.

21st Jul 2023, 3:30 PM
Toufik Hammad
Toufik Hammad - avatar
7 Answers
+ 8
It is better to use return, and print the result separately. In this code it makes no difference, and the result is the same. But it is very useful to learn about good coding practices and architectural principles, so that when you start writing slightly bigger and more complex programs, it will not look like a bloody mess. One of these ideas is SRP or single responsibility principle. Your functions (or objects) are only supposed to do one thing. Taking user input, calculating a discount, and printing it, are three different tasks. They should not be in the same function. Also if you learn about functional programming, one of the key ideas is "pure functions" which only depend on their parameters, based on the same input they always consistently produce the same output, and do not change anything outside their scope. Such code is very easy to test, and also easy to run in paralel.
21st Jul 2023, 3:47 PM
Tibor Santa
Tibor Santa - avatar
+ 6
def calculate_new_price(price, discount): new_price = price-((discount/100)*price) return new_price p = float(input("put your price please: ")) d = int(input("put your discount please: ")) result = calculate_new_price(p, d) print(result) This way, even the name of your function is telling the truth, it is calculating the new price from the old one and the discount. You can test it easily by calling it with some data and you can verify that it is correct. The input and output are "impure" operations and they are best handled outside the function, or you can also put them in a different function.
21st Jul 2023, 3:58 PM
Tibor Santa
Tibor Santa - avatar
+ 4
Let me asnwer your question... What is the best way to return something ? There are two conditions for something to be returned by a function in python 1. Presence of a return keyword 2. Presence of a valid statement after the return keyword This implies that if one of these condition is not met, then you are not returning from that function. So educationally, your second function does not met both of these conditions, it's just a function, it doesn't return but the first does. as for which is better, actually None. they both(return, void) have their use case. NONE is better than the other
21st Jul 2023, 6:06 PM
RuntimeTerror
RuntimeTerror - avatar
+ 3
print function is used to print value or returned value from the function. return value when you want to use returned value in further operations for example if you want to compare greatest between addition of 2 numbers and multiplication of 2 numbers. def add(a, b): return a + b def mult(a + b): return a * b res1 = add(10, 20) res2 = mult(10, 5) if res1 > res2: print(res1) else print(res2)
21st Jul 2023, 3:39 PM
A͢J
A͢J - avatar
+ 2
A͢J thank you , then it's better to use return here
21st Jul 2023, 3:42 PM
Toufik Hammad
Toufik Hammad - avatar
+ 2
Tibor Santa Thank you for your explain , I understand
21st Jul 2023, 4:12 PM
Toufik Hammad
Toufik Hammad - avatar
0
Runtime Terror Thank you for your explain
21st Jul 2023, 6:37 PM
Toufik Hammad
Toufik Hammad - avatar