help!!!!

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

double reciprocal (double);

int main ()
{
     double reciprocal; 

     return 0;
}


double reciprocal (double value)
{
     double reciprocal; 
     value=30;

     reciprocal=1/value;

     cout << reciprocal << endl;

     return reciprocal; 
}


If someone could point me in the right direction I would greatly appreciate it! Thank you!
Last edited on
line 8 doesn't call the function - it appears to declare a variable named reciprocal.

You call a function by using functionname(parameters here).

Are you supposed to be collecting a value from the user in the main function and passing that as a parameter to the reciprocal function?

Edit: and if you're supposed to print out the function result in main instead of the function, you can cout << functionname(parameters here) in main.
Last edited on
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.
Thank you guys so much! I understand what I did wrong now!! :)
Ok so maybe I am still not understanding this... I changed my code to the following and it won't build now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

double reciprocal(double);

int main()
{
	reciprocal (value);

	return 0;
}

double reciprocal(double value)
{
	double reciprocal;
	value = 30;

	reciprocal = 1 / value;

	cout << reciprocal << endl;

	return reciprocal;
}


These are the errors that it shows:
Error 1 error C2065: 'value' : undeclared identifier
Error2 IntelliSense: identifier "value" is undefined
line 8 - you haven't declared value as a variable yet in main.

Is the user supposed to enter this value?
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)?
Ok well this is what I have now. It seems to work it just doesn't print out "infinite" which I am not too worried about.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

double reciprocal(double);

int main()
{
	double value;
	cout << "Please enter a value that is not zero.\n";
	cin >> value;
	if (value == 0)
	{
		cout << "Please reenter a value that is not zero.\n";
		cin >> value;
	}
	reciprocal(value);

	return 0;
}

double reciprocal(double value)
{
	double reciprocal;

	reciprocal = 1 / value;

	cout << reciprocal << endl;

	return reciprocal;
}
Thank you so much for your help! I think I have it down now!
Topic archived. No new replies allowed.