Reputation: 542
I keep getting an error message in CodeBlocks it says:
Error: 'Addnumbers' was not declared in this scope
I just started with C++ and have no idea what this means. Here is my code:
#include <iostream>
using namespace std;
int main()
{
int fnum;
int snum;
cout << "Enter First number" << endl;
cin >> fnum;
cout << "Enter Second Number" << endl;
cin >> snum;
Addnumbers (fnum, snum);
return 0;
}
int Addnumbers(int fnum, int snum){
int ans = fnum+snum;
return ans;
}
Upvotes: 1
Views: 190
Reputation: 409196
You need to declare the function before it's used:
int Addnumbers(int fnum, int snum);
int main()
{
}
int Addnumbers(int fnum, int snum)
{
// ...
}
The first declaration is what is called a prototype, and tells the compiler that somewhere there is a function named AddNumbers
with the specified arguments and return type. Then you can have the definition anywhere, even in another source file.
In C++ (as well as in C or other languages base on C) everything must be declared before it it used. That's how the compiler will know that stuff exists.
Upvotes: 2
Reputation:
You need to either move Addnumbers
before main, or to do a forward declaration:
#include <iostream>
using namespace std;
int Addnumbers(int fnum, int snum);
int main()
{
Upvotes: 1