+ 1

Beginner

What does the code "return" really do ?

9th Jun 2024, 2:21 PM
Legend X-AR
Legend X-AR - avatar
6 Answers
+ 8
Legend X-AR , here is a simple code that demonstrates what `return` statement is doing: def average_temp(temp1, temp2): average = (temp1 + temp2) / 2 return average temp1 = float(input()) temp2 = float(input()) result = average_temp(temp1, temp2) print(result) in this code, we take two float numbers as input from the user and store them in two variables (`temp1` and `temp2`). then we call the function `average_temp()` with these two inputs as arguments. the function `average_temp()` takes two arguments. Inside the function, it calculates the average of the two values. after calculating, it sends (or "returns") the result back to where the function was called. finally we print the result.
9th Jun 2024, 7:22 PM
Lothar
Lothar - avatar
+ 5
A function takes input, does some processing, and returns an output value to the caller. The return statement is what returns the output value to the caller.
9th Jun 2024, 5:28 PM
Brian
Brian - avatar
+ 4
Hi, Legend X-AR! In Python, return always terminates a function and sends back the value specified after it; None if nothing is specified.
10th Jun 2024, 8:02 AM
Per Bratthammar
Per Bratthammar - avatar
+ 1
This is a bit trickier to explain in context of the Sololearn Python interpreter set up, as it works nothing like most Python interpreters you will find. Sololearn treats python programs as if it is calling a function, where all input() calls are arguments to pass the function, and all print() calls are the function returning something to Sololearn. Normal Python interpreters don’t work that way. The print() and input() functions are purely for interfacing with the user, and you can see when each of them run. The return statememt does inside a function what Sololearn makes print() do for a whole program: It gives the outer scope that called the function some value. For example, in the code of the input() function, there is a return statement run on the text it has to read from the Standard Input stream, so it passes it back out to your code that called the input() function.
9th Jun 2024, 2:42 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
Need someone to help with your work? Try https://domyhomeworkfor.me/. They have a team of dedicated writers who provide expert assistance in various subjects. The service is efficient and ensures high-quality work, perfect for students balancing multiple tasks.
2nd Jul 2024, 6:54 AM
Eddy Smith
Eddy Smith - avatar
0
Eddy Smith You are disgusting because of the horrible service you represent. You are actively stopping students from learning by tempting them to take the “easy” way and give up the learning experience. An experience they actually took effort to get the chance to have in the first place, but you want them to forget that so you can appear as a savior to them when in reality you are a thief, attempting to rob them of money for the “service” of robbing them of education.
2nd Jul 2024, 10:54 AM
Wilbur Jaywright
Wilbur Jaywright - avatar