In this lecture you will learn about the constant expressions in C plus plus.
So C plus plus 11 introduce a keyword called Constexpr here, const expression const expression Constexpr
here.
So this is applied to an expression and indicates that the expression should be evaluated at compile
time rather than at runtime.
So for example, let's create an override code here.
Const expression double pi 1415 and let's create another const expression, double expression expr double.
And here we're going to write two pi.
European.
As you can see, our variable names cannot start with a number here.
So that's why I wrote the alphabetical two here and to to multiply by pi here.
So this is similar initialization constant declared with the const keyword.
However, the const expatriate expression keyword can also be applied to functions that return a value
that can be evaluated at compile time.
And so this allows the compiler to optimize the code.
So I'm going to write another example here.
Let's.
A right triangle function here.
Here.
Const expression.
So as you can see, this keyword can be used in functions here and integer.
This takes integer as a parameter.
Let's return E equals if E equals zero here, you will learn about this later.
What is this?
Just write it.
And in next lecture you will learn the return statements and what these are.
In next lecture.
So don't worry.
You will learn about all of them here and here.
So in this example, the function triangle.
Triangle.
You'll calculate triangular numbers recursively.
So the code uses the conditional operator here.
So in this parenthesis, the function parameter is tested to see if it is a zero.
Here.
If it is zero, and if so, the function returns zero in effect, ending the recursion and returning
the function to the original color.
So if the parameter is not zero here, then the return value is the sum of the parameter and the return
value of trying called with the parameter is decremented by one, as you can see here.
So this function, when called with a literal in your code, can be evaluated at compile time.
So the constexpr here is an indication to the compiler to check the usage of the function to see if
it can determine the parameter at compile time.
So if this is the case, the compiler can evaluate the return value and produce more efficiently than
by calling the function at runtime.
So if the compiler cannot determine the parameter, the compile time, the function will be called as
just a normal.
So a function marked with the constexpr here keyword must only have one expression and the use of the
conditional operator here, as you can see here in the trunk function.