using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int r = Mul2(40, 54); Console.WriteLine(); Console.ReadLine(); } static int Mul2(int someInt, int someInt2) { Console.WriteLine("Method has become to work"); Console.WriteLine(someInt); Console.WriteLine(someInt2); int Result = (someInt * someInt2); Result = Result * Result; return Result; Console.WriteLine("Method end!"); } } } Closed due to the fact that off-topic participants αλεχολυτ , pavel , katso , Suvitruf ♦ , iluxa1810 8 Nov '16 at 17:13 .
It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:
- “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - αλεχολυτ, pavel, katso, Suvitruf, iluxa1810
3 answers
In the main method:
1) The variable r you return the result of the method Mul2 .
2) Do not write to the console using Console.WriteLine () ;
3) Wait for the input line.
In the Mul2 method:
1) It is reported that the method has begun to work, the first and second parameters are displayed;
2) In the new Result variable we write the multiplication of the first and second numbers;
3) Next, the Result is assigned to the multiplication itself;
4) Return the values of the variable Result .
An error (or warning?) Pops up, something like statement not reachable
An error (or a warning?) Due to the fact that the method has already completed its work (the return statement "terminates" the method), and you are trying to do something else there (in this case, write to the console that the method has completed the work).
That the method earned and the line Console.WriteLine () became "reachable", lift it just above the return statement .
- 32) Do not write to the console using Console.WriteLine (); - and how the same carriage return? :-) - Evgenii Izhboldin
- @EvgeniiIzhboldin, ay-i-yay)) I caught it, well done!) - Ep1demic
The program is started and the Main method is automatically called, which declares an integer variable r and assigns the result of the Mul2 method Mul2 . Those. from Main call Mul2 with parameters 40 and 54, and write the result to the variable r . When the Mul2 method is Mul2 message is displayed that started working, displaying the first transferred variable (40) and the second (54).
In the integer variable Result write the product 40 x 54 (3780).
After that, in the Result variable we write the previous value of the same variable, multiplied by itself (squared). Those. Result will be equal to Result = 3780 х 3780 = 14288400 .
Now from the Mul2 method, Mul2 return the value of this 14288400 to the method called 'Main' where the value 14288400 is assigned to the variable r .
Now print the empty string, and press Enter. End the program.
Output the string "Method end!" will not be executed, since the command for outputting this text is written after the return , which not only returns the result to the Main method, it also closes the 'Mul2' method.
someInt received the value 40, someInt2 - 54. Then you calculated the result by multiplying 40 and 54, then multiplied result by itself and the function returned this value, which was received by the variable r in Main.
- And again: the line "Console.WriteLine (" Method end! ");" - not reachable. The function returned a new Result. - Kryshtop
- And why the line "Console.WriteLine (" Method end! ");" - not reachable? - user7130621
- @ user7130621, because after executing the return function is abandoned - Grundy
- Thank you everybody. - user7130621
=. Code processing goes line by line. - PinkTux