Example

You just learned how to write a return type method. Time to put it to use! Let's write a new script and call it returnTotal:

What do we have here? You probably understand most of this code with no issues, but it's good practice to go through it line by line. Lines 7 and 8 contain declarations of the number1 and number2 integer variables. Lines 22 to 27 are exactly the same as we used in the last example. They have the declaration of a method that takes two parameters -firstNumber and secondNumber and it returns a value of the int type.

Lines 30 to 34 contain the declaration of the method that simply prints the given int value on the ...

Get Learning C# 7 By Developing Games with Unity 2017 - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.