Transform messy code into clean, reusable functions that work like building blocks! (Ages 8-18)
CSharp-Lesson-Four/
├── .git/
├── .gitignore
├── CSharp-Lesson-Four.csproj
└── Program.cs
Each step below shows the complete contents of Program.cs after you finish that step. When a step says "replace your Program.cs with this", select all (Ctrl+A / Cmd+A) inside Program.cs, delete it, then paste the new code. That keeps you from ending up with two Main methods or leftover code from earlier steps.
- What to do: Go to https://github.com/PhillyCoderDojo/CSharp-Lesson-Four → Click "Fork" → Open GitHub Desktop → Clone your fork
- File(s) touched: Creating project folder
- Code snippet: None
- Why it matters: Get your own workspace for function practice
- Git command:
git clone https://github.com/YOUR-USERNAME/CSharp-Lesson-Four.git
- What to do: Open Rider → File → Open → Select your cloned folder → Click OK
- File(s) touched: Loading project
- Code snippet: None
- Why it matters: Time to organize code like a pro!
- Git command: None
- What to do: Replace your
Program.cswith the code below. It adds aSayHellofunction and calls it three times fromMain. - File(s) touched: Program.cs
- Code snippet (full file):
namespace CSharp_Lesson_Four;
class Program
{
static void SayHello()
{
Console.WriteLine("Hello from my function!");
}
static void Main(string[] args)
{
SayHello();
SayHello();
SayHello();
}
}- Why it matters: Functions let you reuse code instead of copying
- Git command:
git add . && git commit -m "Create first function without parameters"
- What to do: Replace your
Program.cswith the version below. The function now takes anameand anage. - File(s) touched: Program.cs
- Code snippet (full file):
namespace CSharp_Lesson_Four;
class Program
{
static void GreetPerson(string name, int age)
{
Console.WriteLine($"Hello {name}! You are {age} years old.");
}
static void Main(string[] args)
{
GreetPerson("Alex", 12);
GreetPerson("Jordan", 15);
GreetPerson("Casey", 10);
}
}- Why it matters: Parameters make functions flexible with different inputs
- Git command:
git add . && git commit -m "Add parameters to function"
- What to do: Replace your
Program.cswith the version below. Now the functions calculate something and return the result. - File(s) touched: Program.cs
- Code snippet (full file):
namespace CSharp_Lesson_Four;
class Program
{
static int AddNumbers(int a, int b)
{
return a + b;
}
static double CalculateCircleArea(double radius)
{
return 3.14159 * radius * radius;
}
static void Main(string[] args)
{
int sum = AddNumbers(5, 3);
Console.WriteLine($"5 + 3 = {sum}");
double area = CalculateCircleArea(4.0);
Console.WriteLine($"Circle area: {area}");
}
}- Why it matters: Return values let functions send results back
- Git command:
git add . && git commit -m "Add functions with return values"
- What to do: Replace your
Program.cswith the calculator version below. Each math operation is its own function. - File(s) touched: Program.cs
- Code snippet (full file):
namespace CSharp_Lesson_Four;
class Program
{
static double Add(double x, double y) => x + y;
static double Subtract(double x, double y) => x - y;
static double Multiply(double x, double y) => x * y;
static double Divide(double x, double y) => x / y;
static void DisplayMenu()
{
Console.WriteLine("=== Calculator ===");
Console.WriteLine("1. Add 2. Subtract 3. Multiply 4. Divide");
}
static double GetNumber(string prompt)
{
Console.Write(prompt);
return double.Parse(Console.ReadLine());
}
static void Main(string[] args)
{
DisplayMenu();
Console.Write("Choose operation (1-4): ");
int choice = int.Parse(Console.ReadLine());
double num1 = GetNumber("First number: ");
double num2 = GetNumber("Second number: ");
double result = choice switch
{
1 => Add(num1, num2),
2 => Subtract(num1, num2),
3 => Multiply(num1, num2),
4 => Divide(num1, num2),
_ => 0
};
Console.WriteLine($"Result: {result}");
}
}- Why it matters: Each function has one clear job
- Git command:
git add . && git commit -m "Create organized calculator with functions"
- What to do: Replace your
Program.cswith the version below. The newGetValidNumberfunction keeps asking until the user types a real number, and the calculator now uses it instead ofGetNumber. - File(s) touched: Program.cs
- Code snippet (full file):
namespace CSharp_Lesson_Four;
class Program
{
static double Add(double x, double y) => x + y;
static double Subtract(double x, double y) => x - y;
static double Multiply(double x, double y) => x * y;
static double Divide(double x, double y) => x / y;
static void DisplayMenu()
{
Console.WriteLine("=== Calculator ===");
Console.WriteLine("1. Add 2. Subtract 3. Multiply 4. Divide");
}
static double GetValidNumber(string prompt)
{
while (true)
{
Console.Write(prompt);
if (double.TryParse(Console.ReadLine(), out double result))
{
return result;
}
Console.WriteLine("❌ Please enter a valid number!");
}
}
static void Main(string[] args)
{
DisplayMenu();
Console.Write("Choose operation (1-4): ");
int choice = int.Parse(Console.ReadLine());
double num1 = GetValidNumber("First number: ");
double num2 = GetValidNumber("Second number: ");
double result = choice switch
{
1 => Add(num1, num2),
2 => Subtract(num1, num2),
3 => Multiply(num1, num2),
4 => Divide(num1, num2),
_ => 0
};
Console.WriteLine($"Result: {result}");
}
}- Why it matters: Functions can handle errors gracefully
- Git command:
git add . && git commit -m "Add input validation function"
- What to do: In GitHub Desktop → Commit changes → Push origin
- File(s) touched: Uploading to GitHub
- Code snippet: None
- Why it matters: Share your well-organized functions
- Git command:
git push origin main
- Press Ctrl+F5 or click green
▶️ button in Rider - Expected output: Calculator menu, prompts for numbers, shows calculation results
- "Type 'Program' already defines a member called 'Main'" — You have two
Mainmethods. Make sure you replaced the whole file at each step instead of pasting on top of the old code. There should be exactly onestatic void Main(string[] args)inProgram.cs. - "The entry point of the program is global code; ignoring 'Main(string[])' entry point" — Same root cause: there's a stray line of code outside any method (often a leftover
Console.WriteLine("Hello, World!");from the starter). Delete anything outside theclass Program { ... }braces. - "Method must have a return type" — A function is missing
staticor its return type. Make sure every function starts withstaticand eithervoid,int,double, etc.
Create a "Math Quiz Game" using functions! Add the helpers below to your Program class and call AskMathQuestion() from Main.
Hint:
static int GenerateRandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max + 1);
}
static bool AskMathQuestion()
{
int a = GenerateRandomNumber(1, 10);
int b = GenerateRandomNumber(1, 10);
int correct = a + b;
Console.Write($"What is {a} + {b}? ");
int answer = int.Parse(Console.ReadLine());
return answer == correct;
}