1 00:00:05,870 --> 00:00:09,180 In this section of the course, we'll learn about c++ functions. 2 00:00:09,860 --> 00:00:13,020 Functions allow us to divide our programs into modular units 3 00:00:13,020 --> 00:00:16,840 of executable code and call and reuse these units as we wish. 4 00:00:17,719 --> 00:00:20,409 Functions are very powerful abstraction mechanism. 5 00:00:21,020 --> 00:00:23,860 You've already seen functions defined. The main function that 6 00:00:23,860 --> 00:00:25,259 we've been using in every program. 7 00:00:25,810 --> 00:00:28,799 And you've also used some functions such as getline and the 8 00:00:28,800 --> 00:00:32,100 c-style string functions, string copy, string length and others. 9 00:00:32,860 --> 00:00:35,460 In this section, we'll understand what functions are 10 00:00:35,460 --> 00:00:36,600 and why they're so useful. 11 00:00:36,969 --> 00:00:38,670 Then we'll look at functions in detail. 12 00:00:39,350 --> 00:00:42,190 First, we'll see how to define functions and create function 13 00:00:42,220 --> 00:00:45,430 prototypes that make our functions useful anywhere in our code. 14 00:00:46,280 --> 00:00:49,639 As we've already seen, when we call a function, we usually send 15 00:00:49,639 --> 00:00:51,119 information to the function. 16 00:00:51,420 --> 00:00:54,200 This information is sent in the form of function parameters. 17 00:00:54,450 --> 00:00:57,290 We'll see how to define these parameters and exactly how they work. 18 00:00:57,880 --> 00:01:01,309 Functions also usually return some kind of information to us 19 00:01:01,370 --> 00:01:02,620 when they're done processing. 20 00:01:03,030 --> 00:01:06,289 For example, if we have a function that calculates the square root of a 21 00:01:06,289 --> 00:01:09,929 number, well, that function expects the number, and it will return to 22 00:01:09,929 --> 00:01:12,870 us the square root of the number, which we can use however we wish. 23 00:01:13,880 --> 00:01:16,050 Functions can also take many parameters. 24 00:01:16,350 --> 00:01:19,369 And in some cases, we want to default the value of some of 25 00:01:19,370 --> 00:01:21,860 those parameters, so we don't have to provide them every time. 26 00:01:22,469 --> 00:01:25,240 We'll learn how to set these default values for parameters to 27 00:01:25,240 --> 00:01:26,509 make our code easier to write. 28 00:01:27,639 --> 00:01:30,589 Then we'll learn how to pass arrays to functions, and how to work 29 00:01:30,590 --> 00:01:31,850 with them inside the function. 30 00:01:32,810 --> 00:01:35,009 I'll discuss references in the next section. 31 00:01:35,180 --> 00:01:37,740 But we'll look at how we can pass information to a function 32 00:01:37,740 --> 00:01:40,009 efficiently by avoiding copying. 33 00:01:40,330 --> 00:01:43,220 We'll also see how we can change past information as 34 00:01:43,220 --> 00:01:44,719 well as protect it from change. 35 00:01:45,950 --> 00:01:47,860 Then we'll discuss inline functions. 36 00:01:48,230 --> 00:01:51,210 Inline functions are functions that the compiler optimizes 37 00:01:51,450 --> 00:01:54,320 to avoid the overhead involve when we call functions. 38 00:01:55,310 --> 00:01:58,250 Remember, when we use the auto keyword to allow the compiler to 39 00:01:58,250 --> 00:02:01,880 deduce the type of a variable? Well, we can also use the auto keyword 40 00:02:01,880 --> 00:02:05,269 to allow the compiler to deduce the type of the function return value. 41 00:02:05,690 --> 00:02:08,180 This is pretty cool and it can save us from writing a lot of 42 00:02:08,180 --> 00:02:09,788 unnecessarily complex code. 43 00:02:10,729 --> 00:02:12,900 Finally, we'll learn about recursive functions. 44 00:02:13,200 --> 00:02:15,049 These are functions that call themselves. 45 00:02:15,530 --> 00:02:17,910 You might be wondering why would a function call itself. 46 00:02:18,370 --> 00:02:20,789 You'll see it's pretty cool and it can help solve a class 47 00:02:20,790 --> 00:02:23,930 of problems that would be more complex to solve without recursion. 48 00:02:24,670 --> 00:02:27,500 At the end of this section, we'll do a challenge project, and we'll 49 00:02:27,510 --> 00:02:30,360 be well on our way to starting to learn object-oriented programming. 50 00:02:30,580 --> 00:02:31,580 Let's get started.