Reputation: 70
In the following example I have two methods that do the same thing:
public class Program
{
int result=0;
int result2= 0;
public void Add(int num1, int num2){
result = num1 + num2;
}
public int Add2(int num1, int num2){
return result2 = num1 + num2;
}
public static void Main(string[] args)
{
Program program = new Program();
program.Add(5,6);
Console.WriteLine(program.result);
program.Add2(4,2);
Console.WriteLine(program.result2);
}
}
I get that the void method doesn't return a value, so in the above example when I call Add(int num1, int num2), is it not returning the result back to the global variable?
If that's the case why is it that when I print the variable it prints out the result after the addition? Shouldn't it print out 0?
Upvotes: 0
Views: 3377
Reputation: 1070
public class Program
{
int result=0;
int result2= 0;
public void Add(int num1, int num2){
result = num1 + num2;
Console.WriteLine(result);
}
public int Add2(int num1, int num2){
return result2 = num1 + num2;
}
public static void Main(string[] args)
{
Program program = new Program();
program.Add(5,6);
Console.WriteLine(program.result);
program.Add2(4,2);
}
Upvotes: -1
Reputation: 56697
Both methods are equals in that they have unexpected side effects - something you really want to avoid, usually.
The first method does not return an immediate result - it just sets the global variable. The second method both returns an immediate result and sets the global variable. The fact that a method does not return an immediate result does not mean that it can not change any "global" (class or instance-level) fields.
The preferred variant is to return an immediate result without any side effects like this:
public static int Add(int a, int b)
{
return a + b;
}
In your class you would then call this like
result = Program.Add(6, 3);
Upvotes: 2
Reputation: 23732
when I call Add.. is it not returning the result back to the global variable?
No. You don't have a global variable. You have 2 class fields. Internally both your methods do the same thing! They assign the result of a calculation to a class field. Therefore the result is saved and can be accessed in the main method:
Console.WriteLine(program.result2);
Shouldn't it print out 0?
No because the class field has been initialized with the resulting value.
The second method is additionally returning the variable result2
after the calculation and the assignment! The real difference is that you can use the return value of the second method right away in the main:
int n = program.Add2(4,2);
Upvotes: 1
Reputation: 530
In your main method you could do
public static void Main(string[] args)
{
Program program = new Program();
int addedValue = Add2(4,2);
//Work with addedValue
Console.WriteLine(addedValue);
}
But if you tried int addedValue = program.Add(5,6);
you would get a compiler error.
Upvotes: 0
Reputation: 3012
Your Add
method is setting a member variable (result), hence why after you execute the method and you print out the result the value is 11.
Upvotes: 0
Reputation: 7803
The void methodf does not return any type back to the calling code, but it does assign the value of the addition to the global variable.
Void messages can affect the state/value of fields within the Class
Upvotes: 0
Reputation: 101680
The method itself doesn't return any value but that doesn't mean it can't change the value of an instance field. You have a field named result
and you are assigning a value to it in your method, so that value is stored in the result
and that's why it's printed in the console.
Upvotes: 1