12 minlesson

Parameters and Arguments

Parameters and Arguments

Parameters allow you to pass data into methods, making them flexible and reusable.

Parameters vs Arguments

  • Parameters: Variables declared in the method definition
  • Arguments: Actual values passed when calling the method
csharp
1// 'name' is a parameter
2static void Greet(string name)
3{
4 Console.WriteLine($"Hello, {name}!");
5}
6
7// "Alice" is an argument
8Greet("Alice");
9Greet("Bob");

Single Parameter

csharp
1static void PrintDouble(int number)
2{
3 Console.WriteLine($"{number} doubled is {number * 2}");
4}
5
6PrintDouble(5); // Output: 5 doubled is 10
7PrintDouble(12); // Output: 12 doubled is 24

Multiple Parameters

Separate parameters with commas:

csharp
1static void PrintSum(int a, int b)
2{
3 int sum = a + b;
4 Console.WriteLine($"{a} + {b} = {sum}");
5}
6
7PrintSum(3, 5); // Output: 3 + 5 = 8
8PrintSum(10, 20); // Output: 10 + 20 = 30

Order Matters

csharp
1static void Divide(double numerator, double denominator)
2{
3 if (denominator != 0)
4 {
5 Console.WriteLine($"{numerator} / {denominator} = {numerator / denominator}");
6 }
7 else
8 {
9 Console.WriteLine("Cannot divide by zero!");
10 }
11}
12
13Divide(10, 2); // Output: 10 / 2 = 5
14Divide(2, 10); // Output: 2 / 10 = 0.2 (order changed!)

Different Parameter Types

csharp
1static void CreateUser(string username, int age, bool isAdmin)
2{
3 Console.WriteLine($"Creating user: {username}");
4 Console.WriteLine($"Age: {age}");
5 Console.WriteLine($"Admin: {isAdmin}");
6}
7
8CreateUser("alice", 25, false);
9CreateUser("admin", 30, true);

ref Parameters

The ref keyword passes a reference, allowing the method to modify the original variable:

csharp
1static void DoubleValue(ref int number)
2{
3 number = number * 2;
4}
5
6int value = 10;
7Console.WriteLine($"Before: {value}"); // Before: 10
8
9DoubleValue(ref value); // Must use 'ref' when calling too
10Console.WriteLine($"After: {value}"); // After: 20

ref vs Regular Parameters

csharp
1// Without ref - original is unchanged
2static void TryDouble(int number)
3{
4 number = number * 2; // Only changes local copy
5}
6
7int x = 5;
8TryDouble(x);
9Console.WriteLine(x); // Still 5
10
11// With ref - original is modified
12static void ActuallyDouble(ref int number)
13{
14 number = number * 2; // Changes original
15}
16
17int y = 5;
18ActuallyDouble(ref y);
19Console.WriteLine(y); // Now 10

out Parameters

The out keyword is for returning multiple values from a method:

csharp
1static void Divide(int dividend, int divisor, out int quotient, out int remainder)
2{
3 quotient = dividend / divisor;
4 remainder = dividend % divisor;
5}
6
7int q, r;
8Divide(17, 5, out q, out r);
9Console.WriteLine($"17 / 5 = {q} remainder {r}"); // 17 / 5 = 3 remainder 2
10
11// Modern syntax: declare inline
12Divide(23, 7, out int quotient, out int remainder);
13Console.WriteLine($"23 / 7 = {quotient} remainder {remainder}");

out Must Be Assigned

csharp
1static void GetMinMax(int[] numbers, out int min, out int max)
2{
3 min = numbers[0];
4 max = numbers[0];
5
6 foreach (int n in numbers)
7 {
8 if (n < min) min = n;
9 if (n > max) max = n;
10 }
11 // min and max MUST be assigned before method ends
12}

in Parameters (Read-Only Reference)

The in keyword passes by reference but prevents modification:

csharp
1static void PrintDetails(in LargeStruct data)
2{
3 Console.WriteLine(data.Value);
4 // data.Value = 10; // Error! Cannot modify 'in' parameter
5}

This is mainly used for large structs to avoid copying while ensuring immutability.

Practical Examples

Greeting with Parameters

csharp
1static void Greet(string name, string greeting)
2{
3 Console.WriteLine($"{greeting}, {name}!");
4}
5
6Greet("Alice", "Hello"); // Hello, Alice!
7Greet("Bob", "Good morning"); // Good morning, Bob!
8Greet("Charlie", "Hi"); // Hi, Charlie!

Drawing Shapes

csharp
1static void DrawRectangle(int width, int height, char symbol)
2{
3 for (int row = 0; row < height; row++)
4 {
5 for (int col = 0; col < width; col++)
6 {
7 Console.Write(symbol);
8 }
9 Console.WriteLine();
10 }
11}
12
13DrawRectangle(5, 3, '*');
14Console.WriteLine();
15DrawRectangle(10, 2, '#');

Calculating with Parameters

csharp
1static void CalculateCircle(double radius)
2{
3 double area = Math.PI * radius * radius;
4 double circumference = 2 * Math.PI * radius;
5
6 Console.WriteLine($"Radius: {radius}");
7 Console.WriteLine($"Area: {area:F2}");
8 Console.WriteLine($"Circumference: {circumference:F2}");
9}
10
11CalculateCircle(5);
12CalculateCircle(10);

Validation with ref

csharp
1static void ClampValue(ref int value, int min, int max)
2{
3 if (value < min)
4 value = min;
5 else if (value > max)
6 value = max;
7}
8
9int score = 150;
10ClampValue(ref score, 0, 100);
11Console.WriteLine(score); // 100
12
13int temp = -20;
14ClampValue(ref temp, 0, 50);
15Console.WriteLine(temp); // 0

Swap Values

csharp
1static void Swap(ref int a, ref int b)
2{
3 int temp = a;
4 a = b;
5 b = temp;
6}
7
8int x = 10, y = 20;
9Console.WriteLine($"Before: x={x}, y={y}"); // x=10, y=20
10Swap(ref x, ref y);
11Console.WriteLine($"After: x={x}, y={y}"); // x=20, y=10

Summary

In this lesson, you learned:

  • Parameters are variables in the method definition
  • Arguments are values passed when calling the method
  • Multiple parameters are separated by commas
  • ref allows methods to modify the original variable
  • out is for returning multiple values
  • in passes by reference but is read-only

Next, we'll learn about return values.