C
C#5mo 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
Jimmacle5mo ago
what have you searched for about it?
Angius
Angius5mo ago
It returns a value from a method
Unc
Unc5mo ago
I searched Google and they say that it returns a value but I can’t find examples
Thinker
Thinker5mo 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
ThatDaniel5mo 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
Jimmacle5mo ago
the first link i see is the one daniel linked adding c# to the end that page has an example
Unc
Unc5mo 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
MODiX5mo 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
Unc5mo ago
What is it returning? Oh
Jimmacle
Jimmacle5mo 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
Angius5mo ago
Ah, in this case, nothing. The method is void
Jimmacle
Jimmacle5mo ago
the section has 2 examples, one that doesn't return anything and one that does
Angius
Angius5mo ago
return also stops the method from executing further
Unc
Unc5mo ago
And void means u aren’t returning anything Right?
Jimmacle
Jimmacle5mo ago
right
Unc
Unc5mo ago
In this example how could it return number1 and number2 when they aren’t numbers
Jimmacle
Jimmacle5mo 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
Unc5mo ago
So 56 and 887
Jimmacle
Jimmacle5mo ago
so the return statement returns the result of that expression no they can be any number
Unc
Unc5mo ago
Ik but in this example
Jimmacle
Jimmacle5mo ago
in this example they happen to be set to those
Unc
Unc5mo ago
Yeah So the public static sets 56 and 887 as number1 and number2
Jimmacle
Jimmacle5mo ago
no public and static are modifiers applied to the method definition
Unc
Unc5mo ago
Ik I meant like the method in general My bad
Jimmacle
Jimmacle5mo 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
Unc5mo 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
Angius5mo ago
Yes
Unc
Unc5mo ago
And number would only equal 5 if we put “return DisplayIfNecessary(5);” Correct?
Angius
Angius5mo 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
Unc5mo ago
Oh, so the return, by itself, does it. Okay.
Angius
Angius5mo 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
Unc5mo ago
Okay 👍
Jimmacle
Jimmacle5mo ago
if it helps, parameters/arguments are what go into the method and the return is what comes out