0

Why can't I bring a value of a variable to an other method to do something with it and bring it back?

I'm trying out methods and classes ,but I don't seem to be able to bring a variable to the method, change it and return it... See link for code: https://code.sololearn.com/cLAJCZU54TRA/?ref=app

17th Nov 2018, 2:37 PM
Samuel
Samuel - avatar
1 Respuesta
+ 1
Do there are two things going on here. First the variables. Variables can only hold values in the scope they are created. Or better said the variable it self only exsist only in de scope where it is instantiated. int myInt; // this will only exsist // inside the class or // method it was //created in But it's value can be passed on in a method as an argument if the method takes its type Example // method elsewhere. public void printInt(int myIntToPrint) { Console.WriteLine(myIntToPrint); } int myInt = 42; printInt(myInt); And the second part about it is that when you define a method you define the return type. In the previous the return type was "void" meaning this method doesnt return anything. But if I would change it to "int" instead of "void" it will return an int into a variable where this method is called
17th Nov 2018, 8:05 PM
Tihomir Balaban
Tihomir Balaban - avatar