← All lessons

Level 1 / C# Fundamentals / fundamental 5 of 7

Writing methods (functions)

Write methods that take parameters, return values, and wrap up the if statements and loops you already know.

A method is a named, reusable block of code: what other languages call a function. You give it inputs (parameters), it does its work, and it can hand back one value. Write these the way they would sit inside a class; access modifiers like public and static come in near the end.

Understand the idea

The signature reads like a sentence

int Add(int a, int b) says: a method named Add, which takes an int called a and an int called b, gives back an int. Every part of the English description maps to one part of that first line.

return hands a value back

return ends the method immediately and gives the caller a value of the return type. A method marked void returns nothing; it does work like printing instead.

Calling a method

Add(2, 3) runs the method with a = 2 and b = 3, and the call itself becomes the returned value, so you can store it, print it, or use it inside another expression.

English to C#

“Write a method called Add that takes two ints, a and b, and returns their sum.”

int Add(int a, int b)
{
return a + b;
}
int
the type of value it returns (void for none)
Add
the method name, in PascalCase
(int a, int b)
the parameters: type and name for each input
return a + b;
hand the result back to the caller

Rep 1 of 14

Copy it

In English

Write a method called SayHello that returns nothing and prints "Hello".

Type the code shown below. Watch it turn green as you go.

void SayHello()
{
Console.WriteLine("Hello");
}

Waiting for you to type · 0 of 13 pieces

Ctrl+Enter checks or moves on. Tab indents. Paste is off on purpose: your hands are learning this too.

Reps

0 of 14 done

Reps unlock in order, because each one builds on the last.

How your code is checked

Drills compare what you type with known-correct C#, token by token. Spacing, line breaks, and comments do not matter; spelling, case, and punctuation do. It is not a compiler, so it accepts the listed spellings of each answer and nothing else.

Try it in a real .NET project

Move the logic from your earlier console exercises into methods: one that parses input, one that does the math, and one that prints the result. Your top-level code should end up as three readable method calls, and each method can be tested on its own.

Check your understanding

What is the difference between a method that prints its answer and one that returns it?

Show the explanation

A void method that prints shows the value once and the caller can do nothing more with it. A method that returns the value hands it back, so the caller can store it, compare it, pass it to another method, or decide to print it. Returning values is what makes methods reusable building blocks.

Goose School teaches Microsoft's stack. Goose Solutions is not affiliated with, endorsed by, or sponsored by Microsoft.