From the course: C++ Best Practices for Developers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Writing an integer sum method

Solution: Writing an integer sum method - C++ Tutorial

From the course: C++ Best Practices for Developers

Start my 1-month free trial

Solution: Writing an integer sum method

(upbeat electronic music) - [Instructor] How'd it go, did you figure out a solution? Hopefully, yes, it is somewhat similar to reverse concatenating the strings. Simply add the first to the remainders, and don't forget the base case. Let me show you how I did it. We begin with the base case, this is when we only have one argument, and all it does is return that argument, so it gets a V, sends a V back out. Next, we handle the recursiveness. Note how the parameters are split up between the first and the rest, so we get the first here, we get the rest of them, and we just say first, and then was say plus, and we give it back the argument pack. So it's going to go args star dot dot, and send it back, and it's going to keep splitting these up until there's only one, and then that's going to end at the recursid, and it's always adding itself together. And finally, call the function with a variable number of arguments, and so that's right here, we're calling it, and let's go ahead and run…

Contents