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++.
For this project, which I'd like you to call sumBuiltIn, I want you to write a function called sumArray() that takes two parameters. The first parameter is an array of integers, and it's size is the second Parameter. The function should also return a single integer. You should iterate through the array inside of the function, summing the integers stored in the array, and then return the sum when you're done. As a helpful hint, when you pass the array, you don't need to place its size in between the square brackets and the array parameter itself although you could. For simplicity's sake, just put the empty brackets. The second parameter will take care of the size. Use a regular for loop to solve this problem. And don't forget to call your function on an array that you create to make sure that it works from inside of the main function.
Let's take a look at how this works before you pause the video and dive in. All right, so here's the SumBuiltIn project and then I am not going to show the code, but if you see the debug, start without debugging, you can see what the output might look like depending on what are in the array, and I'll just say something like some of the elements in my array is 179 and that's all we need. So, I'd like you to create a project again called SumBuiltIn and pause the video, and see if you can solve this one. How did that work out? Were you able to solve the problem? Let's do it together. So, let's create the SumBuiltIn project, new project, empty project and then we'll call it SumBuiltIn create, in the few seconds and then we will create our main.cpp file as usual. Give it the skeleton, and what we have here return zero. Good. Now, we need to create the prototype for SumArray, so SumArray looks like this, and then int arraySize. That's what I'm going to pass in, and then I'm just going to copy and paste this because this will be the header of the function. So, inside of here, notice I took the semicolon off when I pasted it. I'm going to initialize the sum to zero and then ultimately we're going to return the sum. What happens in between is the important part. So, if they pass an array in it, that's at least got an element in it, we should be able to iterate through it right here, and then sum+= myArray[i]. And you'll note that this is the same as sum = sum + MyArray [i]. It's a shortcut syntax with the compound addition assignment operator if you recall.
Now, we have to call it from main in order to test it. So, we need some array to play around with. So, let's do this. I'll call it my array, again does not have to match the name that we have down here, but I'll call it myArray, it could be called sumArray, yourarray, otherarray, whatever you want it to be named 2, 5, 2, 10, 20, 15, 100 and we'll say 25. All right, so that's 1,2,3,4,5,6,7,8 elements. So, int sum = call sumArray() and myArray, and then pass an 8 in here. That's not necessarily the best convention to pass in a literal in this particular circumstance, we might have a constant called arraySize or something like that, but in this case, it's not being manipulated, nothing is being done to it, so it's okay. Now, we will get the sum of the elements, sum of the elements in myArray is sum, and lets start without debugging. See, what we get.
Sum of the elements in the array is 179. And if you count these up, 5, 6, 7, 8, 9, 19, 39 and then you have 139 plus 40 that's a 179. So, there we go. It did it correctly. That's all we really wanted. So, hopefully you did a good job on that was similar to what I did down here. You may have... There would have been a way to do it without passing in the size of the array. There's actually ways to do that. You could use a different type of for loop. But, since I said you use the regular for loop, so that you would know how to iterate through it based on the index, it's important to know how to do it based on the index, and also to use the enhanced or range-based for loop as well. So, in the next project, which is in the next lecture, we're going to be doing essentially the same thing, but we're going to be using an array object instead of a a built-in array. Let's get to work.
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.