recursion function

Well I am learning about recursion. The exercise is I have to code a recursive function named sumSquares that returns the sum of squares of the numbers from 0 to num.

I have most of my code done (or so I believe), but I can't seem to get the last part in. I can't seem to get the sum :/
 
edit :D


I was also testing out reference variables (just to get more experience with them). I think I got that part down.

I'd appreciate any tips in the right direction! Thanks.
Last edited on
OH! Never mind, I figured it out :D!!

I'd still like some tips on how I did with my code and how I can improve it.
close the feed if you are done, otherwise if you were working with decimals, may I suggest that you use a double instead of an integer.

You can also save space by compressing the code lines
EX:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
double firstNumber,sum;
double sumSquare(double firstDigit); ///recursive function prototype
int main()
{
	cout<<"Enter a number: \n> ";
	cin>>firstNumber;
	cout<<endl;
	sum=sumSquare(firstNumber);
	cout << "Sum of the squares of number " << sum << std::endl;
	return 0;}
double sumSquare(double firstDigit) ///recursive function
{
if (firstDigit == 1)///my base
return firstDigit;
else
return firstDigit * firstDigit + sumSquare(firstDigit - 1); ///call to sumSquare function
}
Last edited on
Yeah I thought of using double, but the exercise calls for nonnegative int. Thanks for the advice! I'll go ahead and clean it up.

Is my math correct by the way?
Topic archived. No new replies allowed.