0

I've used the return function in my code before as I am learning python, but I'm not sure what it does. I've looked it up, but I'm not really sure what it actually does. Can anyone help?

Thanks

(For Ex.)

def break_words(stuff):
    words = stuff.split(' ')
    return words

def sort_words(words):
    return sorted(words)

def print_first_word(words):
    word = words.pop(0)
    print(word)

def print_last_word(words):
    word = words.pop(-1)
    print(word)

def sort_sentence(sentence):
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Sam_Cameron
  • 105
  • 2
  • 11

1 Answers1

0

The return keyword is to exit a function and return a value.

To let a function return a value, use the return statement

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
  • Thanks for replying. The thing is, I don't know what it means to return a value. It's the return part. What happens when it is returned? – Sam_Cameron Mar 31 '20 at 00:49
  • 1
    @SamCameronPY3, that depends on the code that called the function. In `words = sort_sentence(sentence)`, for example, the value returned by `sort_sentence` is assigned to `words`. – Charles Duffy Mar 31 '20 at 00:52
  • @SamCameronPY3, ...by the way, up at the top of your question, it has a link to the other question it was closed as a duplicate of -- [What does return do? Nothing is ever returned](https://stackoverflow.com/questions/19066925/what-does-return-do-nothing-is-ever-returned). Follow that link and read the answers there, which have been around longer, and thus had more time to be improved/vetted/commented-on/voted-on/etc. – Charles Duffy Mar 31 '20 at 00:55
  • @SamCameronPY3 Let's say I have a function called gimmeFive. That function only has one line: return 5. Now I can write x = gimmeFive(), and variable x will equal 5 after that. That's what returning a value means. – see sharper Mar 31 '20 at 00:55
  • Note that even when you don't say `return` anywhere in your function, Python has an *implicit* return value of `None`. If you don't tell a function it should return some non-None thing, then None is what it returns. – Charles Duffy Mar 31 '20 at 00:57
  • So if you change `def gimmeFive(): return 5` to just `def gimmieFive(): 5`, then someone who runs `x = gimmieFive()` will have `x == None`, instead of `x == 5`. The `5` just gets thrown away when `gimmieFive` exits. – Charles Duffy Mar 31 '20 at 00:58
  • @CharlesDuffy So it exits the function and assigns a value to the function to be called upon later?` – Sam_Cameron Mar 31 '20 at 01:01
  • 1
    Exits the function, yes. Assigns a value to the function, no. *Every single time* a function is called, its contents are executed, and then the caller receives any value that was returned during that execution. The caller might not do anything with that value -- it might just be thrown away -- but each and every function call that doesn't throw an exception or otherwise terminate abnormally returns a newly determined (but not necessarily different; as aforementioned, many functions just return `None`) return value. – Charles Duffy Mar 31 '20 at 01:07
  • 1
    Honestly, if you're having trouble here, the educational tool I'd most be likely to point you at would be a stack machine simulator -- something like TIS-100 if we're going to go with a game. Or... I might be showing my age, but have you ever used an RPN-based calculator? – Charles Duffy Mar 31 '20 at 01:10
  • If you use a function on the right side of an assignment, like 'a=function()', then 'a' is assigned whatever is returned from 'function()', as long as the returned value is of the same datatype as 'a'. – futureExpert Mar 31 '20 at 15:35
  • I understand it now. Thanks everyone. Thanks @CharlesDuffy. – Sam_Cameron Mar 31 '20 at 18:36