Skip to content
wannacode

Why methods

Problem

One change, three places

You are writing the till program for a small shop. Every receipt starts with the shop's name and phone number, so you have copied those two lines everywhere a receipt is printed.

The shop changes its phone number. You fix it in two places and miss the third:

Console.WriteLine("Corner Shop");
Console.WriteLine("tel. 02 123 4567");
Console.WriteLine("Bread €1.20");
Console.WriteLine("Corner Shop");
Console.WriteLine("tel. 02 123 4567");
Console.WriteLine("Milk €2.10");
Console.WriteLine("Corner Shop");
Console.WriteLine("tel. 02 765 4321");
Console.WriteLine("Coffee €3.50");
Output
Corner Shop
tel. 02 123 4567
Bread €1.20
Corner Shop
tel. 02 123 4567
Milk €2.10
Corner Shop
tel. 02 765 4321
Coffee €3.50

The third customer gets a receipt with the old number. The more copies there are, the easier it is to miss one.

The idea

Write it once, call it by name

Put the repeated part in one place and give it a name. That is a method: a named block of code that does one job.

You write the method once. Then, wherever you need it, you write just its name with parentheses: PrintHeader();. This is called calling the method.

If the shop changes its phone number again, you fix one line in the method, and every receipt is right.

See it

What happens on a call

The program runs line by line. When it reaches a call, it goes into the method, runs its body and comes back to just after the call. Follow the ball: it is the point the program has reached.

static void PrintHeader()
{
Console.WriteLine("Corner Shop");
Console.WriteLine("tel. 02 123 4567");
}
PrintHeader();
Console.WriteLine("Bread €1.20");
PrintHeader();
Console.WriteLine("Milk €2.10");
Step by step, in words
  1. The program starts in Main and reaches PrintHeader().
  2. A call: execution goes into the method.
  3. The method prints the top of the receipt.
  4. The method is done: execution goes back to just after the call.
  5. Main carries on: it prints the bread.
  6. A second call: the same code, without writing it again.
  7. The top is printed again.
  8. Back in Main, on to the next line.

The second time, you do not write the top part again. You call the same method, and it prints the same two lines.

The idea

How to write a method

Here is the whole program, so you can see where the method goes:

Program.cs
class Program
{
static void PrintHeader()
{
Console.WriteLine("Corner Shop");
Console.WriteLine("tel. 02 123 4567");
}
static void Main()
{
PrintHeader();
Console.WriteLine("Bread €1.20");
}
}
Output
Corner Shop
tel. 02 123 4567
Bread €1.20
staticalways, for nowvoidreturns no resultPrintHeadername()takes nothing
  • The name says what the method does, so it is a verb: PrintHeader, PrintFooter.
  • void means the method only does something (here it prints) and returns no result. Methods that return a result come in the next lesson.
  • The empty parentheses mean the method takes nothing from outside. We will fill them in the next lesson too.
  • The body is between { and }.
  • The method sits in the class, next to Main, not inside Main.

For now we write static before every method. What it means comes in the Classes and objects module, along with the wrapper class Program.

In C#, method names start with a capital letter: PrintHeader, Main. You will see them like this in every C# program.

Try it

In what order?

What will the program print?

static void PrintThanks()
{
Console.WriteLine("Thank you!");
}
Console.WriteLine("Order #1024");
PrintThanks();
Console.WriteLine("Order #1025");
PrintThanks();
Answer

Try it

What is missing?

This method prints the bottom of the receipt. What goes in the blank?

static  PrintFooter()
{
    Console.WriteLine("We hope to see you again soon.");
}

Try it

Find the bug

The program does not compile. Which line is the bug on?

Pick the line with the bug.

Exercise

The receipt and the triangle

Solve these in your editor. Each one has sample output to compare yours with.

1. Receipt. Write three methods:

  • PrintLine() prints a line of 20 dashes with a loop;
  • PrintHeader() prints the shop's name, the phone number and the line;
  • PrintFooter() prints the line and "Thank you!".

In Main, print two receipts: the top, one line with a purchase, the bottom.

Sample output
Corner Shop
tel. 02 123 4567
--------------------
Bread €1.20
--------------------
Thank you!
Corner Shop
tel. 02 123 4567
--------------------
Milk €2.10
--------------------
Thank you!

2. Triangle. Write a method PrintTriangle() that uses nested loops to print a triangle of stars, 4 rows high:

Sample output
*
***
*****
*******

In the next lesson the height becomes a parameter: one method for a triangle of any height you like.

Test

Check yourself

1. When does the code in the body of a method run?
2. What does void before the method name mean?
3. A method is called in three places. How many times is its code written?
4. Where do you write the method?

Made by Ivo Gergov as part of Evo Tech's campaign for accessible programming education ·