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++.
This project should be a pretty simple one for you if you feel you did well in our challenges throughout the section. So, here's what I'd like you to do. Create a project called ProductOfThree and write a function called multiply. The function will take three parameters, all integers and will return the integer product of those three parameters. Recall that the product means we are multiplying all three of these parameters. Let's look at how this program works before you pause the video and work on it on your own. So, here is the ProductOfThree program. If we go to debug, start without debugging, we get the solution is 100. So, for example, a 5 times a 10 times 2 would yield 100. Whatever the three arguments that you pass into the function, it should yield the product of all three of those. So, it's very simple output. Okay, now that we've seen how it works, pause the video and come back when you think you're done or if you need a little help. How'd that go for you? Let's do it together and see what we get. So, we're going to create a new project. Make sure it's empty and we will create the ProductOfThree project. Hit 'Create' and then we are going to of course add our main file, new item cpp, main.cpp. And we have include iostream using namespace std, int main, and now we have to create the at least the skeleton of our function here. So, multiply and it takes three parameters. I'm just going to call them a, b, and c but num1, num2, num3 or whatever you'd like would be fine. Alright. And int multiply, int b, int c, and right here. Very simple. We just returned the product of all three of them. Nice. Inside of main, we will grab the solution. Let's say 5, 10, and 2 so we'll get 100 as the solution. 5 times 10 is 50 and then times 2 is 100 and then we will print out the solution. So, we'll say, Solution, oops, might want to spell it right. "Solution is" and then solution. Great. Let's run it and see what we get. Solution is 100. Awesome. Looks pretty good to me. Also note that it's okay if your solution wasn't exactly like mine. For example, some of you might have declared a local variable and multiply, stored the product of the three parameters and then return the local variables value instead of putting the multiplication expression directly on the return statement, and that's okay too. Note that while I use the integer type a lot in our examples, challenges, and projects because it makes the solution simple, in many of them we could just as easily use float double or long, int or something else. So, keep that in mind as you solve other problems. In the next project, we will be working on summing the values of a built-in array. We'll be combining some of what we learned about arrays previously with what we've learned about functions in this section. Let's get going.
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.