+ 15
I still don't really get return. Can someone pls explain it to me properly?
a question
22 Antworten
+ 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.
+ 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
+ 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)
+ 2
def func():
print("2")
return 5
print("3")
print(func())
Terminal:
2
5
+ 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.
+ 1
Uuups I wrote it in Java :) now I see you put python
+ 1
nvm I still get it
+ 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
+ 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.
+ 1
Basically return just returns the operation that just took place in a function
+ 1
I’m thinking about return like “give me the result you got”
+ 1
Return means, functions which gives back to the main code
0
ohh
0
thanks
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
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