Originally Posted by
polonyman
basically i dont c y int mult(int x, int y); is at the top and bottom only the bottom 1 doesnt have semicolons. be nice if u could explain it, cya
When the compiler gets to a piece of code that calls a function it likes to know a little about the function being called so it can do proper type checking of the arguments at that moment. In order to do this it either needs to have already read through the whole definition of the function before it reaches that particular part of the code, or it at least needs to know how the function gets called and what type of arguments it takes and what type of value it may return. The second method is done by the use of a function prototype which is demonstrated in that section of code you had. It typically looks just like a function definition except it is missing the body of the function and has the semicolon at the end. This is just letting the compiler know that "hey I'm going to be calling a function called mult later on and it takes two int arguments and it returns an int value as its result.
So, you can either fully define the function prior to its being called like this:
Code:
#include <iostream>
// Function fully defined prior to its being called below, no prototype needed
int mult(int x, int y)
{
return x*y;
}
int main()
{
int x, y;
std::cout<<"Please input two numbers to be multiplied: ";
std::cin>>x>>y;
std::cout<<"The product of your two numbers is "<< mult(x, y);
std::cin.get();
std::cin.get();
return 0;
}
Or you would need the function prototype:
Code:
#include <iostream>
// Function prototype needed otherwise compiler won't know about function
// when it gets to the calling of it in the code below
int mult(int x, int y);
int main()
{
int x, y;
std::cout<<"Please input two numbers to be multiplied: ";
std::cin>>x>>y;
std::cout<<"The product of your two numbers is "<<mult(x, y);
std::cin.get();
std::cin.get();
return 0;
}
int mult(int x, int y)
{
return x*y;
}
Originally Posted by
polonyman
it still wont work with cin.get(); to stop it disapearing. where do i put it or what else do i use?
See either of the above examples.