Return function | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

Return function

How do you use the return function? What is it?

2nd May 2024, 3:51 PM
Michelle
Michelle - avatar
5 Réponses
+ 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'))
2nd May 2024, 6:03 PM
Lothar
Lothar - avatar
+ 6
here is a sample code that demonstrates the use of the return statement: https://sololearn.com/compiler-playground/c8w79FUr1j7u/?ref=app
2nd May 2024, 6:13 PM
Lothar
Lothar - avatar
+ 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.
2nd May 2024, 4:40 PM
Sage Starr
Sage Starr - avatar
+ 1
Thank you for the help
3rd May 2024, 12:11 AM
Michelle
Michelle - avatar
0
Inside functions, u need to use return coz some cases the print function does not work and gives error
3rd May 2024, 6:36 PM
Ramavath Ruchi Teja