My prof gave us a question that states "Write a function according to the following specifications:
The function will attempt to calculate, print out, and return the reciprocal value of the argument.
-Return data type: double.
-Function name: reciprocal.
-Single parameter:
~Data type:double.
~Parameter name:value.
-Assuming the passed value is non-zero, calculate the reciprocal as 1/value; print out this value and then return it.
-Otherwise, the passed value is zero, print out "infinite" and return 0.0.
This is the code that I have so far. I built it and it had no errors, but when I compile it nothing shows on the output screen.
When you run your program, it looks for the main() function.
Your main function declares a variable called reciprocal then returns 0. It does not actually call your reciprocal function.
See if you can change your code to actually call that function, then you should see the "reciprocal" variable printed out in that cout statement.
Just in case you don't know, here's an example of a function and calling that function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int myFoo();
int main()
{
myFoo(); // This is a function call, and it prints out "HEY!"
int functionReturn = myFoo(); // This is the function call again. This time I save the return value.
return 0;
}
int myFoo()
{
cout << "HEY!";
return 0;
}
Just as a note, that code is just an example... it doesn't do anything useful with that function or variable.
That is what I am unsure of... My prof didn't tell us that. should I assume that is what he wants? I was assuming not so that is why I just set it equal to a random number, 30.
-Assuming the passed value is non-zero, calculate the reciprocal as 1/value; print out this value and then return it.
-Otherwise, the passed value is zero, print out "infinite" and return 0.0.
This sounds to me like the number is going to be input and you need to check if it's 0 or not (so you can avoid divide by zero errors)?