Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
A parameter is a special variable that you declare at the start of a method. When a method takes parameters, you need to provide argument values when calling that method. Ruby sets each parameter variable with the value in the argument.
- We still can't specify how many seconds our
wait
method should pause for. - But learning about variables was an important step. Now let's learn about the last piece of the puzzle: method parameters.
- A parameter is a special variable that you declare at the start of a method.
- This method takes two parameters, one named
first
and one namedsecond
:
- This method takes two parameters, one named
def add(first, second)
puts first, second
puts first + second
end
- When a method takes parameters, you need to provide argument values when calling that method.
- Ruby sets each parameter variable with the value in the argument.
def add(first, second)
puts first, second
puts first + second
end
def subtract(first, second)
puts first, second
puts first - second
end
add(100, 50) # 150
subtract(75, 25) # 50
add(3, 4) # 7
subtract(10, 5) # 5
- Here's our
wait
method from before, updated with a parameter for the number of seconds it should wait:
def wait(seconds)
puts "Waiting..."
sleep seconds
puts "Done"
end
wait 1
wait 3
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up