+ 1
I always read about returning values and it explains that the return keyword returns a value, but what does that even mean?
what i meant is like what does it exactly do like what happens in there i have no idea!
5 odpowiedzi
+ 6
Basically its used in functions.When a function is called, it can recieve and return a value. Returning a value simple means the function will give the system a value.
for example
int x = Funct(1);
This means the function recieves a value of '1' and whatever it returns/gives will go to 'x'
+ 5
You can have a method or class like so
public int Money(int amount)
{
Money += amount;
return amount;
}
then return will return control from the current method to the caller, and also pass back whatever argument is sent with it. In the example Money is defined to return an integer, and to accept an integer as an argument. The value returned is actually the same as the argument value.
A returned value is used from somewhere else in your code that calls the defined method, Money in this example.
int setMoney = Money(1);
would mean that setMoney gets assigned the value of 1.
Console.Writeline(Money)
// Outputs 1
What this breaks down to is that Money is evaluated. The evaluation of Money is based on what the method returns. Money returns the input, thus, Money will evaluate to 1 in the above example.
+ 2
Some further examples:
Maybe this will make you say "Heureka!".
1. variable assignment
int x = Math.Pow(2, 4)
is the same as
int x = 16;
2. function calls
char[] Foo = { 'B', 'a', 'r' };
int x = Math.Pow(2, Array.IndexOf(Foo, 'r'');
is the same as
int x = Math.Pow(2, 2)
or
int x = 4;
3. logical conditions
int x = 8;
if (Math.Pow(2, 3) == x)
//code
is the same as
if (8 == x)
//code
or
if (8 == 8)
//code
or
if (true)
//code
+ 1
thank you guys alot, although I'll have to read even more about that to be more sure of what it is
0
Here is an example
int func(){
return 5;
}
void main(String[] args){
int t = func();
System.out.println(t); // prints 5
}