The problem: repeated code
Imagine a student named Andy who has to write the same three lines on a whiteboard three times. As a C# program, the direct way looks like this:
Console.WriteLine("This is Andy");
Console.WriteLine("Andy made a mistake");
Console.WriteLine("Andy suffers the consequences");
Console.WriteLine("This is Andy");
Console.WriteLine("Andy made a mistake");
Console.WriteLine("Andy suffers the consequences");
Console.WriteLine("This is Andy");
Console.WriteLine("Andy made a mistake");
Console.WriteLine("Andy suffers the consequences");
For three repetitions that's manageable. But the program contains only three different instructions, and it repeats them in nine lines. If the teacher asks for ten copies, the program has thirty lines, and for a hundred copies it has three hundred. Long, repetitive code is tiring to read, and it's easy to make a mistake while copying it.
The solution: put the lines in a method
A method lets you write the lines once, give them a name, and run them by calling that name:
void PrintMessage() {
Console.WriteLine("This is Andy");
Console.WriteLine("Andy made a mistake");
Console.WriteLine("Andy suffers the consequences");
}
PrintMessage();
PrintMessage();
PrintMessage();
The output is the same nine lines as before. The three calls take three lines, however long the body is. A fourth copy costs one more line, not three, and the saving grows with the size of the body.
Changing the story
The real advantage appears when something changes. Suppose the student is Mike, not Andy. In the repeated version you'd edit every one of the nine lines. In the method version you edit three lines, in one place:
void PrintMessage() {
Console.WriteLine("This is Mike");
Console.WriteLine("Mike made a mistake");
Console.WriteLine("Mike suffers the consequences");
}
Every call now prints the new text, so the five calls in the lesson's example all pick up the change. Having a single place that defines a piece of behavior has a name in programming: avoiding repetition, or keeping the code "DRY". Each fact then lives in one spot, so the program can't disagree with itself.
What functions buy you
- Less code. A body is written once, however many times it runs.
- Easier changes. You fix or update behavior in one place.
- Fewer mistakes. There are no copies that drift apart because you edited some and forgot others.
- Readable programs. A name like
PrintMessagetells the reader what a block of code is for, so the calling code reads like a list of tasks.
The five calls in the example still repeat PrintMessage();, and that repetition can be removed too. Loops, which come later in the course, handle it.
The exercise, step by step
The editor holds a MainFunction that already prints a line between two spots marked by comments. The task is to introduce yourself in both spots without writing the same code twice.
- Define the method. Under the
// Function goes here:comment, writepublic void IntroduceYourself()with a pair of braces. It'svoidbecause it only prints. The name uses PascalCase, which is the C# convention. - Print the first line. Inside the braces, print
"My name is Andy"withConsole.WriteLine. - Print the second line. On the next line, still inside the braces, print
"I code in Python". - Call it the first time. Under the first comment in
MainFunction, writeIntroduceYourself();. Remember the parentheses and the semicolon. - Call it again. After the
"Sorry, I didn't hear you"line, under the second comment, write the same call.
The finished program:
// Function goes here:
public void IntroduceYourself() {
Console.WriteLine("My name is Andy");
Console.WriteLine("I code in Python");
}
public void MainFunction() {
// Introduce yourself the first time:
IntroduceYourself();
Console.WriteLine("Sorry, I didn't hear you");
// Introduce yourself again:
IntroduceYourself();
}
Running MainFunction prints:
My name is Andy
I code in Python
Sorry, I didn't hear you
My name is Andy
I code in Python
If the introduction ever changes, only the body of IntroduceYourself needs editing, and both calls pick up the change.
Common mistakes
- Putting the print statements in
MainFunctioninstead of inside the new method. The method must contain them, or the calls do nothing. - Defining the method but leaving out the calls. Defining never runs anything.
- Calling it without parentheses,
IntroduceYourself;, which doesn't compile. - Mismatched names. C# is case-sensitive, so
introduceYourself()is a different name fromIntroduceYourself().
Key takeaways
- Write code once in a method and call it as often as needed.
- Changing the method changes every call, which keeps the program consistent.
- Good method names make the calling code easier to read.