#๐Ÿ”’ return

66 messages ยท Page 1 of 1 (latest)

gilded coral
#

ive been trying to understand what a return statement does, can anyone explain it to me in simple terms

pastel cradleBOT
#

@gilded coral

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rapid pine
gilded coral
#

kinda yeah

rapid pine
#

can you explain in your own words what you think it is?

gilded coral
#

calling a function is naming it so u can use it later on

#

like this

rapid pine
#

not quite

#

that's defining a function

gilded coral
#

oh

rapid pine
#

def is short for "define"

gilded coral
#

say_hello would be the caller ?

rapid pine
#

say_hello("Timmy") would be the caller

#

say_hello is the name of the function

#

the () is what actually causes the function to run

#

this is "calling"

gilded coral
#

ah

rapid pine
#

!e

def foo():
    print("Hello!")


foo()
pastel cradleBOT
rapid pine
#

here I define foo, and then call it

#

!e

def foo():
    print("Hello!")


foo()
foo()
foo()
foo()
pastel cradleBOT
rapid pine
#

functions are great because we can define it once, but then reuse it as many times as we want

gilded coral
#

oh its to reuse it

#

and not write to much code

rapid pine
#

yes exactly

#

but because functions can also allow for parameters, it makes it even more versatile

#

since we can reuse it but also get various results

#

!e

def greeting(name):
    print("Hello", name)


greeting("Theryx")
greeting("Fashoomp")
greeting("Tommy")
pastel cradleBOT
gilded coral
#

alright

rapid pine
#

ok, especially as a beginner, most of the programs you write have a goal to "print" something

#

but that's not always going to be the goal of writing code

#

printing actually contains no data of its own

#

it's simply a way for us to display some output

#

so sometimes we might want a function capable of performing a task, and giving us the result

#

are you familiar with the len function yet?

gilded coral
#

yeah, i think it means how "long" the word is or how many characters in the word

#

cant really explain it too well

rapid pine
#

yes exactly

#

len is a function

#

it doesn't print anything when we use it

#

it simply returns the value

#

so if I do len("abc"), it will return 3

#

we can store that result in a variable

#
num_letters = len('abc')
#

we are assigning a variable to the result of calling this function

#

python will always evaluate whatever is on the right side of the = first

#

so it will do len('abc'), which gives a result of 3

#

then it will assign that 3 to num_letters

#

this is only possible with return

gilded coral
#

oh

gilded coral
rapid pine
#

so we're not assigning a variable to len('abc')

#

we're assigning a variable to the result of calling that function

#

return simply gives a value to a called function

#
def foo():
    return 5


result = foo()
#

as far as python is concerned, it sees the last line as result = 5

gilded coral
#

oh okay so 5 is foo() right

rapid pine
#

foo() is called, which then returns a value of 5

gilded coral
#

ur explanation is very good im starting to really understand everything better

rapid pine
#

let me know if you have any further questions about it

gilded coral
#

alright yeah i apperciate the help

pastel cradleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.