+ 1
Return function
How do you use the return function? What is it?
5 Antworten
+ 8
Sage Starr ,
there are 2 comments i like to share according your sample code:
(1) it is mentioned that using the *return* statement "It's similar to the print function". sorry to say, but this is completely wrong. what return is doing, is to send one value or multiple values back to the caller of the function.
(2) there is no output from the mentioned code. to fix this, we need to use a variable, that gets the return value/s and store it for future use (in this case for output, but could be used also for other operations).
> so the function call should be done like:
...
result = person_name('John', 'Smith')
print(result)
#or do it in one line:
...
print(person_name('John', 'Smith'))
+ 6
here is a sample code that demonstrates the use of the return statement:
https://sololearn.com/compiler-playground/c8w79FUr1j7u/?ref=app
+ 1
It's a statement sometimes used with classes and method to return a particular value which could be a string, sentence, number, e.t.c. It's similar to the print function.
For example:
def person_name(first_name, last_name):
""" Returns the full name of a person."""
full_name=f" {first_name} {last_name}."
return full_name
Once the function is called; the full name of the person is outputted.
#Calling the function.
person_name('John', 'Smith')
The output is:
John Smith.
+ 1
Thank you for the help
0
Inside functions, u need to use return coz some cases the print function does not work and gives error