Locked lesson.
About this lesson
If you'd like to use Methods and return data to your program, we explore how that's done.
Exercise files
Download this lesson’s related exercise files.
29 - Return Methods.docx61.5 KB 29 - Return Methods SOLUTION.docx
58.5 KB
Quick reference
Return Methods
Methods can return data back to your main program.
When to use
Do this whenever you want to return data back to your program from a method.
Instructions
{
int a, b
a = 3;
b = 2;
Addthings(a,b);
result = Addthings(a, b);
Console.WriteLine($"{a} + {b} = {result}");
}
static int Addthings(int x, int y)
{
return x + y;
}
Hints & tips
- Remember to call a method as the data type to be returned, if you plan on returning data from that method.
- 00:04 Okay, in the last video I showed you how to pass parameters into a method and
- 00:07 then do something in that method.
- 00:09 But many times you're going to want to pass something into a method,
- 00:12 do something, and then have that method return something back to your main program
- 00:17 and then do something, all right?
- 00:19 So that's what we're going to look at in this video, returning methods.
- 00:22 So here, we're passing a 4 and a 1 and then we're writing everything out here.
- 00:29 Well, we might not want to do that.
- 00:30 Maybe we just want to do the math in this function, right?
- 00:37 X + y.
- 00:38 And then return whatever the answer is back into this main method and
- 00:43 then here console write out the answer.
- 00:46 So here it goes console.write line.
- 00:50 And then we could again do this guy,
- 00:55 we could say a + b = result, all right?
- 01:01 And we need to change this back to a and b.
- 01:05 So in order to return something from a method,
- 01:07 we need to change the definition of the method, this thing right here.
- 01:10 So up until now we've been using void.
- 01:13 Again, our main is void, but when you want to return something with a method,
- 01:17 you need to use a data type.
- 01:19 So in this case, we're going to be adding x + y and returning an integer.
- 01:23 So instead of void, we need to change this to int, right?
- 01:26 So we're going to return an int, so this is now an int method, right?
- 01:30 So inside of here, if we want to return something, we just type in return and
- 01:35 then do the thing, right?
- 01:37 So this will return x and y, which is this and this,
- 01:41 which are a and b when we ran this guy, all right?
- 01:46 Which are these two things.
- 01:48 So here we can do this several different ways.
- 01:50 We could just call it like this inside of here.
- 01:54 That will work and we need our semicolon there.
- 01:57 So if we save this and run it.
- 02:01 We get 3 + 2 = 5, right?
- 02:04 That's a little sloppy.
- 02:06 I would maybe take it out of there and instead use a variable result.
- 02:11 And here we can call result and
- 02:13 set it equal to that called method with whatever we passed in.
- 02:19 So, if we save this and run it, again, we'll get the same 3 + 2 = 5.
- 02:24 Now here we're returning something inside of here.
- 02:27 We can do other stuff as well.
- 02:29 So for instance, we could call our display message function
- 02:34 inside of here, it will say program ends.
- 02:37 So let's say program begins instead.
- 02:41 Save this and run it.
- 02:44 Now we see program begins 3 + 2 = 5, right?
- 02:47 because the program flow of this, we start up here,
- 02:51 we call this we pass in our a and b, which then runs this.
- 02:55 The first thing it does is run this, which is this guy.
- 02:58 And then it picks back up here, returns x + y, which comes back here,
- 03:03 gets assigned into results and then output right there.
- 03:07 So a little bit convoluted, but the main thing to remember is,
- 03:10 whenever you want to return a function or a method,
- 03:12 you need to define the data type that you're going to be returning.
- 03:16 So in our case, we're returning an integer, so
- 03:19 this now becomes static int AddThings, whereas before it was static void, right?
- 03:24 And same thing here with passing variables, we can do the same thing,
- 03:27 we could pass a 4 and a 1.
- 03:28 And then maybe down here we need a 4 and a 1.
- 03:32 We don't really need brackets anymore to do that, but same thing.
- 03:37 Now we get 4 + 1 = 5.
- 03:40 And our program knows that because 4 and 1 get passed into this method.
- 03:46 It does the math, it returns it,
- 03:48 which gets assigned to this variable in all as well.
- 03:52 So this a very quick crash course in methods.
- 03:55 There's much more to learn about methods, but you'll pick these things up as we go.
- 03:59 You just need to understand that methods are blocks of code that sit off to
- 04:03 the side, they get executed only when you call them.
- 04:06 And what we do in them is whatever you want.
- 04:09 So again, these are kind of silly examples we have here.
- 04:12 They're not very complicated programs that you normally wouldn't need to break apart
- 04:16 into methods.
- 04:16 But as you go on, methods will become more complicated,
- 04:19 there will be more code in there.
- 04:21 And it's a great way to break apart your programs and keep things separate,
- 04:26 DRY, don't repeat yourself, do it one time, and very cool.
- 04:30 So that's all for this video.
- 04:31 In the next video, we're going to start to look at comparison operators and logic.
Lesson notes are only available for subscribers.