1 00:00:00,750 --> 00:00:05,460 In this lecture you will learn about the constant expressions in C plus plus. 2 00:00:05,460 --> 00:00:15,570 So C plus plus 11 introduce a keyword called Constexpr here, const expression const expression Constexpr 3 00:00:15,570 --> 00:00:16,050 here. 4 00:00:16,050 --> 00:00:22,560 So this is applied to an expression and indicates that the expression should be evaluated at compile 5 00:00:22,590 --> 00:00:24,510 time rather than at runtime. 6 00:00:24,510 --> 00:00:29,370 So for example, let's create an override code here. 7 00:00:30,390 --> 00:00:44,700 Const expression double pi 1415 and let's create another const expression, double expression expr double. 8 00:00:44,940 --> 00:00:48,390 And here we're going to write two pi. 9 00:00:49,620 --> 00:00:50,150 European. 10 00:00:51,520 --> 00:00:55,870 As you can see, our variable names cannot start with a number here. 11 00:00:55,870 --> 00:01:05,770 So that's why I wrote the alphabetical two here and to to multiply by pi here. 12 00:01:05,800 --> 00:01:11,980 So this is similar initialization constant declared with the const keyword. 13 00:01:12,010 --> 00:01:20,230 However, the const expatriate expression keyword can also be applied to functions that return a value 14 00:01:20,230 --> 00:01:23,130 that can be evaluated at compile time. 15 00:01:23,140 --> 00:01:28,310 And so this allows the compiler to optimize the code. 16 00:01:28,330 --> 00:01:31,120 So I'm going to write another example here. 17 00:01:31,870 --> 00:01:32,680 Let's. 18 00:01:34,010 --> 00:01:37,610 A right triangle function here. 19 00:01:38,590 --> 00:01:39,070 Here. 20 00:01:40,040 --> 00:01:40,970 Const expression. 21 00:01:40,970 --> 00:01:47,090 So as you can see, this keyword can be used in functions here and integer. 22 00:01:47,090 --> 00:01:49,310 This takes integer as a parameter. 23 00:01:49,940 --> 00:01:59,510 Let's return E equals if E equals zero here, you will learn about this later. 24 00:01:59,690 --> 00:02:00,800 What is this? 25 00:02:01,970 --> 00:02:04,160 Just write it. 26 00:02:04,160 --> 00:02:10,940 And in next lecture you will learn the return statements and what these are. 27 00:02:11,570 --> 00:02:12,470 In next lecture. 28 00:02:12,470 --> 00:02:14,510 So don't worry. 29 00:02:14,510 --> 00:02:19,040 You will learn about all of them here and here. 30 00:02:19,700 --> 00:02:24,600 So in this example, the function triangle. 31 00:02:24,950 --> 00:02:25,920 Triangle. 32 00:02:26,720 --> 00:02:31,430 You'll calculate triangular numbers recursively. 33 00:02:31,430 --> 00:02:35,060 So the code uses the conditional operator here. 34 00:02:35,060 --> 00:02:42,510 So in this parenthesis, the function parameter is tested to see if it is a zero. 35 00:02:42,510 --> 00:02:43,190 Here. 36 00:02:43,200 --> 00:02:50,640 If it is zero, and if so, the function returns zero in effect, ending the recursion and returning 37 00:02:50,640 --> 00:02:53,450 the function to the original color. 38 00:02:53,460 --> 00:03:01,770 So if the parameter is not zero here, then the return value is the sum of the parameter and the return 39 00:03:01,770 --> 00:03:09,450 value of trying called with the parameter is decremented by one, as you can see here. 40 00:03:09,450 --> 00:03:15,750 So this function, when called with a literal in your code, can be evaluated at compile time. 41 00:03:15,750 --> 00:03:23,610 So the constexpr here is an indication to the compiler to check the usage of the function to see if 42 00:03:23,610 --> 00:03:27,150 it can determine the parameter at compile time. 43 00:03:27,150 --> 00:03:36,630 So if this is the case, the compiler can evaluate the return value and produce more efficiently than 44 00:03:36,630 --> 00:03:38,490 by calling the function at runtime. 45 00:03:38,490 --> 00:03:44,970 So if the compiler cannot determine the parameter, the compile time, the function will be called as 46 00:03:44,970 --> 00:03:46,050 just a normal. 47 00:03:46,050 --> 00:03:54,840 So a function marked with the constexpr here keyword must only have one expression and the use of the 48 00:03:54,840 --> 00:03:59,520 conditional operator here, as you can see here in the trunk function.