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
We can get our methods to accept arguments using method parameters.
- It would be nice to be able to specify how long our
Wait
method should pause for. - We could do that by passing it an argument. For example, we could pass it the number of milliseconds to wait for:
Wait(3000);
- But if we try to run that code, we get an error: "No overload for method 'Wait' takes 1 arguments".
- In other words, a call to
Wait
can't accept an argument... yet. - We can get it to accept one using method parameters.
- Here's a program that declares an
Add
method that accepts two parameters, and aSubtract
method that also accepts two parameters. - A parameter is a special variable that you declare at the start of a method.
- It's just like declaring a variable; you specify a type and a name for the parameter.
- You can specify multiple parameters by separating them with commas.
- Within the method body, you can access the parameter just like you would a variable.
- In the
Add
method, we print the value of thefirst
parameter, and then print the value of thesecond
parameter. - Then we print the result of adding
first
andsecond
together.
- In the
- When a method takes parameters, you need to provide argument values when calling that method.
- C# sets each parameter variable with the value in the argument.
using System;
class Program
{
static void Add(double first, double second)
{
Console.WriteLine(first);
Console.WriteLine(second);
Console.WriteLine(first + second);
}
static void Subtract(double first, double second)
{
Console.WriteLine(first);
Console.WriteLine(second);
Console.WriteLine(first - second);
}
static void Main(string[] args)
{
Add(3, 5); // => 8
Add(10.5, 7.2); // => 17.7
Subtract(9, 3); // => 6
Subtract(21.3, 7.1); // => 14.2
}
}
- We can update
Wait
with a parameter, and pass arguments when we call it:
using System;
using System.Threading;
class Program
{
static void Wait(int delay)
{
Console.WriteLine("Waiting...");
Thread.Sleep(delay);
Console.WriteLine("Done");
}
static void Main(string[] args)
{
Wait(1000);
Wait(3000);
}
}
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
Here is our Wait method from earlier.
0:00
It would be nice to be able to
specify how long it should pause for.
0:02
We could do that by
passing in an argument.
0:07
For example, we can pass at the number
of milliseconds to wait for.
0:09
3,000 milliseconds will cause
it to wait for 3 seconds.
0:14
But if we save this code and
try to run it,
0:18
We get a compiler error, no overload for
method Wait takes 1 arguments.
0:24
In other words,
a call to wait can accept an argument.
0:29
We can give it to accept one
using method parameters.
0:32
Here's a program that declares an Add
method that accepts two parameters and
0:36
then another subtract method that
also accepts two parameters.
0:40
A parameter is a special variable that
you declare at the start of a method.
0:44
It's just like declaring a variable.
0:48
You specify a type and
a name for the parameter.
0:49
You can specify multiple parameters
by separating them with commas.
0:53
Within the method body, you can access the
parameter just like you would a variable.
0:57
In the Add method, we print the value
with the first parameter and
1:01
then print the value of
the second parameter.
1:05
Then we print the result of adding
the first and second parameter together.
1:07
When a method takes parameters,
1:12
you need to provide argument
values when calling that method.
1:13
C# sets each parameter value
with the value in the argument.
1:16
So this first argument to Add is going
to be the value that the first parameter
1:23
gets set to.
1:28
The second argument in the method
call is going to be the value that
1:31
the second parameter gets set to.
1:36
Let's save this and
try running it, with dotnet run.
1:38
And you can see that it prints the value
of the first parameter, then the value
1:45
of the second parameter, and
then the value of adding the two together.
1:49
Let's try adding another column
with different arguments.
1:58
So I'll call Add and
pass the first argument of 10.5 and
2:01
the second argument of 7.2,
let's try saving that.
2:04
And running it.
2:10
And there we are, and
our first parameter is set to 10.5.
2:14
Our second parameter to 7.2 and here's
the result of adding the two together.
2:18
Let's try calling this subtract
method with a first argument of 9 and
2:26
the second argument of 3.
2:30
Let's save this and try running it.
2:32
And here we are at the bottom, our first
parameter is set to 9, our second to 3,
2:39
and here's the result of
subtracting 3 from 9.
2:44
One last call, let's call Subtract
again with different arguments.
2:47
21.3 and 7.1 save this.
2:57
Run it?
3:02
And there's the results of calling
Subtract with different arguments.
3:04
Let's take what we've learned and
fix our Wait method.
3:09
We want to be able to pass an argument to
it with the number of milliseconds that it
3:12
should wait for.
3:16
But right now we're getting
an error saying no overload for
3:17
method Wait takes one arguments.
3:20
That's because we haven't set any
parameters up in the definition for
3:22
the Wait method.
3:25
Let's go ahead and add one now and
see if that fixes it.
3:26
So I'll add an int parameter because
this is going to accept an integer and
3:29
we'll name it delay.
3:35
I don't need to make any other changes to
the method except to take this hard coded
3:38
3000 millisecond value and replace it
with the name of the delay parameter.
3:42
Let me save this and we'll try running it.
3:47
You can see it says waiting, it waits
three seconds and then it prints done.
3:53
Let's try adding a call to wait with a
different argument and see if it waits for
4:01
a different period of time.
4:04
We'll say wait(1,000) milliseconds,
so 1 second.
4:06
Save that.
4:09
Try running it.
4:11
And first, it waits for
one second, and prints done.
4:16
Then it waits for
three seconds and prints done.
4:18
So by adding a delay parameter to the
weight method definition, we were able to
4:21
pass arguments to our calls to wait,
saying how long it should wait for.
4:25
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