1 00:00:00,760 --> 00:00:08,290 In general function names have the same rules for variables, so they must begin with a letter or an 2 00:00:08,290 --> 00:00:13,800 underscore and cannot contain spaces or other punctuation characters here. 3 00:00:13,810 --> 00:00:17,410 So now I'm going to write some functions. 4 00:00:18,580 --> 00:00:27,550 Integer My function here, an integer like my function. 5 00:00:29,360 --> 00:00:33,070 And when we do the integer it here. 6 00:00:34,310 --> 00:00:45,200 This is the illegal and the compiler compiler will or will not compile here will not compile. 7 00:00:46,470 --> 00:00:46,890 Here. 8 00:00:48,390 --> 00:00:49,200 And. 9 00:00:50,830 --> 00:00:58,570 Uh, as I said earlier, they must begin with a letter or an underscore character and cannot contain 10 00:00:58,570 --> 00:01:05,470 spaces, as you can see here or other punctuation characters, for example, like dot comma here, they 11 00:01:05,470 --> 00:01:09,360 cannot contain any punctuation characters for. 12 00:01:09,370 --> 00:01:16,360 So following the general principle of Self-documenting code, you should name the function according 13 00:01:16,390 --> 00:01:18,580 to what it does. 14 00:01:18,580 --> 00:01:25,420 So there is one exception, and these are the special functions used to provide overloads for operators, 15 00:01:25,570 --> 00:01:27,880 which is mostly punctuation symbols. 16 00:01:27,880 --> 00:01:36,310 So these functions have a name in the form here, for example, the multi-year where the. 17 00:01:37,600 --> 00:01:43,300 Mold is like describing the function which will will multiply the two values one by one. 18 00:01:43,300 --> 00:01:46,690 So operators are one example of overloading here. 19 00:01:46,690 --> 00:01:53,950 So you can overload any function that is use the same name, but provide implementations with different 20 00:01:53,950 --> 00:01:57,120 parameter types or different parameter numbers. 21 00:01:57,130 --> 00:02:01,210 We can make it like that, for example, integer multi-year. 22 00:02:01,240 --> 00:02:03,020 We can make multiply. 23 00:02:03,220 --> 00:02:10,390 We we can multiply three numbers one by one here, for example, l h s integer r h s. 24 00:02:10,420 --> 00:02:15,040 If you make it like that or just make it integer, for example. 25 00:02:16,860 --> 00:02:19,100 Actually they will return the same value. 26 00:02:19,110 --> 00:02:22,470 This is just a different writing in C plus plus here. 27 00:02:22,470 --> 00:02:26,190 As you can see, we're going to get error because they are the two same function. 28 00:02:26,190 --> 00:02:30,600 As you can see the redefinition of multi year, multi, multi year. 29 00:02:30,600 --> 00:02:38,760 So if we make add another parameter integer x x, we're going to make the overloading function here 30 00:02:38,760 --> 00:02:47,460 and we're going to return the return, the multiply by an error and multiply in multiply it by the X 31 00:02:47,460 --> 00:02:49,380 age s here. 32 00:02:49,380 --> 00:02:55,560 And if you make it like that as standard C out. 33 00:02:57,040 --> 00:02:59,620 See out here and here. 34 00:02:59,620 --> 00:03:01,390 The most we're going to multiply. 35 00:03:01,390 --> 00:03:08,400 We're going to call this here 8 or 910. 36 00:03:08,410 --> 00:03:18,820 So in this case, uh, the multiply function will, uh, this code here, this code will call this function 37 00:03:18,820 --> 00:03:22,900 because this takes parameters and we gave this function two parameters. 38 00:03:22,900 --> 00:03:29,830 If we make it like that, uh, this is going to call the, uh, the third function. 39 00:03:29,920 --> 00:03:35,350 The second function here that we have three parameters and we are calling this function. 40 00:03:35,350 --> 00:03:39,310 So, and this named the function overloading here. 41 00:03:40,070 --> 00:03:45,050 As you can see here, we multiplied the three numbers one by one here. 42 00:03:45,050 --> 00:03:45,710 So. 43 00:03:47,140 --> 00:03:48,850 Uh, we have function parameters. 44 00:03:48,850 --> 00:03:53,500 The function here, they may not have any parameters like this here. 45 00:03:53,500 --> 00:03:58,750 For example, we can create a function with that parameters like this. 46 00:03:58,750 --> 00:04:05,170 But they let's return create without parameters, but we'll have the return value. 47 00:04:05,170 --> 00:04:14,740 So in which case the case is the function is the fate defined with a pair of empty parentheses. 48 00:04:15,130 --> 00:04:17,110 Uh, like this here, for example. 49 00:04:17,110 --> 00:04:18,370 So a function. 50 00:04:18,580 --> 00:04:26,170 The function definition must give the type and name of the parentheses parameters between parentheses 51 00:04:26,170 --> 00:04:27,070 like this here. 52 00:04:27,070 --> 00:04:28,090 So. 53 00:04:29,280 --> 00:04:34,500 And, uh, in many cases the functions will have a fixed number of parameters. 54 00:04:34,500 --> 00:04:37,800 But you can write functions with a variable number of parameters. 55 00:04:37,800 --> 00:04:42,610 So you can also define functions with the default values or some of the parentheses. 56 00:04:42,630 --> 00:04:45,150 So some of the parameters. 57 00:04:45,150 --> 00:04:52,110 So in effect a providing a function that overloads itself on the number of parameters parameters passed 58 00:04:52,110 --> 00:04:53,550 to the function. 59 00:04:53,550 --> 00:05:00,090 So variable argument list and default arguments will be covered later in this course. 60 00:05:00,090 --> 00:05:07,950 Here we can also make the make the specify the exceptions in C plus plus function here. 61 00:05:07,950 --> 00:05:14,010 The functions also can be marked to indicate whether they will throw an exception. 62 00:05:14,010 --> 00:05:18,570 So more details about exceptions will be given in next lecture. 63 00:05:18,570 --> 00:05:22,410 But there are two syntaxes you need to be aware of. 64 00:05:22,440 --> 00:05:29,170 So earlier versions of C plus plus allowed you to use the trough specifier. 65 00:05:30,350 --> 00:05:34,320 Uh, specified on the function in three ways. 66 00:05:35,210 --> 00:05:43,190 Firstly, you provide a comma separated list of types of the exceptions that may be thrown by code in 67 00:05:43,190 --> 00:05:44,150 the function here. 68 00:05:44,870 --> 00:05:49,040 The second you can provide an ellipsis like this. 69 00:05:49,520 --> 00:05:50,240 Here. 70 00:05:53,000 --> 00:05:58,580 Lapses or which means that the function may throw an exception. 71 00:05:58,580 --> 00:06:06,050 And thirdly, you can provide an empty pair of parentheses, which means that the function will not 72 00:06:06,050 --> 00:06:07,220 throw an exception. 73 00:06:07,220 --> 00:06:08,840 So the syntax here. 74 00:06:09,110 --> 00:06:13,700 Throwing exceptions to exceptions looks like this. 75 00:06:13,700 --> 00:06:19,220 For example, throw we're going to specify the exceptions. 76 00:06:19,220 --> 00:06:23,930 For example overflow overflow error and. 77 00:06:25,320 --> 00:06:25,770 Do. 78 00:06:25,800 --> 00:06:30,930 We're going to make, for example, do something which potentially. 79 00:06:32,550 --> 00:06:34,980 May overflow flow. 80 00:06:35,190 --> 00:06:40,290 So the trough specifier has been deprecated in C plus plus 11. 81 00:06:40,290 --> 00:06:47,430 As you can see, the compiler will say the dynamic exception specifier, uh, overflow error is deprecated. 82 00:06:47,430 --> 00:06:50,700 Consider using noexcept files instead. 83 00:06:50,700 --> 00:06:58,020 Here we can change ignore accept files so it throws specifier has been deprecated in C plus. 84 00:06:58,020 --> 00:07:03,660 Plus is largely because the ability to indicate that the type of function was not useful. 85 00:07:03,660 --> 00:07:10,260 So however, the version of throw that indicates that no exception will be thrown was found to be useful 86 00:07:10,290 --> 00:07:18,690 because it enables the compiler to optimize code by providing no code infrastructure to handle exceptions 87 00:07:18,690 --> 00:07:19,950 here, exceptions here. 88 00:07:19,950 --> 00:07:29,160 So the C plus plus 11 retains this behavior with no accept specifier and the no accept specifier. 89 00:07:29,160 --> 00:07:34,930 Here we can delete this false here and add with no accept specifier. 90 00:07:34,930 --> 00:07:36,220 We're going to check. 91 00:07:37,280 --> 00:07:44,720 Check the parameter and handle overflow appropriately. 92 00:07:47,380 --> 00:07:48,010 Here.