Locked lesson.
About this lesson
Once you've created a method, you may want to pass Parameters to those Methods to perform specific operations.
Exercise files
Download this lesson’s related exercise files.
28 - Passing Parameters To Methods.docx61.1 KB 28 - Passing Parameters To Methods SOLUTION.docx
58.8 KB
Quick reference
Passing Parameters To Methods
You can pass parameters to your methods.
When to use
Use this whenever you need to pass a parameter to a method.
Instructions
{
int a, b
a = 3;
b = 2;
Addthings(a,b);
}
static void Addthings(int x, int y)
{
int result;
result = x + y;
Console.WriteLine($"{x} + {y} = {result}");
}
Hints & tips
- Be sure to define the data type when you pass parameters to a method.
- 00:04 Okay in the last video we started talking about methods.
- 00:06 In this video I want to continue with that and
- 00:08 talk about how to pass parameters into your method.
- 00:11 So here in the last video we remember we just displayed this thing.
- 00:14 And that's great but we often want to pass things into our method,
- 00:17 have the method do things with it, and then take some action.
- 00:21 So how can we do that?
- 00:22 Well, let's come up here and let's change this around a little bit.
- 00:25 Let's get rid of this and maybe we want to get rid of all of this.
- 00:28 And let's come down here and let's create another method.
- 00:31 So let's go static void and that's called AddThings.
- 00:37 I don't know, all right, so here to call this,
- 00:40 we would call AddThings up here, but what do we want to add?
- 00:45 Well, we want to add a and b.
- 00:47 So here we can pass a and b.
- 00:52 So inside of these parentheses, we can pass whatever we want.
- 00:55 Now that's fine, but down here, we need to account for that.
- 00:59 If we know we're going to pass two things into our method,
- 01:01 we need to create our method to be on the lookout for two things, right?
- 01:05 So to do that, when we define it inside here, we put those things.
- 01:10 Now just like any other variable you have to define what it is.
- 01:15 And these are going to be integers.
- 01:16 So we can say int x and int y.
- 01:21 Now, you'll notice here I'm calling them a and b and here I'm calling them x and y.
- 01:27 And you can call them anything you want inside of here.
- 01:30 But the name you call them inside of these parentheses is the name that
- 01:35 you'll reference inside of these brackets.
- 01:38 So here if I want to create a result, so
- 01:43 int result, I could say a result = x + y, right?
- 01:50 And maybe here we want to say console writeline, now let's do that again.
- 01:57 And let's say x + y = (result),
- 02:03 and there's that.
- 02:09 So right here, we're defining those variables, passing them in,
- 02:14 assigning them to a new variable name, x and y.
- 02:17 We could just as easily have called it a and b, like we did earlier.
- 02:21 But I tend to find that becomes confusing because now we're talking about this a and
- 02:26 b or this a and b.
- 02:27 They're the same thing, but still, it's hard to keep track of these things.
- 02:31 So often I sort of recommend that you change the names of the things as you
- 02:35 pass them in your method.
- 02:37 So, okay, let's go ahead and save this and run it see what happens here, And
- 02:43 we get the same thing, 3 + 2 = 5, but it's all now happening inside of here.
- 02:49 And in fact if we take out this line right here comment it out and save this and
- 02:53 run it, nothing is going to happen at all.
- 02:57 All right, we get absolutely nothing there because like I said,
- 03:00 methods don't get run unless they're specifically called.
- 03:02 So very cool, now we're calling this method and it's doing this.
- 03:08 We could then call this method from inside of here, and
- 03:12 sort of pile these on top of each other.
- 03:14 So there we go.
- 03:17 If we save this and run it, we get the same thing we got in the last program,
- 03:21 3 + 2- 5, program ends.
- 03:23 But it's done in a very different way in this video.
- 03:26 So, again, these are very basic methods and it might not make sense to break our
- 03:31 program apart into different methods when our code is this simple.
- 03:35 But I'm just trying to show you the concepts of methods.
- 03:37 You'll use them for more complicated things in the future, but
- 03:40 first we need to wrap our brains around exactly what they are and how they work.
- 03:44 So, again, passing things in here, we use an a and a b variable.
- 03:50 You don't have to, we could pass a 4 and a 1, just like that.
- 03:55 Now it will take that 4 and that 1, assign them to x and y.
- 04:00 So x will become 4 and y will become 1, x + y,
- 04:04 4 + 1 should result in 5 if we save this and run it.
- 04:11 We get 4 + 1 = 5, and program ends, very cool.
- 04:15 So this is a very cool way to become very interactive with your program
- 04:19 because you can pass anything you want.
- 04:22 You could take user input, and then pass that user input into this method,
- 04:26 and then do stuff with it.
- 04:28 And then display things out on the screen, very cool.
- 04:31 So that's how to pass parameters into a method.
- 04:33 In the next video, I'll show you how to return stuff from one method back to our
- 04:37 main program, and that'll be in the next video.
Lesson notes are only available for subscribers.