1 00:00:00,350 --> 00:00:02,080 A function performs a task. 2 00:00:06,120 --> 00:00:10,350 So far, you learned to storing that information, using variables and control the way your code runs, 3 00:00:10,350 --> 00:00:11,940 using offense, which. 4 00:00:13,300 --> 00:00:15,910 The next step is to organize your code and the functions. 5 00:00:19,140 --> 00:00:21,750 And in this lesson, you're gonna learn to write functions and call them. 6 00:00:25,110 --> 00:00:28,080 So first, you're going to in a new job, a project named section for. 7 00:00:30,030 --> 00:00:34,700 And inside the project, you're going to get a new file named chorused Java and make sure the chorus 8 00:00:34,710 --> 00:00:36,360 class has the main method. 9 00:00:45,470 --> 00:00:49,070 All right, in this lesson, you're going to build a program that sings the chorus to blame it on the 10 00:00:49,070 --> 00:00:50,600 boogie by a Michael Jackson. 11 00:00:53,030 --> 00:00:55,670 If you listen to the song, then you're familiar with the chorus. 12 00:00:56,150 --> 00:00:59,660 The chorus is just four lines long and it's one of the more famous songs out there. 13 00:01:03,630 --> 00:01:08,580 In any case, because the chorus is four lines long, that means we need for print statements. 14 00:01:09,660 --> 00:01:10,530 So I'm going to print. 15 00:01:14,260 --> 00:01:15,640 Don't blame it on the sunshine. 16 00:01:31,470 --> 00:01:32,880 Don't blame it on the moonlight. 17 00:01:47,170 --> 00:01:48,940 Don't blame it on goodtimes. 18 00:02:02,280 --> 00:02:03,840 Blame it on the boogie. 19 00:02:11,430 --> 00:02:19,050 And how robotic do I sound in any case, Michael Jackson sings this chorus five times, so your application, 20 00:02:19,050 --> 00:02:20,730 the chorus singer, needs to do the same. 21 00:02:22,670 --> 00:02:26,120 So naturally, I'm going to try to copy and paste this far more times. 22 00:02:34,240 --> 00:02:36,370 All right, let's just run to make sure everything's all right. 23 00:02:36,400 --> 00:02:37,480 We wrote a lot of code. 24 00:02:47,670 --> 00:02:48,590 And everything's good. 25 00:02:52,290 --> 00:02:55,820 But wow, that was so much code for a task, so simple. 26 00:02:57,370 --> 00:03:03,340 Honestly, just looking at it makes me nauseous on that very same note, never, ever copy and paste 27 00:03:03,340 --> 00:03:04,210 code ever. 28 00:03:04,270 --> 00:03:05,710 It's very bad practice. 29 00:03:07,150 --> 00:03:11,140 So how do we sing the chorus five times without copying the code five times? 30 00:03:14,490 --> 00:03:16,140 Well, let me introduce functions. 31 00:03:17,190 --> 00:03:22,710 If you ever feel the need to copy and paste code, then you should probably use a function instead because 32 00:03:22,710 --> 00:03:25,680 the function is a grouping of code that can perform a task. 33 00:03:27,090 --> 00:03:31,590 You're going to make a function that performs one task, and once you define the function, you can 34 00:03:31,590 --> 00:03:33,320 call it as many times as you need to. 35 00:03:38,170 --> 00:03:44,200 Now, there are five parts to a function, there is the level of access which is more often than not 36 00:03:44,200 --> 00:03:48,520 going to be public because public means we can access the function from anywhere. 37 00:03:49,450 --> 00:03:54,400 There's the return value in this case, void means the function doesn't return a value, it returns 38 00:03:54,400 --> 00:03:55,540 nothing void. 39 00:03:57,240 --> 00:04:03,540 The function name the name of the function should reflect the task that it performs, there are the 40 00:04:03,540 --> 00:04:07,770 parameters, the function can receive parameters and use them in its code. 41 00:04:08,340 --> 00:04:11,220 And then there's the code that actually performs your task. 42 00:04:12,720 --> 00:04:16,529 And so we could talk about this all day, but the best way to understand the different function parts 43 00:04:16,529 --> 00:04:19,649 is to employ them by creating your first function. 44 00:04:28,160 --> 00:04:31,180 It's going to be a very simple function, the function is going to be public. 45 00:04:32,430 --> 00:04:34,200 So that we can call the function from anywhere. 46 00:04:35,380 --> 00:04:38,860 The return value, he was going to be void and it's not going to return any values. 47 00:04:39,780 --> 00:04:44,160 The name of the function is going to be sing chorus because it's going to sing the chorus to the Michael 48 00:04:44,160 --> 00:04:45,060 Jackson song. 49 00:04:46,540 --> 00:04:50,780 And the function is not going to have any parameters because the code is not going to need them. 50 00:04:51,910 --> 00:04:52,280 All right. 51 00:04:52,300 --> 00:04:55,840 Now, remember that a function is a grouping of code that performs one task. 52 00:04:57,170 --> 00:04:59,780 So it's going to contain code that sings the chorus. 53 00:05:05,980 --> 00:05:10,850 All right, that's all you define a function that performs a task and it sings the chorus. 54 00:05:11,440 --> 00:05:12,820 So how do we use this function? 55 00:05:13,120 --> 00:05:14,080 How do we run its. 56 00:05:17,220 --> 00:05:19,560 We need to call a function in order to run its. 57 00:05:24,980 --> 00:05:29,960 The Syncora function is public, which means we can call it from anywhere, and so we need to call it 58 00:05:29,960 --> 00:05:33,140 from Maine because Maine is the entry point of our application. 59 00:05:39,480 --> 00:05:43,770 And calling a function is easy, all you need to do is refer to it by name saying Kooris. 60 00:05:47,640 --> 00:05:51,150 With a set of parentheses and there seems to be an error. 61 00:05:52,530 --> 00:05:56,670 Let's compile our code anyway, just to get a better picture of what that error really is. 62 00:06:05,730 --> 00:06:11,370 And there is the error, non static method, Syncora cannot be referenced from a static context. 63 00:06:12,640 --> 00:06:17,470 If you truly take the time to understand what this error is telling you, it says that we're calling 64 00:06:17,470 --> 00:06:20,380 the Syncora function from the static main method. 65 00:06:20,890 --> 00:06:24,640 And for this to work, the function that we're calling needs to be static as well. 66 00:06:25,330 --> 00:06:29,620 In essence, all it's telling you is that you need to add static keyword to your function. 67 00:06:35,630 --> 00:06:37,460 All right, things time to run the code. 68 00:06:44,470 --> 00:06:45,490 And everything works out. 69 00:06:49,600 --> 00:06:54,220 aFunction is a grouping of code that performs a task, can you define a function that sings Acorah's? 70 00:06:55,440 --> 00:07:00,030 And then you called the function now, even though this worked, you might still have some questions 71 00:07:00,030 --> 00:07:01,410 regarding the static Fuad. 72 00:07:02,390 --> 00:07:07,490 For now, I'm going to tell you is that every function in our class needs to be static, don't worry 73 00:07:07,490 --> 00:07:08,070 about why that is. 74 00:07:08,090 --> 00:07:13,220 For now, you're going to learn more about the static keyword in module two object oriented programming, 75 00:07:13,220 --> 00:07:18,470 because understanding how static works requires a basic understanding of programming using objects. 76 00:07:22,510 --> 00:07:27,970 In any case, what's interesting here is how easy it is to call a function, if you want to sing the 77 00:07:27,970 --> 00:07:31,600 chorus many times, then just call the function many times. 78 00:07:34,590 --> 00:07:36,090 All right, we can run the code. 79 00:07:44,930 --> 00:07:48,830 And that is just amazing, this is what makes functions reuseable. 80 00:07:49,870 --> 00:07:52,930 Once you define a function, you can call it as many times as you want. 81 00:07:57,450 --> 00:08:00,480 Functions make your code more organized than reuseable. 82 00:08:04,480 --> 00:08:10,480 So go back to the same logic, but without functions, and this is extremely bad coding, if you ever 83 00:08:10,480 --> 00:08:12,780 find yourself copying and pasting code, just stop. 84 00:08:12,790 --> 00:08:14,130 It's a very bad practice. 85 00:08:14,740 --> 00:08:19,240 Instead, make a function that performs the task and then you can call the function as many times as 86 00:08:19,240 --> 00:08:19,780 you need to. 87 00:08:22,550 --> 00:08:24,170 And to me, this looks a lot better. 88 00:08:28,450 --> 00:08:29,810 Now, here's a fun fact for you. 89 00:08:29,860 --> 00:08:31,210 Maine is also a function. 90 00:08:33,140 --> 00:08:38,210 Look at Maine and course, they have the exact same signature public static void. 91 00:08:39,240 --> 00:08:44,430 And that's because both of them are functions, but they both have separate tasks, the course function 92 00:08:44,430 --> 00:08:50,730 performs the task of singing a chorus, and the main function performs the more important task of running 93 00:08:50,730 --> 00:08:53,230 your application whenever you run your code. 94 00:08:53,250 --> 00:08:57,120 What Java is actually doing is it's calling the main function behind the scenes. 95 00:09:00,670 --> 00:09:06,520 And here are some things for you to consider, always right function names using lower Camiel case like 96 00:09:06,520 --> 00:09:09,360 sing chorus, play game kickball. 97 00:09:09,850 --> 00:09:14,290 The first word starts with a lowercase letter and the words that follow are uppercase. 98 00:09:15,070 --> 00:09:19,930 And you're often going to hear functions being referred to as method's functions and methods mean the 99 00:09:19,930 --> 00:09:20,920 exact same thing. 100 00:09:24,790 --> 00:09:30,640 In this video, you learn to define a function and call it first, I showed you the dark and grim world 101 00:09:30,640 --> 00:09:32,250 of coding without functions. 102 00:09:32,650 --> 00:09:37,240 You built a program that sings the chorus to blame it on the boogie by Michael Jackson. 103 00:09:37,690 --> 00:09:39,430 And the chorus is four lines long. 104 00:09:39,430 --> 00:09:41,260 Michael sings the chorus five times. 105 00:09:41,260 --> 00:09:44,130 So this demanded 20 print line statements. 106 00:09:44,710 --> 00:09:46,890 Now, looking at this code is nauseating. 107 00:09:47,290 --> 00:09:53,290 You should never copy and paste code instead make a function that performs the task and then call the 108 00:09:53,290 --> 00:09:55,100 function as many times as you need to. 109 00:09:55,990 --> 00:10:00,760 You created a function that sings the chorus and then you call the function as many times as you need 110 00:10:00,760 --> 00:10:01,150 it to.