C#, lesson 6
As a program grows, one long list of statements becomes hard to follow. A method packages a piece of work under a name so you can call it whenever you need it, and test or fix it in one place.
With top-level statements you can declare a method right in the file, alongside your other code. C# calls these local functions. You can call them before or after the place where they are written:
A method declaration has a return type (int, or void for "returns nothing"), a name in
PascalCase that usually starts with a verb, a parameter list with a type for each
parameter, and a body. The static keyword on a local function promises that it only uses
its parameters and not the variables around it, which keeps it self-contained. It is a good
default.
When a method is a single expression, you can use the short form:
2 exercises to try what you just read.
return hands a value back and ends the method immediately. Every path through a non-void
method must return a value, or the code will not compile:
Methods that answer a yes/no question return bool and read naturally in conditions:
if (IsEven(n)) ....
By default C# passes arguments by value: the method receives a copy, so assigning to a
parameter does not affect the caller's variable. The out and ref keywords change that. You
will meet out in the lesson on errors: int.TryParse(text, out int value) uses it to hand back
the parsed number alongside a bool that says whether parsing worked.
Parameters can also have default values, and callers can name the arguments they pass:
A variable lives only inside the block { } where it is declared. A static local function
cannot see the variables of the surrounding code at all; it talks to the rest of the program only
through parameters and its return value. That makes each method a small, predictable unit.
In larger programs, methods live inside classes, as in Math.Sqrt or Console.WriteLine. You
will write your own classes in a later lesson; for single-file programs, local functions are
the simplest tool.
Write a local function static bool IsPrime(int n) that returns true when n is prime (a whole
number greater than 1 whose only divisors are 1 and itself). Then read one number N and print
every prime from 2 to N on one line, separated by single spaces. If there are none, print
none.
Hint: you only need to try divisors d while d * d <= n.
Sample input:
Sample output:
Each input line describes a shape: circle r, rectangle w h or triangle b h (base and
height). For every line, print the area with two decimal places. Write one local function per
shape, CircleArea, RectangleArea and TriangleArea, each returning a double. Use
Math.PI.
Sample input:
Sample output:
using System;
Console.WriteLine(Square(4)); // 16
Console.WriteLine(Square(3) + Square(4)); // 25
static int Square(int x)
{
return x * x;
}static int Square(int x) => x * x;
static bool IsEven(int n) => n % 2 == 0;static string Sign(int n)
{
if (n > 0) return "positive";
if (n < 0) return "negative";
return "zero";
}static string Greet(string name, string greeting = "Hello") => $"{greeting}, {name}!";
Console.WriteLine(Greet("Ana")); // Hello, Ana!
Console.WriteLine(Greet("Ana", greeting: "Hi")); // Hi, Ana!202 3 5 7 11 13 17 19circle 2
rectangle 3 4.5
triangle 6 312.57
13.50
9.00