Programming basics: Methods

Welcome reader,

We’ve gone through a lot of the core fundamentals of programming with C#. We’ve seen how to evaluate conditions in our program flow and different ways to repeat a block of code with the help of loops. We’ve seen different notions that can help us to really understand how a software program works with the basics.

At first, when we create a console application, we dwell in the fact that the software application has X lines of code. Sure, having a lot of code seems fun, we look like we know what we’re doing. As fun as it may look, a lot of code is really hard to maintain. While we may think that as soon we’re doing writing a software that it’s done, we may have to come back in a few days or months to improve its functionality.

But that’s not the topic of this post. What we’ll learn today is to create methods. Using methods can reduce the code we need to have in the main method, which was seen in a previous example. A software program runs from the main method which will do exactly what our program needs to do. Sounds weird huh ? Code can exist outside the main method, but to be used, the code must be called in the main method.

Basically, a method helps us to not have to repeat code, and that’s something we should always try to achieve while we are developing something. Why would we want to do that ? Because if a piece of code has a bunch of errors, if we duplicate it in several places, we’ll have to look for all those pieces and correct what was wrong. That is not a good practice, we should aim to not do that.

Methods can be simple, we could only need something which would take the name of a person as an input and print a welcome message. Like this


static void PrintWelcomeMessage(String personName)

{

    Console.WriteLine("Welcome in this software application, {0}", personName);

}

With methods, the naming convention changes. The first letter is upper-case. Moreover, the first word of the method has to be a verb, it shows exactly what the method is trying to achieve. In the previous example, the method is “PrintWelcomeMessage”. It’s easy to understand that the method is printing a welcome message and takes a parameter, a string data type. For now, we’ll won’t focus on the static keyword.

More than just the naming convention, methods, as it was mentioned earlier, can take, what we call, parameters. Sometimes, they don’t. Parameters are values that are placed inside the parenthesis. We have to define the type of the parameter and give a name to the variable, like we always have done before.


static void PrintWelcomeMessage(String personName, int ageOfPerson)

{

    Console.WriteLine("Welcome in this software application, {0}", personName);
    if(ageOfPerson>18)
    {
       Console.WriteLine("You are a minor");
    }else{
         Console.WriteLine("You are not a minor");
    }

}

We haven’t discussed another keyword that has appeared in this post, “void”. Void means that the method won’t return any value after it has reach its end. When we’re using methods, we can decide if we want a method to return something, like the result of a computation. We’ll try out a class(classes will be discussed at length in a later post) which is given by the .NET framework, Math. Thanks to this previous class, we can use different math skills which we could have learned before such as square root, sinus or power.


static double[]  ComputeDifferentValues(int a, double b, double c)

{

     double[] myMathComputedArray =  new double[5];

     for(int i=0;i<5;i++){

        switch(i){

          case 0:

                myMathComputedArray[i]= Math.sqrt(a+b+c);  //This will compute the square root of(a+b+c)

                break;

          case 1:

                myMathComputedArray[i]=Math.pow(a,b); //This will compute a power b

                break;

          case 2:

                myMathComputedArray[i]=Math.Abs(a-b-c);//returns the absolute value of a double floating-point number

                break;

          case 3:

                 myMathComputedArray[i]=Math.Ceiling(b);//returns the smallest int (integer) value that is greater than or equal to the double value that was passed as a parameter

                break;

          case 4 :

                myMathComputedArray[i]=Math.Sin(a); //returns the sine of the given angle

                break;

        }

     return myMathComputedArray;

}

As we could have seen in the last code sample, when we perform any kind of operation and we want to have it later, we have to return what we which to have. In this case, we used a switch to store different values in an array. When we were done, we used the return keyword, which return the variable we which to return we need it.

We have covered basics to some point. There is a lot we need to know before we can get better. But now, with what we know at the moment, a lot can be achieved.  We’ll see how to create console applications in order to resolve issues with all we’ve seen so far in later post such as creating a menu for a user who wishes to withdraw money from his bank account or how to manage an inventory.

Published by

Leave a comment