Flashcards in Ruby Methods Deck (6):
a procedure of code. define it with def. End it with end.
# method body goes here
Parameters are used when you have data outside of a method's scope, but you need access to it within the method's scope. If the method does not need access to any outside data, you do not need to define any parameters. Below "(words)" is the parameter.
say("how are you")
Arguments are pieces of information that are sent to a method to be modified or used to return a specific result. We "pass" arguments to a method when we call it. Here, we are using an argument to pass the word, or string of words, that we want to use in the say method. When we pass those words into the method, they're assigned to the variable words and we can use them how we please from within the method. Note that the words variable is scoped at the method level; that is, you cannot reference this variable outside of the say method.
When you're defining methods you may want to structure your method so that it always works, whether given parameters or not.
puts words + '.'
Ruby methods ALWAYS return the evaluated result of the last line of the expression unless an explicit return comes before it.