The course is part of this learning path
Functions in C++ are reusable named pieces of code that we can call or invoke when we need them to do something. They help us to take large problems and break them down so that they are more manageable. This course explores functions and puts them to use in a range of projects.
- Beginner coders, new to C++
- Developers looking to upskill by adding C++ to their CV
- College students and anyone studying C++
To get the most out of this course, you should have a basic understanding of the fundamentals of C++.
In the last several lectures, we've explored how to define our own custom user-defined functions. In this lecture, we will explore one of the libraries that is full of so called built-in functions, that are defined for us externally by the wonderful developers who maintain the C++ language itself as well as its many libraries. We specifically we'll be looking at some of the functions of the cmath library which contains, as the name suggests, many different math related functions. As software engineers and developers, we will use Math in our code frequently throughout our careers. You may end up writing a variety of different software during your career and Math will follow you throughout. Mathematical data and functions are used in the geometry of graphics and game engines. Data science applications use a ton of statistical functions and probability as well. Server software might need to do latency or load balancing calculations to make efficient use of resources. There are literally endless possibilities and opportunities to use math functions. Even though the cmath library contains many functions, including functions for trigonometry and other complex calculations, we will limit ourselves to just a few here including: square root, for finding the square root, pow for finding a base taken to a power, ceil for rounding a value up to the nearest integer and floor for rounding a value down to the nearest integer. So, let's create a project called MathFun. Create a project, empty project, and we will call it MathFun. So, we will create our starting file as usual, main.cpp. I will fill in the skeleton, namespace standard, int main, and we of course we'll need the cmath library as well. So, let's do that. Okay? All right, good. Now, let's fill in some of the code here. So, we're not going to create our own functions except for main, we're going to use some of the defined functions that are in cath. So, int powResult = pow 2^3. We're going to say square root result = the square root of 25. So, these names were given to the functions by the C++ developers themselves. So, we just call them as we would call ours. So, take the ceil of 4.2, should be ceilResult, it doesn't really matter, it wasn't a valid identifier, but name wise this makes more sense. And now we have floorResult equals the floor of 4.2. We're going to print out some stuff now. So, 2^3 is powResult, end now. We're going to say that the square root of 25 is sqrtResult. Okay. We're going to print that the ceiling of 4.2 is ceilResult. And I see a little bit of an error here. Square root, sqrt not sqrl, it's not squirrel result, okay? And now we have the floor of 4.2 is floorResults. Excellent. Alright, and we of course have to return zero. So, let's let's run it. All right. So we'll look at each of these in turn, we've got the power based to the exponents. So, the first could be a double and then another double, but here we're just using integers, but since they're compatible and actually there's a version available now that takes any type really. So, we've got any of the integer types. So, we've got 2^3, that is 8. The square root of 25 is 5 in fact, ceiling of 4.2, remember, that finds the closest integer above the floating point or double value that we have here. So, that's a 5. The floor of 4.2 is a 4. So, that's what happens when we print them out of course. But when we call them, that's the values that are stored. Pretty cool. Note that you can also pass in doubles to the pow and square root functions like I had mentioned. And you can capture doubles as the return values as well. So, there are many more functions available through the cmath library and other libraries, and I encourage you to check them out. A really good reference I want you to be aware of is the cplusplus.com. So, cplusplus.com website. It's a very good reference. And you can go up to the search and type cmath or just use Google and type cplusplus.com or even just type cathcplusplus and you're going to find this as one of the first page results almost for sure. So, you've got all these different functions and they're categorized. You can click on each of them and see how they work. So, the hyperbolic, trigonometric functions, regular trig functions, exponential and logarithmic, power functions, lgamma, rounding and remainder, floating point, minimum, maximum, absolute values. There's all kinds. So, it's really good reference. And of course, you can go to any of the other libraries that you'd like as well. So, excellent. So, now I'm going to stretch your skills a little bit by making use of the functions in this reference that we didn't cover in the lecture. In fact, this is one of the more common tests that you'll be doing as a student or even a professional, that is looking at reference material. So, there are so many different pieces of this puzzle we call programming that it's impossible to know everything. But knowing how to look up information is very useful and makes your life easier. And some of the libraries you might want to use may not be standard libraries that are built-in C++, you may get them from inside your own company or from another company. The sky's the limit. Someone else might have developed a bunch of functions and then that company sells their services or their functions basically for other companies to use. So, here's a challenge for you, you can use the same project that we're working in right now or make your own, make a new one. Doesn't really matter. Just make sure to include the cmath library if you create a brand new project. We've already got ours here. What I want you to do, is I want you to use the log2 function that you can look up if you need to in the cmath section of the C++ programming website or other websites that are good references, if you like another website, that's fine. So, you're going to use the log2 function to find the binary logarithms of 512, and then you're going to print that result to the console. If you recall, logarithms reverse exponentiation. So, if I asked you, to what power would you take the number 2 to get the value 8? You would respond 3, because 2^3 is 8. That's two cubed. So, that's what the logarithms tell us. The binary logarithms, log2 tells us the power to which we take the base 2 to get the number that you pass in. In our case, 512. To what power do you take 2 to get 512? That's the question we're asking with this function. Make sure to print out the result as well. So, pause the video and try this challenge. And remember, feel free to use cplusplus.com or any other site that you want to to find out how does the log2 function work. [no audio] Did you finish this challenge? This one stretch your skills in a direction we haven't done much in yet. Being able to research functions, we have not given an example for. I hope you were able to accomplish this challenge. Let's do it together regardless. So, what we're going to do is very simply. We're just going to go to the bottom here, these others and I'll say int logResult equals log2 of 512, see, that's all there is to it. And then I'm going to print it out down here. I'll say log2 of 512 is? And then print out logResult. Excellent. So, let's see what happens when we run it of course. So, the debug start at debugging. And we're interested in this last one. So, we have 2^9 is 512 and you can verify that if you'd like. So, that wasn't super difficult, I hope. In the next lecture, we will be talking about recursion, where a function can call itself to solve a problem. I'll see you there.
John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.