Locked lesson.
About this lesson
In this lesson, we introduce Methods and how to use them in your program.
Exercise files
Download this lesson’s related exercise files.
27 - Methods.docx61.1 KB 27 - Methods SOLUTION.docx
58.5 KB
Quick reference
Methods
Methods are programs within your program.
When to use
Use them to break apart parts of your code that you plan to use over and over again.
Instructions
{
int a, b, result;
a = 3;
b = 2;
result = a + b;
Console.WriteLine($"{a} + {b} = {result}");
DisplayMessage();
}
static void DisplayMessage()
{
Console.WriteLine("Program Ended");
}
Hints & tips
- Methods only run when they are called
- 00:04 Okay, and in this video I want to start to talk about methods.
- 00:06 And methods are an incredibly important part of programming, and
- 00:09 you're going to see them all the time.
- 00:11 They're also called functions in some programming languages.
- 00:13 So, if I call them, accidentally, functions from time to time,
- 00:16 I mean methods.
- 00:17 Methods, functions, they're the same thing.
- 00:19 And a method is a block of code that sort of sits off to the side and
- 00:24 does something.
- 00:25 Now, up until now everything we've done, all the code we've written,
- 00:29 just gets executed whenever we run the program.
- 00:32 We run the program, the code runs, and then it ends.
- 00:35 And that's fine, methods are different.
- 00:38 They don't run unless you specifically call them.
- 00:41 So we'll create it, it'll be sitting there, but
- 00:43 it won't get executed in our program until we specifically say, hey, run that method.
- 00:49 And methods are a little bit complicated, but really not that easy.
- 00:52 Just think of them as blocks of code that sit off to the side that
- 00:56 do something, right?
- 00:57 So, very useful.
- 00:58 And with programming, so often you do the same thing over, and over, and over,
- 01:03 again, in a program.
- 01:04 And you don't want to write that same block of code, over, and over, and
- 01:08 over, again.
- 01:08 You want to write it once and then just call it when you need it, right?
- 01:12 So, it's a principle called DRY, don't repeat yourself.
- 01:16 And it's very important in programming.
- 01:18 So, in this video, we're just going to start to touch on methods.
- 01:21 In the next couple of videos, we'll get into them in more detail.
- 01:24 But this video, I just want you guys to familiarize yourself with them.
- 01:27 And then learn how to create a very basic one, and
- 01:29 then we'll get into more complicated stuff in the next few videos.
- 01:33 So, I'm going to come up here and let's just create an int, and
- 01:37 I'll say a, b, and result.
- 01:40 And let's go a = 3, b = 2, and
- 01:44 result = a + b, all right?
- 01:48 So, here we can Console.WriteLine.
- 01:56 And maybe we want to interpolate this and
- 02:00 let's go,{a} + {b} = {result}.
- 02:05 So, just a very basic program, right?
- 02:10 So, if we save this and run it, we know what's going to happen.
- 02:12 It's going to print out onto the screen, 3 + 2 = 5, right?
- 02:16 And so, here's another example of our code just being executed when we
- 02:19 run the program.
- 02:20 So, let's say after this we want to put a little message
- 02:23 on the screen that says program ended or something.
- 02:26 And we could do it right here.
- 02:27 Let's say we want to create our own method, and
- 02:29 put that little message in there, and then just call the method.
- 02:32 Think very basic method, how would we do that?
- 02:34 So, up until now, we've been doing everything inside of these brackets.
- 02:39 And you'll notice this Main, that's actually a method.
- 02:42 We haven't really talked about that, but Main is actually a method.
- 02:45 So, to create our own method, it's going to look very similar to this.
- 02:48 So, to do that we go, static void.
- 02:52 And we'll talk about void in the next couple of videos.
- 02:54 Don't worry about it, yet.
- 02:55 And now, let's just name this, and let's say DisplayMessage, all right?
- 03:00 And we put our parentheses.
- 03:02 Now, underneath here we put curly brackets, right?
- 03:06 So, you see, this, looks very much like this, right?
- 03:11 It's got the static void, the name of it and curly brackets, static void,
- 03:15 the name and curly brackets.
- 03:17 And inside of these curly brackets, we can do anything we want.
- 03:19 We can write one line of code, two lines of code, 1,000 lines of code,
- 03:23 it doesn't matter.
- 03:24 All of the code that we write in here won't be executed until
- 03:27 we tell the program, specifically, hey, run this method.
- 03:30 So, let's go, I don't know, Console.WriteLine.
- 03:37 And let's say, Program Ends..., all right?
- 03:41 So if we run this, what do you think's going to happen?
- 03:45 Yeah, trick question.
- 03:47 It just says, 3 + 2 = 5.
- 03:49 because like I said, our method doesn't actually run until we tell it to run.
- 03:52 And we haven't told it to run, yet.
- 03:54 We've created the method, but we haven't told our program, hey, run that method.
- 03:58 So, how do we do that?
- 03:59 Well, you just name it.
- 04:00 So, here's the name and
- 04:02 we can come up here into our Main method here, and just go like this.
- 04:07 Now, it will put 3 in a, 2 in b, add up the results, put them on the screen.
- 04:12 Then it will, hey, call this method, which will then do this, all right?
- 04:16 So, if we save this and run it, we see 3 + 2 = 5, program ends.
- 04:23 Very cool and, again, this is a very basic method.
- 04:26 We're not doing much anything here, but
- 04:28 we'll get into more complicated stuff in the next few videos.
- 04:32 So, we can call this as often as we want, right?
- 04:35 So we could call it 3 times, here.
- 04:37 If we run this it will say, 3 + 2 = 5, Program Ends,
- 04:42 Program Ends, Program Ends, right?
- 04:45 We can do other stuff inside of here.
- 04:49 So, maybe, we copy this and do it again, I don't know.
- 04:51 I don't know why you would want to do that, but you can.
- 04:54 You can see, now, 3 + 2 = 5, Program Ends, 3 + 2 = 5, Program Ends.
- 04:59 So, those are methods in a very small nutshell.
- 05:00 We'll get into them in more detail in the next couple of videos.
- 05:03 In the next video, I'll show you how to pass things into your
- 05:06 methods from outside of your methods, and then do stuff with them.
- 05:09 Very interesting, fun stuff, and that'll be in the next video.
Lesson notes are only available for subscribers.