1 00:00:00,820 --> 00:00:05,200 Functions are the basic infrastructure of C++ code. 2 00:00:05,200 --> 00:00:08,140 So the code is contained in functions. 3 00:00:08,140 --> 00:00:11,470 To execute that, you have to call a function. 4 00:00:11,470 --> 00:00:17,140 So C++ is remarkably flexible in the ways that you can define and call functions here. 5 00:00:17,140 --> 00:00:23,590 So you can define functions with a fixed number or parameters or variable number of parameters. 6 00:00:23,890 --> 00:00:29,320 So you can write generic code so that the same code can be used with different types. 7 00:00:29,320 --> 00:00:33,760 And you can even write generic code with a variable number of types here. 8 00:00:34,060 --> 00:00:37,600 So defining C++ functions. 9 00:00:37,600 --> 00:00:45,490 So at the most basic level, a function has parameters here, so it has the code to manipulate the parameters 10 00:00:45,490 --> 00:00:47,800 and it has the return value. 11 00:00:47,800 --> 00:00:52,720 So C++ gives you several ways to determine these three aspects. 12 00:00:52,720 --> 00:01:02,140 In this next lecture, we will cover those parts of C++ function from left to the right of declaration. 13 00:01:02,140 --> 00:01:08,050 So functions can also be templated, but this will be left to later sections. 14 00:01:08,740 --> 00:01:16,180 So a function must be defined exactly once, but should overloading, you can have many functions with 15 00:01:16,180 --> 00:01:20,530 the same name that differentiate by their parameters. 16 00:01:20,530 --> 00:01:25,840 So code that uses a function has to have access to the name of the function. 17 00:01:25,840 --> 00:01:32,650 And so it needs to have access to either the function definition or the declaration of the function, 18 00:01:32,650 --> 00:01:34,540 also called the function prototype. 19 00:01:34,660 --> 00:01:40,660 So the compiler uses the prototype to type check that the calling code is calling the function. 20 00:01:40,660 --> 00:01:42,370 So using the right types here. 21 00:01:42,370 --> 00:01:49,090 So typically libraries are implemented as separate compiled library files, and the prototypes of the 22 00:01:49,090 --> 00:01:56,290 library functions are provided in header files so that the many source files can use the functions by 23 00:01:56,290 --> 00:01:57,670 including the heater's. 24 00:01:57,910 --> 00:02:04,660 So however, if you know the function name parameters and return type, you can type the prototype yourself 25 00:02:04,660 --> 00:02:06,220 in your file here. 26 00:02:06,220 --> 00:02:13,180 So whichever you do, you're simply providing the information for the compiler to type check the expression 27 00:02:13,180 --> 00:02:14,410 that calls function. 28 00:02:14,410 --> 00:02:21,100 So it's up to the linker to locate the function in the library and either copy the code into the executable 29 00:02:21,100 --> 00:02:25,630 or set up the infrastructure to use the function from a shared library. 30 00:02:25,630 --> 00:02:32,200 So including the header file from for a library does not mean that you will be able to use the functions 31 00:02:32,200 --> 00:02:33,280 from that library. 32 00:02:33,280 --> 00:02:40,690 Because in standard C++, the header file does not have information about the library that contains 33 00:02:40,690 --> 00:02:41,500 a function. 34 00:02:42,150 --> 00:02:45,900 So most of the C runtime library is implemented this way here. 35 00:02:45,900 --> 00:02:52,830 So the function is completely compiled in a static library or a dynamic link library, and the function 36 00:02:52,830 --> 00:02:56,220 prototypes are provided in a header file. 37 00:02:56,820 --> 00:03:03,210 So you provide the library and the linker command line and typically you will include the header file 38 00:03:03,210 --> 00:03:08,850 for the library so that the function prototypes are available to the compiler. 39 00:03:09,270 --> 00:03:16,620 So as long as the linker knows about the library, you can type the prototype in your code here so this 40 00:03:16,620 --> 00:03:21,030 can save you from encoding some large files into your source files. 41 00:03:21,030 --> 00:03:26,880 The files that will mostly have prototypes of the function that you will not use here. 42 00:03:26,880 --> 00:03:33,840 So far in this course we have used one source files, so all the functions are defined in the same file 43 00:03:33,840 --> 00:03:35,760 as where they are used. 44 00:03:35,760 --> 00:03:38,550 So and we have defined the function before calling it. 45 00:03:38,550 --> 00:03:44,160 So that is the function is defined above the code that calls it here. 46 00:03:44,160 --> 00:03:44,460 So. 47 00:03:44,460 --> 00:03:48,510 So here you do not have to define the function before it is used. 48 00:03:48,510 --> 00:03:53,190 As long as the function prototype is defined before the function is called here. 49 00:03:53,190 --> 00:04:01,140 So in main function we're going to print something here, mute here for example C out mult. 50 00:04:02,350 --> 00:04:04,510 As you can see, we have to input the. 51 00:04:07,160 --> 00:04:09,740 Okay, CDC out here. 52 00:04:10,700 --> 00:04:17,000 And as you can see, we got an error because we have to import the Ustream header file. 53 00:04:17,300 --> 00:04:20,060 And as you can see, we can now print something on the string. 54 00:04:20,300 --> 00:04:24,380 So moved here yesterday and. 55 00:04:25,300 --> 00:04:27,730 Six, six, seven. 56 00:04:29,110 --> 00:04:30,250 And in line. 57 00:04:33,840 --> 00:04:34,320 Here. 58 00:04:34,320 --> 00:04:38,190 And after that, we're going to create the mold function here. 59 00:04:38,580 --> 00:04:40,520 Mold air into. 60 00:04:40,530 --> 00:04:43,140 I will show you what's wrong with this coat here. 61 00:04:43,850 --> 00:04:44,400 RH. 62 00:04:44,520 --> 00:04:51,060 S here and return the l hs multiply by x here. 63 00:04:52,230 --> 00:04:57,210 So the mouth function here defined after the main function here. 64 00:04:57,210 --> 00:05:00,420 But this court will the here. 65 00:05:00,450 --> 00:05:01,920 Let's try this out. 66 00:05:03,120 --> 00:05:04,950 Actually see here. 67 00:05:10,540 --> 00:05:10,960 Here. 68 00:05:11,170 --> 00:05:18,260 This court will not compile because it is given the prototype before the main function. 69 00:05:18,280 --> 00:05:21,850 So this is called here, if it makes like that. 70 00:05:25,400 --> 00:05:31,640 As you can see here, the function can compile and it will multiply six by seven here. 71 00:05:31,850 --> 00:05:36,800 So the prototype doesn't have to be have two parameter names. 72 00:05:36,920 --> 00:05:42,800 So this is because the compiler only needs to know the types of the parameters so their names. 73 00:05:42,800 --> 00:05:48,380 So however, since parameter names should be self documenting, it's usually a good idea to get the 74 00:05:48,380 --> 00:05:54,380 parameter names so that you can see the purpose of the function here. 75 00:05:55,540 --> 00:06:03,880 Now, here in this lecture, in this example, we the function is defined in the same source file. 76 00:06:03,910 --> 00:06:09,400 So the internal linkage, this means the internal there is an internal linkage here in this code. 77 00:06:09,430 --> 00:06:15,950 So if the function is defined in another file, the prototype will have external linkage. 78 00:06:15,970 --> 00:06:24,490 And so the prototype will have to be defined, for example, like disc here, extend. 79 00:06:25,870 --> 00:06:28,480 X10 integer mode. 80 00:06:29,240 --> 00:06:31,700 Here are integer integers here. 81 00:06:34,510 --> 00:06:41,530 So the external keyboard is one of many specifiers that you can add to a function declaration. 82 00:06:41,530 --> 00:06:46,050 And in the previous lectures we have seen others. 83 00:06:46,060 --> 00:06:51,430 So for example, the static specifiers can be used on a prototype to indicate that the function has 84 00:06:51,430 --> 00:06:56,770 internal linkage and the name can only be used in the current source file. 85 00:06:57,430 --> 00:07:05,350 So current source file in this example here in, for example, static. 86 00:07:06,780 --> 00:07:07,470 Integer. 87 00:07:08,270 --> 00:07:11,510 So you have to write static before the function return type here. 88 00:07:11,960 --> 00:07:13,460 So in this example. 89 00:07:14,710 --> 00:07:19,600 It is appropriate to mark the function as static in this case. 90 00:07:19,600 --> 00:07:26,800 So you can also declare a function as an external here, which affects how the name of the function 91 00:07:26,800 --> 00:07:30,490 is stored in the object file. 92 00:07:30,490 --> 00:07:35,410 So this is important for libraries and will be covered shortly.