#๐ return
66 messages ยท Page 1 of 1 (latest)
@gilded coral
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.
Do you understand what "calling" a function is?
kinda yeah
can you explain in your own words what you think it is?
oh
def is short for "define"
say_hello would be the caller ?
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"
ah
!e
def foo():
print("Hello!")
foo()
:white_check_mark: Your 3.12 eval job has completed with return code 0.
Hello!
here I define foo, and then call it
!e
def foo():
print("Hello!")
foo()
foo()
foo()
foo()
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | Hello!
002 | Hello!
003 | Hello!
004 | Hello!
functions are great because we can define it once, but then reuse it as many times as we want
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")
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | Hello Theryx
002 | Hello Fashoomp
003 | Hello Tommy
alright
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?
yeah, i think it means how "long" the word is or how many characters in the word
cant really explain it too well
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
oh
i didnt know about this
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
oh okay so 5 is foo() right
foo() is called, which then returns a value of 5
ur explanation is very good im starting to really understand everything better
let me know if you have any further questions about it
alright yeah i apperciate the help
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.