I still don't really get return. Can someone pls explain it to me properly? | Sololearn: Learn to code for FREE!

+15

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

a question

3/5/2017 1:27:03 AM

Jethro Tan

19 Answers

New Answer

+32

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.

+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) ; }

+5

I get it

+4

thanks

+2

def func(): print("2") return 5 print("3") print(func()) Terminal: 2 5

+2

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)

+1

Uuups I wrote it in Java :) now I see you put python

+1

nvm I still get it

+1

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.

0

0

thanks

0

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

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.

0

Excelent

0

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.

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)

0

return is simply giving back a value you can use later

0

Peter Omienge it's a post from 5 years ago looool

0

The main point of using return in a function is that let the function do it's operations on the given values and at the final it should store the result and return it to us.... And after return we will be able to use the stored result in different operations and different ways as we want.