Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
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
We still aren't able to specify how many
seconds our wait method should pause for.
0:00
But learning about variables
was an important step.
0:04
Now let's learn about the last piece
of the puzzle, method parameters.
0:07
A parameter is a special variable that
you declare at the start of a method.
0:12
Let's start a new file, so that we can
experiment with method parameters.
0:16
So we'll name it parameters.rb.
0:23
A parameter is a special variable that
you declare at the start of our method.
0:25
So let's define a new method named
add that takes two parameters.
0:30
Parameters are included within
parentheses following the method name.
0:35
So we're going to define our ad method
to take one parameter named first.
0:39
And this is basically just
an ordinary variable, so
0:44
it follows all the same naming
conventions as variables.
0:47
Then we'll add a second parameter that
will hold the number that should be added
0:51
to the first number.
0:54
We separate parameters with commas.
0:55
So I'll type a comma and a space and
0:57
then my second parameter which
I'll choose to name second.
0:59
Method definitions end
with the keyword end.
1:03
So I'll go ahead and type that first.
1:06
And now that we've declared
these parameter variables,
1:09
we can use them within the method body.
1:11
So let's add a couple snippets of
code that use these parameters.
1:13
First I'll make a call to put S and
pass it the first and
1:17
second parameters just to print out
what we received as parameters.
1:22
And then I'll call puts again.
1:27
And I'll pass it the result of adding
the value in the first parameter to
1:28
the value in the second parameter.
1:32
We'll be talking more about math
operations like this one in an upcoming
1:35
stage.
1:39
Now, let's define a second
method named subtract.
1:40
And this is also going to
take a couple parameters.
1:46
We'll use the same names as we did before.
1:49
Just by choice we could change
them to different names, but
1:52
it's okay if we repeat them, too.
1:56
So we'll name them first and second.
1:58
Method definition should
end with the keyword end.
2:03
And as before, we'll call puts and
pass it the first and second parameters.
2:06
And we'll also call puts again,
2:12
with the result of subtracting the second
parameter from the first parameter.
2:14
When you've define a method
to take parameters,
2:20
you need to provide argument
values when calling that method.
2:23
Ruby sets each parameter value
with the value in the argument.
2:27
So let's try making
a call to our add method.
2:31
And we need to provide arguments to that
method here in parenthesis following
2:35
the method name.
2:39
Just like before, we need to
separate our arguments with comas.
2:40
I'm going to call the add the method and
I'll set the first parameter,
2:43
the parameter named first up here,
to the value 100.
2:49
And I'll set the second parameter, the
parameter named second to the value 50.
2:53
Ruby will take the first
argument to the method call and
3:00
set the first parameter to that value.
3:03
It'll take the second argument
to the method call and
3:06
set the second parameter to that value.
3:09
Let's save this and try running it.
3:13
So we'll say ruby space parameters.rb.
3:16
Our code calls the add method
with arguments of 100 and 50.
3:24
The first parameter get set to 100.
3:28
The second parameter get set to 50.
3:31
And first within our method body,
3:35
it calls the puts method with the first
and second parameters as arguments.
3:37
It prints out our 2 arguments, 100 and 50.
3:42
Then this line of code calls the puts
3:45
method with the result of adding
the first and second parameters.
3:48
So 100 gets added to 50 with the result
150 and that is what gets printed out.
3:52
Now, let's try adding a call
to our subtracts method.
3:58
We a typed a method names subtract
parenthesis for our arguments.
4:01
And we'll set our first argument to 75 and
we'll set our second argument to 25.
4:06
Try running that.
4:13
And here you can see the results
of the add call him down
4:18
here you can see the results
of the subtract call.
4:21
It prints out its 2 arguments,
first and second 75 and 25.
4:25
And then it prints the result of
subtracting 25 from 75 which is 50.
4:29
And we can pass different arguments
each time we call a method.
4:35
So let's try calling
the add method again and
4:39
this time we'll use arguments of 3 and 4.
4:41
We'll also make another call to subtract.
4:44
And we'll give it arguments of 10 and 5.
4:47
Save that, run it.
4:52
And here you can see the results of
our second call to the add method.
4:55
It prints out its arguments of 3 and 4,
4:58
plus the results of
adding them which is 7.
5:01
And here you can see our
second call to subtract.
5:05
It prints out the arguments of 10 and
5:07
5 and the result\ of
subtracting them which is 5.
5:09
And again, as with all other method calls,
parentheses are optional.
5:13
If we wanted, we could remove
the parentheses up here, and
5:17
the code will work exactly the same way.
5:20
So I'll replace the opening
parenthesis with spaces.
5:22
I'll delete the closing parentheses,
save this, and try re-running it.
5:25
And you can see that the code
works just the same as before.
5:31
Now that we understand Ruby parameters and
arguments a bit better,
5:34
we're ready to update our wait method to
take a variable number of seconds to wait.
5:38
So let's close out of our parameters.rv
file and go back to our temp.rv file.
5:43
We're going to go up to
the definition of the wait method.
5:48
And we're going to type in
a parameter that it should accept.
5:52
We'll name it seconds, as in the number
of seconds that it should wait for.
5:56
That should go here in parentheses
following the method name.
6:01
And that parameter becomes a variable
that can be used within our method body.
6:04
So in place of always sleeping for
three seconds down here,
6:08
we're going to sleep for whatever
value the seconds variable contains.
6:12
With that parameter added
to the method definition,
6:17
we can now go down here to the call to
wait and add an argument to that method.
6:19
We'll go ahead and
skip the parentheses this time.
6:25
And for my first call to wait,
I'm going to wait just 1 second.
6:27
I'l add another call to wait
where I wait for 3 seconds.
6:30
Let's go down here to the console and
try running it.
6:34
Ruby space temp.rb.
6:38
And you see that it says
waiting with a brief pause and
6:41
then waiting again with much longer pause.
6:46
And those pauses are 1 and
3 seconds specifically because that's how
6:50
long we specified in our
arguments to the wait method.
6:55
You need to sign up for Treehouse in order to download course files.
Sign up