Writing functions
A fundamental principle of programming is Don’t Repeat Yourself (DRY).
While programming, if you find yourself writing the same statements over and over again, then turn those statements into a function. Functions are like tiny programs that complete one small task. For example, you might write a function to calculate sales tax and then reuse that function in many places in a financial application.
Like programs, functions usually have inputs and outputs. They are sometimes described as black boxes, where you feed some raw materials in one end, and a manufactured item emerges at the other. Once created and thoroughly debugged and tested, you don’t need to think about how they work.
Understanding top-level programs and functions
In Chapter 1, Hello, C#! Welcome, .NET!, we learned that since .NET 6, the default project template for console apps uses the top-level program feature introduced with C# 9.
Once you start writing functions...