+ 1
Methods, c#
Sqr seems to be changed to a new value? It goes from x to a? Am I right? namespace SoloLearn { class Program { static void Sqr(int x) { x = x * x; } static void Main(string[] args) { int a = 3; Sqr(a); Console.WriteLine(a); } } }
2 Respuestas
+ 1
No.
The value you send to Sqr, but not get any return value. And arguments are passed by values.. Means sent values only, in the function it get stored in separate variable x, is different from a in main. Both have separate locations.. Changing on one value doesn't affect on other value..
So x value is 9 while a remains 3.
+ 1
That what you mean can be reached i.e. as follows:
static void Sqr(ref int x)
{
x = x * x;
}
static void Main(string[] args)
{
int a = 3;
Sqr(ref a);
Console.WriteLine(a);
}