Dan Kenny Game Design
  • Home
  • Portfolio
    • Game Design
    • 2D & Animation
  • Blog
  • About Me
  • Contact
  • Home
  • Portfolio
    • Game Design
    • 2D & Animation
  • Blog
  • About Me
  • Contact

C# Beginners Tutorial: Methods

30/3/2018

Comments

 
Hey there, Gamers and Game Makers!

In part two of this introduction to C#, we're going to take a look at methods. What exactly is a method? A method is code you write that performs a task. We write this code in a way that we can re-use it through out our programming. The idea behind a method is that we can call the method easily and it performs a task without us having to re-write similar code over and over again. Ok, let's get into it!

Picture
I'll be using the same script from the last tutorial on the introduction of variables. If you missed that one,I suggest going back and reading over it before moving on to this.
Picture
Ok, to start off, let's do something very simple. We will create a method that will print a string of text to the console when called. To create a method we first need to declare the method type. For this example lets leave it as a void method as we aren't returning any value. I'm going to call this method "helloWorld" We write it as follows:

void helloWorld() {
}


Notice that the brackets directly after the method name are empty? Remember that a void method does not return anything, so we don't need to provide it with any parameters.
Picture
So, like before in our main body of code, we can do things like print strings. The only difference here is that the string will not be printed unless the method is called. For this simple example I've added:

print ("Hello world! Lovely day isn't it?");
Picture
Now that we have our method, it's time to see how we call it from the main body of our code. As you can see, I've commented out the line from our previous tutorial and added a line that will call my helloWorld method. To call the method, we simply write:

helloWorld();
​
Now, let's try running our script in the unity scene.
Picture
As you can see, the console displays our string. What's happening here is in the main body of code, called our method which then runs the code that is in it. In this case that's a print function that displays the text we now see. This is a very basic example and seems rather pointless in practice. We can make more complex methods that make use of our variables and parameters. Lets do another one!
Picture
Ok, we're going to look at how we can add numbers using a method. For the purpose of this I've declared three new variables. firstNumber, secondNumber and total. All of which are ints. I've initialized firstNumber to 10, secondNumber to 15 and given total an initialized value of 0.
Picture
Now for the method that will actually do the addition for us. I've declared this method as a type int because we'll be returning an integer value from the method. I've called the method addnumbers. Now, because we are going to be passing variable values from our main to this method, that means the method needs to be able to take parameters. So, inside the brackets next to the method name I'm declaring two int variables. These basically act as the placeholders for the variables that will be passed to them.

Now for the calculation part of the method. Since we're going to be adding two numbers, we'll need a variable to store the result of the addition. So, I've simply declared an int variable called totalSum and given it a value of 0. Next we'll do the actual addition by saying totalSum is equaled to num1 + num2. This takes care of the addition but we still need to return the answer so that we can see the result. To do this I simply say, return totalSum; Now the result will be passed back to the main.
Picture
Now, how do we actually make use of this method? Back in the main we call the method in a similar way that we did with the helloWorld method. Except this time we'll be making use of the variables we've declared. If I want the method to calculate the total sum of firstNumber and secondNumber, I have to first say my variable total = addNumber (firstNumber, secondNumber)

As you can see, this time I have to provide parameters to the method. Which in this case is firstnumber and secondNumber. This values will be passed to the method which will add them together and return the value which will be stored in the variable total. To then display the value of the variable total, we simply use a print function but provide the variable total as its argument.
Picture
Picture
As you can see, the value of our result which was calculated by our method is now displayed in the console window. That just about does it for this introduction into methods. You can create all kinds of methods to do just about anything and methods are very important as we can re-use them again and again to save time and make our programs more efficient.

Until next time!

Comments

    Archives

    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    July 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    June 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    August 2014
    November 2013
    September 2013

    Categories

    All

    RSS Feed

© COPYRIGHT 2020. ALL RIGHTS RESERVED.