Code Reuse

One of the big benefits of methods is the idea of code reuse. If you write a method the right way, then you need only write it once, and you can use it over and over again. For instance, we use Console.ReadLine() many times in our programs.

This GetInput() method takes a string which serves as a prompt, and then returns an Integer. Once you have the method written, you can use it over and over again.

In general, this means we are entering a new phase, in that when it comes time to do homework or take a test, you can begin to start out with a set of methods you write that you can simply paste into the test or homework to complete a particular task.

There are really two interrelated ideas here. One is code reuse, and the other is avoiding repetition. If you have approximately the same code in twenty places in your program, and you find a bug in that code, then you have to fix it in 20 places, which is an error prone process. So instead you put the code in a method, and then call it twenty times. If you need to fix it, then you fix it once, and all twenty places that call it get the benefit.

"Why would I want to create a method like GetInput()?"

It is helpful when you want to reuse code. Now you have a method called GetInput() that you can use whenever you need to prompt the user for integer based input. All you need to do is pass in the appropriate prompt, and everything else takes place automatically. You can customize the method for different situations by passing a different prompt. "Enter a number and I will find the square of all numbers between it and 2000" "Enter a number and I will find its square root." "Enter a temperature and I will convert it to celsius, etc."

It's a bit abstract, and not entirely relevent, but here is at least a hint as to how important the concept of code reuse is to programmers: