Time for action – returning a value from AddTwoNumbers()

Modify LearningScript to call the AddTwoNumbers() method twice and get a grand total. Also create another method whose sole purpose is to display the grand total result.

  1. Open LearningScript in MonoDevelop to modify it.
  2. On line 12, declare the answer variable (this statement is on 3 lines).
  3. On lines 19 to 23, redefine the AddTwoNumbers() method with a return type.
  4. On lines 25 to 28, define the DisplayResult() method.
  5. Save the file.
  6. Click on Play in Unity.
Time for action – returning a value from AddTwoNumbers()

What just happened?

As you can see in the following screenshot, the result is 14. However, the main concept to learn from this example is this:

  • When a method returns a value, it's a type of data just like a variable would store
  • In fact, the value returned from a method could easily be stored in a variable
What just happened?

Analysis of the code is as follows:

  • The code on line 10 and its description is as follows:
    void Start()

    Unity calls the Start() method once only.

  • The code on lines 12 to 14 and its description is as follows: (Note: I have put this single statement on three lines for a better screenshot.)
    int answer = 
    AddTwoNumbers(number1, number2) + 
    AddTwoNumbers(number1, number3);

    All this line does is add two numbers and store the result in a variable named "answer".

    First there is a call to AddTwoNumbers(number1, number2) on line 19.

    The arguments number1 and number2 send the integers 2 and 3 to the method parameters on line 19.

  • The code on line 19 and its description is as follows:
    int AddTwoNumbers(int firstNumber, int secondNumber);

    The integers 2 and 3 are assigned to the parameter variables firstNumber and secondNumber.

  • The code on line 21 and its description is as follows:
    int result = firstNumber + secondNumber;

    The numbers 2 and 3 are added and stored in the declared variable result.

  • The code on line 22 and its description is as follows:
    return result;

    The integer 5, stored in the variable result, is returned back to line 12, where the method was called.

  • Back to the code on line 12 with its description:

    Where you see AddTwoNumbers(number1, number2), now sits the integer 5. The substitution has taken place.

    Now, line 12 continues its execution with another call to: AddTwoNumbers(number1, number3) on line 19

    The only difference is that the arguments have changed.

    The arguments number1 and number3 send the integers 2 and 7 to the method parameters on line 19.

  • Back to the code on line 19 again with its explanation:

    The integers 2 and 7 are assigned to the parameter variables firstNumber and secondNumber.

  • The code on line 21 and its description:

    2 and 7 are added and stored in result.

  • The code on line 22 with its description:

    The integer 9, stored in result, is returned back to line 12, where the method was called.

  • Back to the code on line 12 again with its description:

    Where you see AddTwoNumbers(number1, number3), now sits the integer 9. The substitution has taken place.

    Now line 12 continues its execution. There is a plus sign between the two method calls which means 5 and 9 are added together and the resultant integer 14 is now stored in the variable answer.

    The Start() method code block now continues execution on line 16.

  • The code on line 16 and its description is as follows:
    DisplayResult(answer);

    This is calling the DisplayResult() method on line 25.

    It takes one argument. The argument used is the variable answer which stores a value of type int.

    The argument answer sends the integer 14 to the method parameter on line 25.

  • The code on line 25 with its descripton:
    void DisplayResult(int total)

    The integer 14 is assigned to the parameter variable total.

  • The code on line 27 and its description:
    Debug.Log("The grand total is: " + total);

    This output to the Unity Console includes a little peek into the next chapter.

    Some text is displayed as well as the value stored in the variable total.

    The Unity Console displays The grand total is: 14.

    The Start() method is done executing its code. Since there is no further code in LearningScript to execute, the script is done.

Have a go hero – add two more numbers together

Try modifying line 12 to add the numbers together that are stored in the variables number2 and number3. You will have to include an additional call to AddTwoNumbers(). The result in the Console should be The grand total is: 24.

Calling a method is a logic detour

As you can see by following the code analysis, code is executed one step at a time. However, calling a method does send code execution on a detour. The method is then executed one line at a time until the end of the method is reached. If the method return type is void, then execution restarts from the point where the method was called. If the method returns a value, then the value returned is substituted at the place the method was called, then execution restarts from the point of substitution.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.226.240