Argument : when/why to use



I have googled to no end and cannot seem to find the answer to this.

I understand that the argument is a value passed to a method at the time of the call. But I am very unclear on what determines if you need an argument in your method?

I am new to ruby and if you could explain in plain language that would really help!

Thanks :slightly_smiling:


A method can only use variables passed by argument or created inside the method. If you want to use in your method an information defined outside the method, you have to pass it by argument.

This doesn't work because the variable "name" only exist outside the method :

name = "John"

def hello
    print "Hello " + name


This works because when you pass the variable "name" in argument to the method, it will store the content of "name" in the local variable "the_name" :

name = "John"

def hello(the_name)
    print "Hello " + the_name