C# Tutorial, please help me understand this code...

Here’s the code from the book I’m learning from:

using UnityEngine;
using System.Collections;

public class MemoryTick : MonoBehaviour
{

    public int number1 = 2;
    public int number2 = 3;
    public int number3 = 7;

    // Use this for initialization
    void Start ()
    {
        int answer =
        AddTwoNumbers (number1, number2) +
        AddTwoNumbers (number1, number3) +
        AddTwoNumbers (number2, number3);

        DisplayResult (answer);
    }

    int AddTwoNumbers (int firstNumber, int secondNumber)
 
    {
        int result = firstNumber + secondNumber;
        return result;
    }

    void DisplayResult(int total)
    {
        Debug.Log ("The grand total is: " + total);
    }
}

I understand most of it, but I do have some questions:

The code shows the two numbers added to eachother, however in the last line of my start method I am calling the method DisplayResult(), why does it need the argument “answer” within it?
I tried without it, and the code won’t execute, has it to do with the “int total”?
if so, in what order is the code being read by unity?

Hope someone can explain it in detail to a total noob :stuck_out_tongue: thanks

I’ll try.

First, please use the CODE tags around your code in a forum post, which makes it much easier to read. Easiest way to do that is to use the “Insert Code” button, located in the edit bar between the movie clip and the disk icon. (I wonder if yoots today even recognize that 3.5" floppy disk?)

OK, so now for the code itself. You’ve created a MonoBehaviour here, which is a type of component that gets attached to a GameObject. When that GameObject is created, Unity will first call the Start method. (And then later it will call the Update method on every frame — but this particular MonoBehaviour doesn’t have one, which is fine, so just file this tidbit away for the future.)

So, as far as this code is concerned, that’s it. The computer will begin at the first line of the Start method, and execute each line therein, until it gets to the end of the Start method, and then it’s done.

Now, to understand what’s going on, just examine each line. Hmm, I should point out that a “line” in C# terms ends with a semicolon or curly brace. So your Start method has two lines.

The first line declares a local variable called “answer” and assigns it a value by adding up the result of three AddNumber calls. (So on each AddNumber call, the computer goes down into the AddNumber function, does what it says to do, and then comes back with a value.)

The second line calls the DisplayResult method, passing in the value of that “answer” local variable. So at this point the computer goes down into the DisplayResult method, and does what it says there, then comes back.

DisplayResult’s job is to print out a value, but what value should it print? It could print anything. The value it prints is whatever you pass in. That value needs a name so the code within DisplayResult can refer to it; that name is defined by the “int total” bit in the method declaration.

Note that this does not need to match the name of whatever’s passed in… in fact what’s passed in doesn’t even have to have a name. You could DisplayResult(42) or DisplayResult(6*7) or anything else.

Does this clear it up for you?

Sorry I was actually looking for a button like that, but read only “insert” and assumed it would be like paste :stuck_out_tongue:
(As an early 90’s kid I do remember the floppy, though vaguely, hah)

Yeah, I tried with your example of 42 and now I see how the value is assigned to the new variable total, makes sense now :slight_smile: thank you! I thought it would give me an error message if I didn’t use the same variable name as I used to assignt the values to answer, but all it said was that the variable was never used in the code, and just inputting 42 along with the message. Should probably just have played around with it some more, thanks!

Glad I could help! Feel free to leave a tip here if you are so moved. :slight_smile: