C
C#11mo ago
Unc

can someone give me an example of what “return” does and how to use it?

can someone give me an example of what “return” does and how to use it?
34 Replies
Jimmacle
Jimmacle11mo ago
what have you searched for about it?
Angius
Angius11mo ago
It returns a value from a method
Unc
UncOP11mo ago
I searched Google and they say that it returns a value but I can’t find examples
Thinker
Thinker11mo ago
public static int Add(int number1, int number2)
{
return number1 + number 2;
}
public static int Add(int number1, int number2)
{
return number1 + number 2;
}
Console.WriteLine(Add(1, 2));
Console.WriteLine(Add(1, 2));
ThatDaniel
ThatDaniel11mo ago
Jump statements - break, continue, return, and goto - C#
C# jump statements (break, continue, return, and goto) unconditionally transfer control from the current location to a different statement.
Jimmacle
Jimmacle11mo ago
the first link i see is the one daniel linked adding c# to the end that page has an example
Unc
UncOP11mo ago
Console.WriteLine("First call:"); DisplayIfNecessary(6); Console.WriteLine("Second call:"); DisplayIfNecessary(5); void DisplayIfNecessary(int number) { if (number % 2 == 0) { return; } Console.WriteLine(number); }
MODiX
MODiX11mo ago
Angius
REPL Result: Success
public static int Add(int number1, int number2)
{
return number1 + number2;
}

int sum = Add(56, 887);

Console.Write(sum);
public static int Add(int number1, int number2)
{
return number1 + number2;
}

int sum = Add(56, 887);

Console.Write(sum);
Console Output
943
943
Compile: 357.789ms | Execution: 27.621ms | React with ❌ to remove this embed.
Unc
UncOP11mo ago
What is it returning? Oh
Jimmacle
Jimmacle11mo ago
what do you think it's returning? oh, you picked the one that doesn't return anything you should read the entire section
Angius
Angius11mo ago
Ah, in this case, nothing. The method is void
Jimmacle
Jimmacle11mo ago
the section has 2 examples, one that doesn't return anything and one that does
Angius
Angius11mo ago
return also stops the method from executing further
Unc
UncOP11mo ago
And void means u aren’t returning anything Right?
Jimmacle
Jimmacle11mo ago
right
Unc
UncOP11mo ago
In this example how could it return number1 and number2 when they aren’t numbers
Jimmacle
Jimmacle11mo ago
they are numbers look at the data types number1 + number2 is an expression that adds the numbers stored in number1 and number2 and gives you a new number
Unc
UncOP11mo ago
So 56 and 887
Jimmacle
Jimmacle11mo ago
so the return statement returns the result of that expression no they can be any number
Unc
UncOP11mo ago
Ik but in this example
Jimmacle
Jimmacle11mo ago
in this example they happen to be set to those
Unc
UncOP11mo ago
Yeah So the public static sets 56 and 887 as number1 and number2
Jimmacle
Jimmacle11mo ago
no public and static are modifiers applied to the method definition
Unc
UncOP11mo ago
Ik I meant like the method in general My bad
Jimmacle
Jimmacle11mo ago
no, the method doesn't set anything it accepts values passed to it when called like Add(56, 887); the method itself doesn't have anything to do with which values it gets
Unc
UncOP11mo ago
Last question on return Console.WriteLine("First call:"); DisplayIfNecessary(6); Console.WriteLine("Second call:"); DisplayIfNecessary(5); void DisplayIfNecessary(int number) { if (number % 2 == 0) { return; } Console.WriteLine(number); } If we wrote DisplayIfNecessary(5) Number would equal to 5?
Angius
Angius11mo ago
Yes
Unc
UncOP11mo ago
And number would only equal 5 if we put “return DisplayIfNecessary(5);” Correct?
Angius
Angius11mo ago
No, why? Just calling the method and passing a value into the parameter makes the parameter take the value Especially since DisplayIfNecessary() is a void method too, you cannot return a result of a void method
Unc
UncOP11mo ago
Oh, so the return, by itself, does it. Okay.
Angius
Angius11mo ago
The return has nothing to do with the parameters. A method can be void and have no return, and you can still give it parameters
Unc
UncOP11mo ago
Okay 👍
Jimmacle
Jimmacle11mo ago
if it helps, parameters/arguments are what go into the method and the return is what comes out
Want results from more Discord servers?
Add your server