-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_functions.cs
More file actions
42 lines (35 loc) · 1002 Bytes
/
05_functions.cs
File metadata and controls
42 lines (35 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
class Program
{
// Helper methods (can be called from inside functions())
static int add(int a, int b)
{
return a + b;
}
static void greet(string name, int age = 25)
{
Console.WriteLine($"Hello {name}, you are {age} years old.");
}
static void functions()
{
Console.WriteLine("=== FUNCTIONS ===");
greet("Bob");
greet("Alice", 32);
Console.WriteLine($"Sum: {add(5, 7)}");
// Variable arguments (params)
static int sumAll(params int[] numbers)
{
int total = 0;
foreach (int n in numbers) total += n;
return total;
}
Console.WriteLine($"Sum of many numbers: {sumAll(1, 2, 3, 4, 5)}");
// Action / lambda example
Action<string> printMessage = msg => Console.WriteLine($"Message: {msg}");
printMessage("This is a lambda!");
}
static void Main(string[] args)
{
functions();
}
}