+ 2

can we use methods without declare variables before it?

18th Jul 2016, 3:52 PM
Mohamed Fawzy
Mohamed Fawzy - avatar
3 Answers
+ 5
"int myInt = 5;". "static int MyMethod(int x)". @Dainius, the above code isn't JavaScript. Even if you may be right theoretically, using another language's vocabulary just confuses beginners like us.
20th Jul 2016, 4:12 PM
ZinC - avatar
it is not important to declare variable you can use variable without declaring it.
30th Jul 2016, 10:30 AM
Muhammad Raza
- 1
The method takes in parameters. F.e.: static int MyMethod(int x) { return x } The method itself does not require you to declare anything prior to it. However if you want to use the method you have to provide some kind of input. In this (MyMethod method) case the final data type of this input would be an int. F.e.: int myInt = 5; static int MyMethod(int x) { return x } MyMethod(myInt); Furthermore the variable does not have to be declared "before" the method. As you usually write the method and then use it later on in your code. F.e.: static int MyMethod(int x) { return x } int myInt = 5; MyMethod(myInt); Another use for the method completely answers your question. In this case you do not declare variables at all. You pass in the values directly to your parameters. However this is hard coding. F.e.: static int MyMethod(int x) { return x } MyMethod(x: 5); Finally I want to point out that the name of the variable does not have to equal the parameters name. You can save the variable with a different name but within the method only the value of the variable is saved with the name of the paramater (in this case x). To summarise - the answer to your question is Yes, but you need some type of Input to use the Method itself. However, you do not have to declare the variable before the Method. For further information I would strongly sugguest visiting MSDN. Have a nice day~
20th Jul 2016, 11:40 AM
Dainius Varanauskas
Dainius Varanauskas - avatar