Now let's look at how to create our own methods.
For this example we will create a method that finds the average of three numbers. First we will see the entire definition of this method, then we will go back and analyze it:
def average3(n1, n2, n3)
return (n1 + n2 + n3) / 3.0
end
The first line of a method definition begins with the word 'def', which means that a method is being defined. The next word is the method name, which in this case is 'average3'. If the method has no parameters, then this is all that is needed in the first line. However, in this case, we have three parameters, which we will call n1, n2, and n3. We put these in parenthesis after the method name, and separate them by commas if there are more than one.
You will notice that the next line is indented. This is because the word 'def' on the first line created a code block. A
code block in RGSS is a section of code and the logical structure that encases it. It begins with the structure declaration and ends with the word 'end'. The code inside the block is indented one tab in front of the code outside it.
In our example, the first line defines the start of a block and the third line defines the end of the block, with line 2 being the code contained in the block. While there are often multiple lines contained within a block, in this case only one is needed.
Now looking at the second line, we see the statement
return (n1 + n2 + n3) / 3.0
The 'return' word references a method that sets the parameters on that line to the return value of the method, and then exits the method. Due to this behavior, any code in the same code block as 'return' line that falls after the line that uses 'return' will not be processed, as the method will have been exited at an earlier point. For example, any lines inserted between lines 2 and 3 in the code will have no effect whatsoever.
Now let's look closer at the second part of the line. We notice that the parameters are being used in this line. This is because inside the code block of a method definition, the parameters act as variables. These variables have the same values as the parameters that are used when referencing the method, but are named differently.
Now onto the math. At first glance it might seem too complicated. To get the average of three numbers, all we do is add them up and divide by 3. You may make the mistake of writing it like this:
This is wrong for two reasons. First, we have to remember how multiple methods are processed. In this case:
n1 + n2 + n3 / 3
n1 + n2 + divide(n3 / 3)
n1 + add(n2, divide(n3 / 3))
add(n1, add(n2, divide(n3 / 3)))
Only n3 is being divided by 3, the other two numbers are simply being added. We can fix this by adding parenthesis:
(n1 + n2 + n3) / 3
(n1 + add(n2, n3)) / 3
add(n1, add(n2, n3)) / 3
divide(add(n1, add(n2, n3)), 3)
Great, now the whole thing is being divided by 3 just like we want. It may seem like we are finished, but there is a small point that must be solved. n1, n2, and n3 can be any numbers, either integers or decimals. If they all the parameters are integers, then division will produce an integer result. If we tried averaging the numbers 1, 2, and 4, we would expect 7 / 3 = 2.3333, when we would actually get 2. This is one of the rules of integer division:
when two integers don't divide evenly, the result will be rounded down (even if it is negative). For example, if we tried averaging -1, -2, and -4, we would get -3 instead of -2.3333.
To fix this, we simply change 3 to 3.0, making one of the parameters decimal. This will perform decimal division, which will produce the desired results of 2.333333 and -2.333333 for the sets of numbers above.
Now that we understand the method, we can try using it, by inserting the following script:
def average3(n1, n2, n3)
return (n1 + n2 + n3) / 3.0
end
p average3(1,2,4),average3(-1,-2,-4)
p average3(rand(10),rand(10),rand(10))
This code defines the average3 method, displays the results for the sets of numbers above, and then averages three random digits. There are a few things to keep in mind:
- A method definition can be placed anywhere in a script, at the beginning, at the end, in the middle, or in an entirely separate script from the one that is referencing it.
- A method definition will not run on its own, it must be referenced from code outside its definition code block