2

I think this will be probably one of the most dumbests questions of all, but i'm not really understading very well the functions and one thing is about the use of "return"

So, let's imagine the simple example that I want to greet my function (by saying "Hi function!") and use a function for that.

So should I use the print statement in the function like this:

def hello_func (greeting):
    print (f'{greeting} function!')

greeting = 'Hi'

hello_func(greeting)

Or should I return the value in the function:

def hello_func (greeting):
    return (f'{greeting} function!')

greeting = 'Hi'

print (hello_func(greeting))

The output is the same (Hi function!)... so in this particular case it doesn't matter what I use or is there a "better" code?

Thank you.

1
  • 3
    Prefer the latter. Making it a function is more general, you could then do hellostr = hello_func(greeting) and use it later. Make functions "pure", and separate out the IO functions (like print). Commented Feb 20, 2022 at 2:32

4 Answers 4

3

Functionally, the big difference is that in your second example, you can save the returned value into a variable. Then you can manipulate it however you want:

def hello_func (greeting):
    return (f'{greeting} function!')

greeting = 'Hi'

result = hello_func(greeting)  # Function is only called once

print(result)  # Displays the result
print(result + "!!!")  # Does stuff with result
print(result + "?????")  # Does even more stuff with result
print(result == 'Hello')  # Compares the result to something
newest_result = result + "foo bar"  # Creates a new variable based on result
print(newest_result)  # Displays the new variable

This means that you only need to execute hello_func once, then you save its result to a variable, which is then further processed by your program. This becomes even more crucial if hello_func is an expensive function that takes a lot of time to execute.

The key question to ask when deciding whether to take your first or second approach is: do you only want to display the result on screen, or do you want to save the result to memory and use it later down on your program?

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

Comments

1

Returning would be a better practice.

Comments

1

returning or printing both on your choice for this greet function.

there are times that you need to use function return value in other places; at that moment it's necessary to use return statement that you can use that function return value at another places.

Comments

1

Overall, it depends on what you want to do.

For example if you're printing an error, just print it. Alternatively if you're processing a string, then return.

In any case, keep in mind the purpose.

4 Comments

If you're printing an error, often the better way would be to raise an exception instead
Sorry, I primarily program in C, so I didn't have exceptions in mind.
Well, in C you would arrange to return an error code?
Yes, if its an operation of some sort that can fail, such as solely returning an integer error code, a structure with a ssize_t or using errno. Though if you have choice between those and exceptions it's not a hard answer.

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.