What a function is
A function is a named container for a few lines of code that perform one task. Instead of writing everything in one long run of statements, you split a program into small pieces, give each piece a name, and run a piece whenever you need it. The program becomes easier to read, to change and to reuse.
In C# a function is usually called a method, because it belongs to a class. The lessons show the methods on their own, and the editor around them takes care of the rest of the program.
The shape of a method
A method definition has a header and a body:
returnType FunctionName() {
// body: the statements to run
}
- The return type says what kind of value the method gives back.
voidmeans it gives back nothing. - The name identifies the method. C# style is PascalCase, with each word capitalized:
SayHello, notsayHello. - The parentheses hold the method's inputs. They're empty for now, and a later lesson fills them in.
- The body is the code between the curly braces. It's customary to indent it one level so the structure is easy to see. C# doesn't require the indentation the way Python does, but everyone does it.
The lesson's methods are also marked public, an access modifier that says other code may use the method. Here is a complete one:
public void SayHello() {
Console.WriteLine("Hello World");
}
Defining is not running
If you only define SayHello, nothing is printed. A definition tells C# that the method exists and what it does. The body runs only when the method is called, which means writing its name followed by parentheses:
SayHello(); // prints: Hello World
SayHello(); // prints: Hello World
You can call a method as many times as you like, and every call runs the whole body from the first statement to the last. The parentheses matter. Writing SayHello; by itself is a compile error, because the bare name doesn't make a call.
Small C# habits used in the tutorials
- You print with
Console.WriteLine(...), which writes its argument and then starts a new line. varlets the compiler work out a local variable's type from its value. Withvar message = "Hello World";,messageis astring. The type is still fixed at compile time; you just don't write it out.- Methods are written in PascalCase, as above.
public void SayHello() {
var message = "Hello World"; // type inferred as string
Console.WriteLine(message);
}
The exercise, step by step
The editor starts with an empty MainFunction, the place where the program's work happens. The task has three parts.
- Define the method. Above
MainFunction, writepublic void SayHello()followed by a pair of braces. It'svoidbecause it prints a message instead of handing a value back. - Fill in the body. Between the braces, call
Console.WriteLine("Hello everybody!");. This line runs every time the method is called. - Call it. Inside
MainFunction, writeSayHello();. Without this line the program would print nothing, because defining a method never runs it.
Together:
public void SayHello() {
Console.WriteLine("Hello everybody!");
}
public void MainFunction() {
SayHello();
}
Running MainFunction prints Hello everybody! once. Adding a second SayHello(); line would print it twice.
Common mistakes
- Defining but never calling. The most common reason a new method seems to do nothing.
- Forgetting the parentheses on a call.
SayHello;doesn't compile. - Leaving out the braces. The body must be wrapped in
{ }. Without them the print line isn't part of the method. - Misspelling the name or its capitalization. C# is case-sensitive, so
sayhello()andSayHello()are different names, and calling the wrong one is a compile error.
The order in which methods are defined inside a class doesn't matter to the compiler: a method can call another one that appears further down. What matters is the order of the statements inside a body, which run from top to bottom.
Key takeaways
- A method is a named block of code: return type, name, parentheses, then a body in braces.
voidmeans the method returns nothing.- Defining a method stores it; calling it with
Name();runs it. - Every call runs the whole body, so one definition can serve many calls.