Where is the return value specified for your function, and what is that type?
int bigger( double x, double y );
If you can't answer that question, you need to reread the chapter in your book on functions. You may also want to read this:
Here is a very simple function:
Code:
#include <iostream>
using namespace std;
void showNumber()
{
cout<<100<<endl;
}
int main ()
{
showNumber();
return 0;
}
Functions must specify a 'return type', and the return type precedes the function name. In the function above, there is no return value, and that is designated by 'void'. The function is 'called' in main() by this line:
showNumber();
That says, "please go find a function named showNumber, and execute the lines of code contained therein."
Here is another example:
Code:
#include <iostream>
using namespace std;
void showNumber(int a)
{
cout<<a<<endl;
}
int main ()
{
showNumber(100);
showNumber(25);
return 0;
}
Now, instead of always displaying the same number, you can send the function a specific number, and the function will display whatever number you send it. Once again, the function does not return a value, so its return type is 'void'. You should note that the type of the number you send to the function must match the function 'parameter'. The function parameter above is "int a", which means the function is expecting an int type to be 'sent' to it. You send a value to a function like this:
showNumber(100);
To send a value to a function, you just put the value between the parentheses after the function name. The function parameter "int a" creates an integer variable named "a" which stores the value you send to the function--that's why the type of the value you send has to match the function parameter. Then, inside the function you can use the variable "a" by referring to it by name:
cout<<a<<endl;
Here is another example:
Code:
#include <iostream>
using namespace std;
int addTen(int a)
{
int answer = a + 10;
return answer;
}
int main ()
{
int result1 = addTen(2);
int result2 = addTen(5);
cout<<"Here are the results: "<<result1<<" "<<result2<<endl;
return 0;
}
This time the function returns a value. The return type preceding the function name must match the type of the value in the 'return statement'. The return statement replaces the function call in main(), e.g. addTen(2), with the return value. So, result1 is assigned 12, and result2 is assigned 15.
To summarize:
1) A function must list a return type before its name.
2) The return type must match the type of the value in the return statement--if there is no return statement, then the type is 'void'.
3) When you call a function, the value you send it must match the function parameter.
4) The function call in main() is replaced by the function return value.
5) Sometimes if a function doesn't have any parameters, like in the first example, "void" will be used for the parameter. For instance you might see the function in the first example written like this:
Code:
void showNumber(void)
{
cout<<100<<endl;
}
(void) is equivalent to ( )