Not understanding use of return statement in c#.
-
I am new to programming and learning on youtube. I have question in following code: using System; class test { public static void Main() { int i = 0; abc(ref i); Console.WriteLine(i); Console.ReadLine(); } public static int abc(ref int j) { j = 100; return j; } } After running this, I am getting answer 100. But when I change return statement as "return 0", again answer is 100. May I know why? If I am returning 0, method should return 0 value to variable i no? I am new to programming so please explain me in very details. Thank you so much.
-
I am new to programming and learning on youtube. I have question in following code: using System; class test { public static void Main() { int i = 0; abc(ref i); Console.WriteLine(i); Console.ReadLine(); } public static int abc(ref int j) { j = 100; return j; } } After running this, I am getting answer 100. But when I change return statement as "return 0", again answer is 100. May I know why? If I am returning 0, method should return 0 value to variable i no? I am new to programming so please explain me in very details. Thank you so much.
It has to do with variable scope. You are passing by Reference which is different than passing by value. You are basically allowing the function ABC to modify the actual variable i. For this to act as you expect you need to assign the return value from ABC to i like this: i = abc(i) and remove the Ref keyword. There is a little more to it but see below. Look here at a StackOverflow answer[^] and here at the Microsoft Ref keyword.[^]
Jack of all trades, master of none, though often times better than master of one.
-
I am new to programming and learning on youtube. I have question in following code: using System; class test { public static void Main() { int i = 0; abc(ref i); Console.WriteLine(i); Console.ReadLine(); } public static int abc(ref int j) { j = 100; return j; } } After running this, I am getting answer 100. But when I change return statement as "return 0", again answer is 100. May I know why? If I am returning 0, method should return 0 value to variable i no? I am new to programming so please explain me in very details. Thank you so much.
You aren't actually using the return statement in your code. What is happening here is that the ref statement is the thing that is actually setting the value of the variable. If you want some fun, try to predict what the value of this statement is (changing your code slightly):
int i = 0;
i = abc(ref i);Edited with the correct statement on line 2.
This space for rent
-
You aren't actually using the return statement in your code. What is happening here is that the ref statement is the thing that is actually setting the value of the variable. If you want some fun, try to predict what the value of this statement is (changing your code slightly):
int i = 0;
i = abc(ref i);Edited with the correct statement on line 2.
This space for rent
Fortunately, you'll get a compiler error:
Error CS1510 A ref or out value must be an assignable variable
But ... back in the seventies I worked with a FORTRAN compiler that didn't issue an error, and would happily change the value of constants. Imagine if this C# code worked:
void xx(ref int i)
{
i += 10;
}
static int main()
{
int i = 666;
xx(666);
int j = 666;
Console.WriteLine($"{i}, {j}");
}And printed
666, 676
Now debug that in a 1,000,000 line project without an IDE! :laugh:Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Fortunately, you'll get a compiler error:
Error CS1510 A ref or out value must be an assignable variable
But ... back in the seventies I worked with a FORTRAN compiler that didn't issue an error, and would happily change the value of constants. Imagine if this C# code worked:
void xx(ref int i)
{
i += 10;
}
static int main()
{
int i = 666;
xx(666);
int j = 666;
Console.WriteLine($"{i}, {j}");
}And printed
666, 676
Now debug that in a 1,000,000 line project without an IDE! :laugh:Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
Doh, it was meant to be
i = abc(ref i);
That's what I get when I type directly into the code editor.
This space for rent
-
It has to do with variable scope. You are passing by Reference which is different than passing by value. You are basically allowing the function ABC to modify the actual variable i. For this to act as you expect you need to assign the return value from ABC to i like this: i = abc(i) and remove the Ref keyword. There is a little more to it but see below. Look here at a StackOverflow answer[^] and here at the Microsoft Ref keyword.[^]
Jack of all trades, master of none, though often times better than master of one.
Thank you so much for your reply. But my question is that when i write statement "return j", it is returning value of j. This part I got but when I write "return 0", why it is not giving answer as 0? As I am new, I am not getting this. Please explain me in a little bit detail. Thank you
-
Thank you so much for your reply. But my question is that when i write statement "return j", it is returning value of j. This part I got but when I write "return 0", why it is not giving answer as 0? As I am new, I am not getting this. Please explain me in a little bit detail. Thank you
The problem you have is that you are mixing two things up here. Let's break this method down:
public static int abc(ref int j)
{
j = 100;
return 0;
}The method signature indicates that you are accepting a variable that you are going to populate in this method. It also indicates that you are returning a type of
int
as the return type from the method (the bit that goesint abc
). This means that you have told the compiler that you have two methods of setting values here. Now, suppose you changed your call to this:int output = abc(ref i);
You now have two values coming back from the method. The value in
output
would be 0, as that is the value coming back from the return statement. The value ini
would be 100 because that is the value you setj
to inabc
.This space for rent
-
The problem you have is that you are mixing two things up here. Let's break this method down:
public static int abc(ref int j)
{
j = 100;
return 0;
}The method signature indicates that you are accepting a variable that you are going to populate in this method. It also indicates that you are returning a type of
int
as the return type from the method (the bit that goesint abc
). This means that you have told the compiler that you have two methods of setting values here. Now, suppose you changed your call to this:int output = abc(ref i);
You now have two values coming back from the method. The value in
output
would be 0, as that is the value coming back from the return statement. The value ini
would be 100 because that is the value you setj
to inabc
.This space for rent
Thank you so much for your helpful explaination. Now I got the actual concept of return statement. Thank you so much.
-
Thank you so much for your helpful explaination. Now I got the actual concept of return statement. Thank you so much.
You are most welcome.
This space for rent
-
I am new to programming and learning on youtube. I have question in following code: using System; class test { public static void Main() { int i = 0; abc(ref i); Console.WriteLine(i); Console.ReadLine(); } public static int abc(ref int j) { j = 100; return j; } } After running this, I am getting answer 100. But when I change return statement as "return 0", again answer is 100. May I know why? If I am returning 0, method should return 0 value to variable i no? I am new to programming so please explain me in very details. Thank you so much.
Hi Hassan, You are getting result 100 because your function abc(ref int i) is accepting ref type parameter. Generally, the ref keyword is used to pass parameter as a reference this means when the value of parameter is changed in called method, then that will get reflected in calling method also. Whenever the following function executed, the result of value of j will be assigned to your parameter i.
public static int abc(ref int j)
{
j = 100;
return j;
}That's the reason you are getting i values as 100 even after you change return statement as "return 0". If you want to know more about return statement and ref keyword check following topics it will help you understand how & when to use these keywords clearly in c#. Ref Keyword in C#[^] Return Statement in C#[^]