Methods or Function in ruby allows us to put our code in a container and call it when necessary. This allows reusability and prevent duplication ,take for instance we want multiply 3 different pairs of number 9 time we would do this
puts "#{3 * 4}"
puts "#{1 * 0}"
puts "#{8 * 2}"
puts "#{7 * 6}"
.....
This would be a very long and repetitive process but with methods we can do this.
def multiply(a,b)
a*b
end
multiply(3,4)
multiply(1,0)
This is easier to maintain and be used by anyone
Top comments (0)