C# & .NET Framework Methods — Your Toolbox
💡
Exercise 22

Parameters & Return Values 15 XP Easy

Ctrl+Enter Run Ctrl+S Save

📏 Calculation Tools — Input In, Results Out!

A hammer that just swings in the air isn't useful. Real tools need materials (inputs) and produce results (outputs). In C#, method parameters are the materials you feed in, and return values are the finished products you get back.

Imagine you're the architect's chief calculator. Clients hand you measurements, and you hand back precise areas, costs, and approvals. That's the power of parameters and return values!

📥 Parameters — Feeding Your Tools

Parameters are variables declared in the method's parentheses. When you call the method, you pass arguments — actual values that fill those parameters.

// 'width' and 'height' are parameters static int CalculateArea(int width, int height) { return width * height; } // Calling with arguments: int area = CalculateArea(10, 5); // width=10, height=5 Console.WriteLine(area); // Output: 50

📤 Return Values — The Finished Product

The return keyword sends a value back to the caller. The method's return type (before the name) declares what type of value it returns. Once return executes, the method immediately exits — like handing over the finished product and stepping away from the workbench.

// Returns a double static double CircleArea(double radius) { return Math.PI * radius * radius; } // Returns a string static string GetGreeting(string name) { return $"Hello, {name}!"; } // Returns a bool static bool IsApproved(double cost) { return cost < 5000; }

🔢 Multiple Parameters — Complex Calculations

Methods can accept multiple parameters of different types. Each parameter is separated by a comma. Think of it as a tool that needs multiple inputs to do its job — like a cost estimator that needs quantity, unit price, and tax rate.

static double CalculateTotalCost(int quantity, double unitPrice) { return quantity * unitPrice; } static void Main() { double total = CalculateTotalCost(50, 25.00); Console.WriteLine($"Total cost: ${total:F2}"); // Total cost: $1250.00 }

💾 Storing Return Values

When a method returns a value, you can store it in a variable, use it directly in an expression, or pass it as an argument to another method. The returned value is just like any other value of that type.

// Store in a variable int area = CalculateArea(10, 5); // Use directly in Console.WriteLine Console.WriteLine(CalculateArea(10, 5)); // Use in a condition if (CalculateTotalCost(50, 25.00) < 2000) { Console.WriteLine("Within budget!"); } // Pass to another method bool approved = IsApproved(CalculateTotalCost(50, 25.00));

💡 Key Takeaways

  • Parameters are the method's inputs — declared in parentheses with their types.
  • Arguments are the actual values you pass when calling a method.
  • The return keyword sends a value back and exits the method immediately.
  • A method's return type must match the type of value returned.
  • Return values can be stored in variables, used in expressions, or passed to other methods.
  • Use Math.Round(value, digits) to round decimals for precision.
📋 Instructions
**The Architect's Calculation Suite** Build a set of calculation tools for the construction office: 1. Define `static int RectangleArea(int width, int height)` that returns `width * height`. 2. Define `static double CircleArea(double radius)` that returns `Math.Round(Math.PI * radius * radius, 2)`. 3. Define `static double TotalCost(int quantity, double unitPrice)` that returns `quantity * unitPrice`. 4. Define `static bool IsApproved(double cost)` that returns `true` if cost is less than or equal to `5000`. 5. In `Main`: - Print `Rectangle area: ` followed by the result of `RectangleArea(10, 5)` - Print `Circle area: ` followed by `CircleArea(5.0)` - Calculate the total cost with `TotalCost(50, 25.00)` and print `Total cost: $` followed by the value formatted to 2 decimal places - Use `IsApproved` on that total cost: if approved, print `Building is approved!`
For formatting, use `$"Total cost: ${cost:F2}"` to show 2 decimal places. `Math.Round(Math.PI * radius * radius, 2)` rounds to 2 decimal places. `IsApproved` should return `cost <= 5000`. Store the return value of `TotalCost` in a variable so you can reuse it.
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.