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.
LearningScript
in MonoDevelop to modify it.AddTwoNumbers()
method with a return type.DisplayResult()
method.As you can see in the following screenshot, the result is 14. However, the main concept to learn from this example is this:
Analysis of the code is as follows:
void Start()
Unity calls the Start()
method once only.
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.
int AddTwoNumbers(int firstNumber, int secondNumber);
The integers 2 and 3 are assigned to the parameter variables firstNumber
and secondNumber.
int result = firstNumber + secondNumber;
The numbers 2 and 3 are added and stored in the declared variable result.
return result;
The integer 5, stored in the variable result, is returned back to line 12, where the method was called.
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.
The integers 2 and 7 are assigned to the parameter variables firstNumber
and secondNumber
.
2
and 7
are added and stored in result.
The integer 9, stored in result, is returned back to line 12, where the method was called.
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.
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.
void DisplayResult(int total)
The integer 14 is assigned to the parameter variable total.
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.
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.
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.
18.118.226.240