返回函数是干什么用的? [英] What is the return function used for?

查看:66
本文介绍了返回函数是干什么用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前在学习 python 时在我的代码中使用过 return 函数,但我不确定它的作用.我查了一下,但我不太确定它实际上做了什么.有人可以帮忙吗?

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?

谢谢

(例如)

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)

推荐答案

return关键字是退出一个函数并返回一个值.

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

要让函数返回值,请使用 return 语句

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

这篇关于返回函数是干什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆