Returning a value from a method

Now it's time to discover the "power" feature of using a method. This usually means sending data to the method, which you just learned to do, then have the method return a value back. Previously, all you had the AddTwoNumbers() method do was take the result of adding two numbers and display it to Unity's Console.

Now, instead of displaying the result directly, you're going to modify AddTwoNumbers() to return the result of the addition back to the place the method was called.

Note

Remember, I told you that when you call a method, it's just a substitute for the code block that will be executed. It's like taking all the code in the method's code block and placing it right there where the method was called.

The next screenshot ...

Get Learning C# by Developing Games with Unity 3D Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.