Programming-Basics-Book-CSharp-EN

Exercises

In order to learn in practice what we have learned about methods we will solve a few problems, in which it will be required to write methods with certain functionality and after that to invoke them by passing them data, read from the console.

What We Learned in This Chapter?

Before starting, let’s review what we have learned about the methods in C#:

Defining a Method

This is how we define a method, which takes a parameter and returns a value:

static double CircleArea(double radius)
{
    return Math.PI * radius * radius;
}

Invoking a Method

This is how we invoke a method, pass a parameter value for the invocation and process the returned value:

Console.WriteLine("a = {0}, area = {1}", 5.33, CircleArea(5.33));
// a = 5.33, area = 89.2491915365671

Console.WriteLine("a = {0}, area = {1}", 9.999, CircleArea(9.999));
// a = 9.999, area = 314.0964366475

The Exercises

We will work on the following exercises: