I have this program that adds powers of numbers and for some reason it keeps throwing an error at me when I run it, the program runs fine if I declare and define the contents before the main function but I don't understand why that's necessary... Here's the code that's giving me problems:
#include <iostream>
#include <math.h>
using namespace std;
long long addPow(int n, int p);
int main() {
cout << addPow(100, 1) * addPow(100, 1) - addPow(100, 2) << endl;
return 0;
}
addPow(int n, int p) {
long long sum = 0;
for (int i = 1; i <= n; i++) {
sum += pow(i, p);
}
return sum;
}
changing it to this fixes it all, but I don't really know why...
#include <iostream>
#include <math.h>
using namespace std;
long long addPow(int n, int p) {
long long sum = 0;
for (int i = 1; i <= n; i++) {
sum += pow(i, p);
}
return sum;
}
int main() {
cout << addPow(100, 1) * addPow(100, 1) - addPow(100, 2) << endl;
return 0;
}
If someone could help me out I'd really appreciate it!
The function in the first code block, defined as
addPow(int n, int p) {
needs the extra information (namely the return type) you put into the prototype. It should look like this:
long long addPow(int n, int p) {