1 00:00:00,580 --> 00:00:07,390 We need to talk about nested functions, let's say you have a function like this called Two Steps Forward, 2 00:00:07,540 --> 00:00:13,990 one back, and we pass in a distance of steps, which then returns an integer. 3 00:00:14,980 --> 00:00:20,560 And what this function is going to do is take literally two steps forwards and then one back multiplied 4 00:00:20,710 --> 00:00:22,720 by the distance that you pass in. 5 00:00:23,510 --> 00:00:23,800 Okay. 6 00:00:23,950 --> 00:00:29,800 And if you're not from the English speaking world, perhaps you don't recognize this, but it's a common 7 00:00:30,130 --> 00:00:32,860 I don't know, it's in common parlance, in common English. 8 00:00:33,640 --> 00:00:35,730 Sometimes things are so hard in life. 9 00:00:35,740 --> 00:00:39,490 You take two steps forwards, but you always slide one back. 10 00:00:39,490 --> 00:00:41,320 And I put coding in that category. 11 00:00:41,560 --> 00:00:44,530 When you're just learning, it's pretty difficult to learn it. 12 00:00:45,580 --> 00:00:45,910 Right. 13 00:00:45,940 --> 00:00:46,900 How would we do this? 14 00:00:46,930 --> 00:00:51,010 Well, most people would type a line of code, a line of code, a line of code, etc.. 15 00:00:51,200 --> 00:00:59,800 OK, but what we can do is we can nest functions so we can declare a function inside of a function. 16 00:01:00,070 --> 00:01:01,180 And I've got a couple here. 17 00:01:02,870 --> 00:01:10,430 This is the forward one, so we have a function forward which goes a distance and passes and passes 18 00:01:10,430 --> 00:01:11,510 out an integer. 19 00:01:11,930 --> 00:01:14,650 Now this is all mashed together in one line. 20 00:01:14,900 --> 00:01:16,520 We can actually do this. 21 00:01:19,090 --> 00:01:26,380 So for Swift, it doesn't actually matter if you're all in one line or not, because it'll evaluate 22 00:01:26,380 --> 00:01:29,230 the same way, other languages don't necessarily do that. 23 00:01:29,240 --> 00:01:31,210 So be careful in that respect. 24 00:01:32,110 --> 00:01:38,410 It's what this function forward does is return the distance times to because two steps forwards, then 25 00:01:38,410 --> 00:01:40,000 we have a function for backwards. 26 00:01:42,110 --> 00:01:43,700 Can you guys hear that plane outside? 27 00:01:45,710 --> 00:01:49,440 Because I sure can it's really loud to me, but you probably can't hear it. 28 00:01:50,120 --> 00:01:56,480 OK, then we have a function for backward which returns the distance times minus one, because two forwards, 29 00:01:56,480 --> 00:01:57,170 one backwards. 30 00:01:57,170 --> 00:01:58,550 Backwards is negative. 31 00:02:00,030 --> 00:02:06,550 Now, I cannot call those functions outside of the original function, so I can't say forward. 32 00:02:07,140 --> 00:02:08,270 It just doesn't exist. 33 00:02:09,910 --> 00:02:11,830 Same for backward, nothing that. 34 00:02:13,480 --> 00:02:16,690 But I can actually call them inside here. 35 00:02:18,910 --> 00:02:23,140 And I can pass in the distance, I can also call backward. 36 00:02:26,030 --> 00:02:27,350 Pass in the distance. 37 00:02:28,160 --> 00:02:31,880 So we want to have forward and backward and what have I got here? 38 00:02:31,910 --> 00:02:33,200 Yes, we're going to add them. 39 00:02:34,460 --> 00:02:39,620 So put this onto the next line at that, and then we're going to return it. 40 00:02:41,450 --> 00:02:43,700 So if we run that function. 41 00:02:47,640 --> 00:02:53,710 With two, we should get an answer of two, because it'll go four steps forward and two back against 42 00:02:53,710 --> 00:02:53,880 to. 43 00:02:54,760 --> 00:03:02,230 And there's the answer we have to now, obviously, the way the maths of this works out is we just simply 44 00:03:02,230 --> 00:03:04,950 get back, I think the number that we originally put in. 45 00:03:05,500 --> 00:03:06,310 So it's for. 46 00:03:07,370 --> 00:03:11,820 That's just the way the math works out, but my point is a nested function sits inside a function. 47 00:03:11,850 --> 00:03:18,300 Now, you're going to come across this concept quite often, but it won't necessarily have the function 48 00:03:18,300 --> 00:03:19,430 keyword attached to it. 49 00:03:19,890 --> 00:03:26,190 What you have to recognize is when you have these curly braces and that tells you that what you're looking 50 00:03:26,190 --> 00:03:29,770 at is a function, whether it's named or it's anonymous doesn't matter. 51 00:03:30,120 --> 00:03:32,250 You're looking at a nested function. 52 00:03:33,450 --> 00:03:39,900 I tend not to use nested functions, to be honest, I don't think they're very useful in terms of structuring 53 00:03:39,900 --> 00:03:42,660 your code and they might confuse things. 54 00:03:42,660 --> 00:03:45,510 But as always, there are use cases for everything. 55 00:03:45,840 --> 00:03:46,250 Right. 56 00:03:46,260 --> 00:03:47,580 Time for you to do the same thing. 57 00:03:48,060 --> 00:03:50,730 Here's your homework and make a function to add two numbers. 58 00:03:51,150 --> 00:03:52,800 Use a nested function to check. 59 00:03:52,800 --> 00:03:56,960 The answer is less than 10 and return greater than if it is. 60 00:03:57,690 --> 00:03:58,970 That should be less than it. 61 00:04:01,000 --> 00:04:07,550 And return less than if it is less than 10 or greater than 10, if it's not less than 10, right. 62 00:04:07,680 --> 00:04:11,980 So a lot of confusing words there, I think even now that I've done it. 63 00:04:12,160 --> 00:04:19,900 But I'm saying it I feel pretty confused reading it and see what you can make of that and let me see 64 00:04:19,900 --> 00:04:20,830 what you come up with.