I still don't really get return. Can someone pls explain it to me properly? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 15

I still don't really get return. Can someone pls explain it to me properly?

a question

5th Mar 2017, 1:27 AM
Jethro Tan
Jethro Tan - avatar
22 Answers
+ 34
For a function to return something means it gives back a value you can use. Ex: If you have a function that multiplies 3 numbers together (let's say multiply()), you'll probably want that result back. So every time you use multiply(), it's like it's replaced it with whatever result you return. Ex: multiply(2, 4, 3) is the same as 24. You can print out that value immediately, store it in a variable, or use that value for another function.
5th Mar 2017, 1:34 AM
Tamra
Tamra - avatar
+ 7
For example if you have method which does addition of two numbwrs and you want to use that sum later i. e. public int sum() { int a, b, sum; sum = a + b; return sum; When you call the method sum() ; it will return value of sum and in next line you can write if(sum >5) { System.out.println("sum is greater) ; else{ System.out.println("sum is smaller) ; }
5th Mar 2017, 1:36 AM
goga
+ 5
I get it
5th Mar 2017, 1:38 AM
Jethro Tan
Jethro Tan - avatar
+ 4
thanks
5th Mar 2017, 1:38 AM
Jethro Tan
Jethro Tan - avatar
+ 3
To understand return, see it as a memory holder for whatever is inputed so that it can be recalled several times in the code. It goes alongside the def function which specifies what the purpose of the code is while the expected sub values are the return function. The final output can then be called or printed. E.g Def color_mixer(red, blue, yellow, white, black) red + blue return purple red + yellow return orange blue + yellow return green red + white return pink black + white return gray red + blue + yellow return brown color_mixer(red, blue, yellow, white, black)
12th Mar 2022, 4:31 PM
ijeoma Nsaka
ijeoma Nsaka - avatar
+ 2
def func(): print("2") return 5 print("3") print(func()) Terminal: 2 5
29th Jul 2021, 8:07 PM
Bobbersnucker
Bobbersnucker - avatar
+ 2
Another good way to think about it is to imagine a function as a black box. You put your arguments into it, turn it on, and something comes out the other side. The rest of the programme doesn't actually need to know what's going on inside the box. It just needs the finished product. 'Return' is the thing the box spits out.
29th May 2022, 6:56 PM
Sarah C
Sarah C - avatar
+ 1
Uuups I wrote it in Java :) now I see you put python
5th Mar 2017, 1:40 AM
goga
+ 1
nvm I still get it
5th Mar 2017, 1:41 AM
Jethro Tan
Jethro Tan - avatar
+ 1
Returning is optional because even you can use print function and call the values. But short ways is to write return and Then it will return the values or calculated one
19th Mar 2021, 8:01 AM
Nar Bdr Kharka
Nar Bdr Kharka - avatar
+ 1
Return is also a function which returns the current running function results. i.e in the form of addition of two numbers or like other. return only returns the function results after the completion of the function.
22nd May 2022, 5:33 AM
Muhammad Rehan
Muhammad Rehan - avatar
+ 1
Basically return just returns the operation that just took place in a function
8th Oct 2022, 10:06 PM
Osei Joseph Aboagye
Osei Joseph Aboagye - avatar
+ 1
I’m thinking about return like “give me the result you got”
25th Nov 2022, 6:55 AM
Jakub Huťa
Jakub Huťa - avatar
+ 1
Return means, functions which gives back to the main code
26th Nov 2022, 4:28 AM
Pamuda U De A Goonatilake
Pamuda U De A Goonatilake - avatar
0
ohh
5th Mar 2017, 1:35 AM
Jethro Tan
Jethro Tan - avatar
0
thanks
5th Mar 2017, 1:36 AM
Jethro Tan
Jethro Tan - avatar
0
For a function to return something means it gives back a value you can use. Ex: If you have a function that multiplies 3 numbers together (let's say multiply()), you'll probably want that result back. So every time you use multiply(), it's like it's replaced it with whatever result you return. Ex: multiply(2, 4, 3) is the same as 24. You can print out that value immediately, store it in a variable, or use that value for another function.
24th Jul 2021, 4:57 AM
Vaibhav Nehe
Vaibhav Nehe - avatar
0
Excelent
9th Aug 2021, 8:44 PM
pablo Salaza
pablo Salaza - avatar
0
He is when a creator of a variable, but with not a variable for pick, you can save in one using a function and print he, or print(function)
29th May 2022, 2:05 PM
Gabriel Henrique
Gabriel Henrique - avatar
0
return is simply giving back a value you can use later
16th Jul 2022, 4:56 PM
Peter Omienge
Peter Omienge - avatar