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 we have the method return a value. Previously, we used a void type method. I have mentioned before that this is a keyword for nothing, which means that the function isn't returning anything.

Let's learn about return type functions now. We won't use void anymore. Instead of that, we will write the type of data that we want our method to return. Don't worry if this sounds complicated; it isn't. I remember that, years ago, I had some issues getting my head around it. In practice, this is a very simple concept.

Let's take a look at the following example. I have highlighted two key areas that we will speak about next.

Returning a value from a method

As you can see, this method is very similar to the AddAndPrintTwoNumbers method that we spoke of previously. The two main differences are highlighted.

A return type function will always begin with a description of the type of data that it's returning. In this case, we will be returning the sum of two numbers, so our type is int (an integer). In simple words, the AddTwoNumbers function is returning a number.

Returning the value

Once you have decided what type of data will be returned by a method, you must tell the function what value will be returned. The syntax is very straightforward. We use the return keyword, as highlighted in blue, followed by the value we are returning.

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 LearningReusableMethodsWithReturn:

Example

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 method that simply prints the given int value on the Unity console. Now is the most important part you need to remember. Take a look at line 14:

int sumResult = AddTwoNumbers(number1, number2); 

The left-hand side of this line is a simple declaration of an int variable called sumResult. Simple! What I want to talk about is the right-hand side—the assignment of this variable. As you can see, what we are doing here is calling the AddTwoNumbers method instead of simply giving the value to be stored in sumResult. It might look a bit awkward. You would expect a value to be passed instead of another method call.

Let me explain how it works. The AddTwoNumbers method is a return type method. It does return an int value in every place where you call it—instantly. In even simpler words, AddTwoNumbers() is an integer, and a number value.

This concept might be a bit difficult to get your head around. If you still don't get it, don't worry. All you need to remember right now is the fact that, whenever a program calls a method that returns something, it is calling the method and inserting the value that the method returns into the place where it made the call.

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 of the code in the method's code block and placing it right where the method was called.

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

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