Value Returning Functions

Hello, having problems understanding value returning functions. Actually the assignment has students write a program that has 4 functions. I think I've got the (void menu function done...at least it compiles :-) I'm stuck with a value returning function that returns a double value and an int value based on user input and menu choice. Although the assignment calls for additional functions I do my best to work through the problems on my own and think with a bit of help I'll get the rest on my own. The function is supposed to do the following:

A void function named mostPowerful
o That accepts three Jetpack power output measurements in MW as double values.
o Returns the largest power output and the number of the Jetpack as parameters.
o Return a 1 for the number if the first Jetpack’s power output is the largest, a 2 if the second Jetpack’s power output is the largest, and a 3 if the third Jetpack’s power output is the largest.

When I choose M when prompted my program doesn't do anything even though I've called the mostPowerful function. Can someone point where my mistakes are or offer any tips that would make understanding the concept easier? Here is what I have thus far. Again much thanks for this site and to all of you that help us beginners out!!

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//Joe Snuffy 
//Assignment 11
/*The purpose of this function is to continue to 
develop knowledge in Functions and Reference Parameters.
The program calls 3 Functions of the type VOID and one function
that returns a character.*/

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;
//Function Prototypes
double mostPowerful(double pwrOutput, int jetPacks);

char menu();

int main()
{
		char userChoice = menu(); //call to menu function returning a character
		while (userChoice != 'Q')
		{
			cout<<"Please enter the option (M, D, H, or Q: ";
			cin>>userChoice;

			if(userChoice == 'M')
			{
				//function call to the most powerful function
				double mostPowerful(double pwrOutput, int jetPacks);
			}

		}
			

system("Pause");
return 0;
}


//Function Definitions
//Define call to menu selection first 

char menu()
{
	char userChoice;
	cout<<"Welcome to Joe Snuffy's Spaceship Company"<<endl;
	cout<<"(M)"<<"ost Powerful Calculation"<<endl;
	cout<<"(D)"<<"iscount Calculation"<<endl;
	cout<<"(H)"<<"ow Many Calculation"<<endl;
	cout<<"(Q)"<<"uit"<<endl;
	cout<<"Please Enter the option (M, D, H, or Q): "<<endl;
	cin>>userChoice;//user's input choice
	return userChoice;//return user choice 
}
//Function definition for mostPowerful function
double mostPowerful(double pwrOutput, int jetPacks)
{
	double num1, num2, num3;
	int packNbr1 = 1;
	int packNbr2 = 2;
	int packNbr3 = 3;
	cout<<"Enter three power output Measurments in Mega Watts (MW): "<<endl;
	cin>>num1>>num2>>num3;

	if(num1 > num2 && num1 > num3)
	{
		cout<<"The Largest Output is: "<<num1<<"and is Jetpack number: "<<packNbr1<<endl;
	}
	else if (num2 > num1 && num2 > num3)
	{
		cout<<"The Largest Output is: "<<num2<<"and is Jetpack number: "<<packNbr2<<endl;
	}
	else cout<<"The Largest Output is: "<<num3<<"and is Jetpack number: "<<packNbr3<<endl;
	
	return pwrOutput, jetPacks;
	}
Hi there,

Please allow me to clarify a few things about value-return functions.

Although functions are declared as such:

double mostPowerful(double pwrOutput, int jetPacks);

Which means "hey compiler, I'll be using a function called mostPowerful, it takes a double and an int as arguments and it will return a double.". That's declaration, actually executing (or calling) the function, is done as such:

mostPowerful(pwrOutput, jetPacks);

Also, since they return a value, you'll probably want to store that returned value in a variable:

double result = mostPowerful(pwrOutput, jetPacks);

Note that "result" has the same type (double) as the value returned by mostPowerful().



As for your function itself, your declaration states it will return a double. However, your funciton returns:

return pwrOutput, jetPacks;

I don't see your function doing any calculations for it to return, so perhaps a value return function might not be the best suited for this. A simple example would be:

1
2
3
4
5
6
7
8
9
int sum(int a, int b)
{
    return a+b;
}

int main()
{
    std::cout << "3 + 4 = " << sum(3,4);
}



As a final remark, on line 29 you try to call your function using pwrOutput and jetPacks as arguments - but they don't exist in main(). Either declare those in main as well, or pass in actual data.

For more information:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

Hope that helps, please do let us know if you have any further questions.

All the best,
NwN
Let's look at the original question:
A void function named mostPowerful
o That accepts three Jetpack power output measurements in MW as double values.
o Returns the largest power output and the number of the Jetpack as parameters.


Given that description, we can start to write the function declaration. Let's start with the return type (void) and function name:
 
void mostPowerful(

Now what about the parameters? Well, there are three input parameters of type double, let's call them input1, input2 and input3.
 
void mostPowerful(double input1, double input2, double input3

And what else do we need, maybe just a closing bracket ')' ? But wait, there are two more parameters which will be the return values. When we need to allow the function to change the value of a parameter, we pass it by reference using the & operator. If we call those outputPower and outputNumber, we end up with this:
1
2
void mostPowerful(double input1, double input2, double input3, 
    double & outputPower,  int & outputNumber);


Of course, you may want to choose more appropriate names for the parameters. Passing by reference is explained in the second link, http://www.cplusplus.com/doc/tutorial/functions2/
Last edited on
I'm still confused and have updated my code. Also, read the instructions more closely. The function is a void function and the instructions are the following:

A void function named mostPowerful
o That accepts three Jetpack power output measurements in MW as double values.
o Returns the largest power output and the number of the Jetpack as parameters.
o Return a 1 for the number if the first Jetpack’s power output is the largest, a 2 if the second Jetpack’s power output is the largest, and a 3 if the third Jetpack’s power output is the largest.

I've gotten quite a bit done but am getting some errors now. Thanks for clarifying some of my errors which I fixed. Does my updated function look correct?

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
31
32
33
//Function definition for mostPowerful function
void mostPowerful(double &powerOutput, int &jetPacks)
{
	//variables to hold user input
	double powerOutput;
	int jetPack;
	double power1, power2, power3;
	double mostPower;
	int packNbr1 =1;
	int packNbr2 =2;
	int packNbr3 =3;
	cout<<"Please enter 3 power-output measurements in MW: ";
	cin>>power1>>power2>>power3;
	//if else if statements to compare input
	if(power1 > power2 && power1 > power3)
	{ 
		mostPower = power1;
		power1 = powerOutput;
		cout<<"The largest power output is: "<<mostPower<<"and is Jetpack: "<<packNbr1<<endl;
		
	}
	else if(power2 > power1 && power2 > power3)
	{
		mostPower = power2;
		power2 = powerOutput;
		cout<<"The largest power output is: "<<mostPower<<"and is Jetpack: "<<packNbr2<<endl;
	}
	else if(power3 > power1 && power3 > power2)
	{
		mostPower = power3;
		power3 = powerOutput;
		cout<<"The largest power output is: "<<mostPower<<"and is Jetpack: "<<packNbr3<<endl;
	}
Remove line 5 you don't need it.

Line 6 you have declared an int jetPack and you have an int& jetPacks you don't appear to use.
allendks45 wrote:
Does my updated function look correct?

Simple answer: no.

There should not be any cin and cout statements in the function, it simply needs to assign the correct values to the two output variables.

You need to put the cin and cout outside this function. What i mean by that is, put them in whichever function will be calling this one.

That means your code would look something like this:
1
2
3
4
5
6
7
8
    double power1, power2, power3, mostPower;
    int packNbr;

    cout << "Please enter 3 power-output measurements in MW: ";
    cin >> power1 >> power2 >> power3;
    mostPowerful(power1, power2, power3 , mostPower,  packNbr);
    cout << "The largest power output is: "<< mostPower
         << " and is Jetpack: " << packNbr << endl;


1
2
3
4
5
6
7
8
9
void mostPowerful(double power1, double power2, double power3, 
    double & mostPower, int & packNbr)
{
    // if else if statements to compare input
    // assign largest power to mostPower 
    // and the corresponding number (1, 2 or 3) to packNbr

 
}



Certainly appreciate all the help. Not sure if working on this 5 hours is good or bad. I've worked through some of the problems and still have a ways to go. Will have to code 3 additional functions that will execute when user enters the character. I believe the one function is complete and it compiles but my code keeps prompting me the menu. I made changes at one point and it accepted the character 'M' but the program never prompted for input which i'm positive is needed to test the if else if statements correct?

Should I move all the calls to the additional functions in the menu function? That would mean the only statements in main would be the while loop condition. Dang I feel lost on this. Here is updated code...hopefully i'm closer to a solution. Jeez this assignment is difficult.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//Joe Snuffy 
//Assignment 11
/*The purpose of this function is to continue to 
develop knowledge in Functions and Reference Parameters.
The program calls 3 Functions of the type VOID and one function
that returns a character.*/

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

//Function Prototypes with reference variables as parameters
void mostPowerful(double power1, double power2, double power3, double &mostPower, int &packNbr);

//Function prototype for Discount 
void discountResults(double discountAmnt, double discountPrice);

//Function prototype for Howmany
void howMany(int nbrOfItems, int leftOver);

//Function prototype that returns a character
char menu();

int main()
{
	double power1, power2, power3, mostPower; //variables for mostpowerful function
	double price, disPercent, discountAmnt, discountPrice; //variables for discount function
	double fundsAvail, costItem; //variables for howmany function
	int packNbr; //variable for most powerful function
	int nbrOfItems, leftOver; //variables for howmany function
	char userChoice = ' ';
	
	while(userChoice != 'Q' && userChoice != 'q')
	{	
		menu();//call to menu function

		if(userChoice == 'M' || userChoice == 'm')
		{
			cout<<"Please enter 3 output measurements in MW: "<<endl;
			cin>>power1>>power2>>power3;
			mostPowerful(power1, power2, power3, mostPower, packNbr);
		}
	}//end while loop....there will be more calls to other functions based on userchoice

system("Pause");
return 0;
}
//Function Definitions
//Define call to menu selection first 

char menu()
{
	char userChoice = ' ';
	cout<<"Welcome to Joe Snuffy's Spaceship Company\n\n"<<endl;
	cout<<"(M)"<<"ost Powerful Calculation"<<endl;
	cout<<"(D)"<<"iscount Calculation"<<endl;
	cout<<"(H)"<<"ow Many Calculation"<<endl;
	cout<<"(Q)"<<"uit"<<endl;
	cout<<"Please Enter the option (M, D, H, or Q): ";
	cin>>userChoice;//user's input choice
	return userChoice;//return user choice
}

//Function definition for mostPowerful function
void mostPowerful(double power1, double power2, double power3, double &mostPower, int &packNbr)
{
	if(power1 > power2 && power1 > power3)
	{
		mostPower = power1;
		packNbr = 1;
	}
	else if(power2 > power1 && power2 > power3)
	{
		mostPower = power2;
		packNbr = 2;
	}
	else if(power3 > power1 && power3 > power2);
		mostPower = power3;
		packNbr = 3;
		
}
Last edited on
I'm short of time right now, so just a brief comment. That looks better, I think you're heading in the right direction.
Sorry, I've had a few things to attend to away from this site. Had another look at this now.

In main() at line 37,
 
    menu(); // call to menu function 

the result of calling the function needs to be assigned to a variable, like this:
 
    userChoice = menu(); // call to menu function 


After the call to mostPowerful() at line 43, there should be a cout statement to display the result. Notice the difference between this function call and the call to the menu() function. With the menu(), you need to assign to result to a variable (userChoice ), but in the case of mostPowerful(), there's no need to do that (in fact it's not possible. as there is no return value). Instead, the result is returned by modifying the values of the two parameters which were passed by reference.

One more comment, inside function mostPowerful(), there are a couple of errors. Line 79 should not end with a semicolon. The next two lines should be enclosed between braces { }. And lastly, line 79 doesn't actually need to do any additional checking, since if neither the first nor the second were the largest, the third must be the largest. So, the code ends up like this:
old:
79
80
81
    else if(power3 > power1 && power3 > power2);
        mostPower = power3;
        packNbr = 3;

new:
79
80
81
82
83
    else 
    {
        mostPower = power3;
        packNbr = 3;
    }


Hope this helps.
I'm going to work on it tomorrow due to a different class tonight. I see what you mean by the last recommendation. Originally the last statement was an else but someone better at this than me said to use the if else if statements. do you think I should repost my code in the same topic or start a new one? 60 hour work weeks and other classes don't make it easy to learn. We only get a little over a week to do each one and other requirements. Wish I lived near a computer lab with good programmers available to ask questions. Again.....really appreciate the help and input.
Code finally works! Although not fully complete as I still have to code two more functions but feel more confident. There is still confusion understanding what is happening within the code with the arguments and reference parameters. Not sure I quite understand what is happening with the value returned in the <menu function> and with the parameters in the <mostPowerful> function. Can someone explain these in simpler terms to give us the "very very beginners" a better understanding as we move on to more complex code? Here is the code that now works correctly. Please feel free to offer any comments.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//Joe Snuffy 
//CSCI 1010 W-1
//Functions Continued Assignment
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

// Function declarations, variables are passed by reference
void mostPowerful(double power1, double power2, double power3, double &mostPower, int &packNbr);

char menu(); //function prototype for menu function

int main()
{
	double power1, power2, power3, mostPower; //variables for mostpowerful function
	double price, disPercent, discountAmnt, discountPrice; //variables for discount function
	double fundsAvail, costItem; //variables for howmany function
	int packNbr; //variable for most powerful function
	int nbrOfItems, leftOver; //variables for howmany function
	char userChoice = ' ';
	
	while(userChoice != 'Q' && userChoice != 'q')
	{	
		userChoice = menu();//call to menu function
		if(userChoice == 'M' || userChoice == 'm')
		{
			cout<<"Please enter 3 output measurements in MW: "<<endl;
			cin>>power1>>power2>>power3;
			mostPowerful(power1, power2, power3, mostPower, packNbr);
			cout<<"The largest power-output is: "<<setprecision(2)<<fixed<<mostPower;
			cout<<"\t"<<"and the Jetpack number is: "<<packNbr<<endl;
			cout<<"================================================";
		    cout<<"========================\n\n"<<endl;
		}
	}//end while loop....there will be more calls to other functions based on userchoice

system("Pause");
return 0;
}
//Define call to menu selection first 
char menu()
{
	char userChoice = ' ';
	cout<<"Welcome to Joe Snuffy's Spaceship Company"<<endl;
	cout<<"(M)"<<"ost Powerful Calculation"<<endl;
	cout<<"(D)"<<"iscount Calculation"<<endl;
	cout<<"(H)"<<"ow Many Calculation"<<endl;
	cout<<"(Q)"<<"uit\n\n"<<endl;
	cout<<"Please Enter the option (M, D, H, or Q): ";
	cin>>userChoice;	//user's input choice
	return userChoice;//return user choice
	cout<<"\n\n";
}
//Function definition for mostPowerful function
void mostPowerful(double power1, double power2, double power3, double &mostPower, int &packNbr)
{
	if(power1 > power2 && power1 > power3)
	{
		mostPower = power1;
		packNbr = 1;
	}
	else if(power2 > power1 && power2 > power3)
	{
		mostPower = power2;
		packNbr = 2;
	}
	else 
	{
		mostPower = power3;
		packNbr = 3;
	}		
}
In terms of understanding what is happening, now that you've had a bit of hands-on experience with using functions, it's probably worth re-reading the tutorial pages on functions. Some points which may have seemed meaningless the first time you read them, may make more sense now.
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
Topic archived. No new replies allowed.