0

I am learning Python these days and I want to ask which code is better to write and applicable in real world or used in practice actually? Both codes are giving the same output though. In first function, I used return inside and then had to use print when it comes to invoking. While on the other hand, I used print inside and then just call the function. So, which is a good practice?

And apart from that, many times I would need to get many things inside my function and I will have an option to either get it back through "print" and "return" - so also in those conditions, what should I do ideally and what is being used in practice?

def min_func(values):
    return min(values)
print(min_func([3, 41, 12, 9, 74, 15]))
def min_func(values):
    print(min(values))
min_func([3, 41, 12, 9, 74, 15])
3
  • 2
    print is not giving it back - it just prints whatever, return returns value that you can use. so if you would want to use it like x = min_func.. - you could not assign values to x if used print Commented Jun 2, 2020 at 9:39
  • If you need to use the result of the function further in your code, it is probably better to return it. Otherwise print is enough Commented Jun 2, 2020 at 9:42
  • Its always best to return values from functions, especially those functions which do some work for you. Printing should only be used on the outside of functions to print the results. Commented Jun 2, 2020 at 9:42

2 Answers 2

2

It depends on what you want your function to do.

For example, if we create a function to print the minimum number from a list, we can do this:

def print_min(values):
    print(min(values))
print_min([3, 41, 12, 9, 74, 15])

Note that the function does not return any value. When you call this function, it will just print the value and return nothing. So, that value cannot be used later in the program.

On the other hand, if you want your function to return the minimum number so that you can use it to do more processing later (and print some other output later), you can do:

def get_min(values):
    return min(values)
m = get_min([3, 41, 12, 9, 74, 15])

Now you may print it, or write it to a file, or do some mathematical operations on it. The return value is available for your program to use later.

Sign up to request clarification or add additional context in comments.

1 Comment

Agreed. It all depends on what the actual function of the code depends on. As a general rule, functions should return something, but python operates under the "easier to ask forgiveness than permission" mindset, so you can do whatever you want. The first option is neater and allows for more processing later. +1 from me for this answer.
0

I think it is better to use the first approach you mentioned because it is more flexible to work with the value the function is returning and with the returned value you can do whatever you want

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.