Sometimes we don’t just want a method to print something to the console, but we actually want that method to hand us (or another method!) back a value. For that, we use
def double(n) return n * 2 end output = double(6) output += 2 puts output
- In the example above, we define a new method called
doublethat accepts one parameter called
- Inside the method, we return two times
- After that, we call our new
doublemethod with an argument of
6and store the result,
- Then, we increase
outputto 14 and print it out to the console.
Define a new method called
add that takes two numbers as parameters.
Inside the method,
return the sum of those numbers.