Alright everyone, I'm a little behind in my programming class and I missed the lecture on functions. I'm not asking for anyone to do my work for me but a little help on where to start would be nice. thanks :)

alright well here's the question...

Write three C++ functions named Sum (), product (), and average(). the sum() product() and average() functions will take two integer parameters. each of the three functions will compute and return the sum, product, and average of the two numbers. The sum() function will return and integer, the product() function will return and integer and the average() function will return a double.

Write another function called jupiter high() that will take one string parameter and ask you for the name of your high school. The function should then return the name of your high school.

Do Not display the computed value of any of the functions function, just return them. Then you will call each function from the main() function and display the result for each.

The output should look similar to this:

Please enter a value: 100
Please enter another value: 50

The sum of these integers is 150

Please enter a value: 10
Please enter another value: 50

The product of these integers is 500

Please enter a double value: 50.6
Please enter another value: 45.3

The average of these numbers is 47.95

Please enter the name of your high school. Jupiter High
The name of your high school is Jupiter High

And again, I'm not asking for you to do my work for me, I just want a little help on where i should start and what I'm supposed to do. Thanks everyone

9 Years
Discussion Span
Last Post by JDsmith

basically a function looks like this:

return_type function_name (type variable, type variable) 
     return return_type;

So, let's do the first one, sum. What does it need to return? I'd say an int (though a double is probably needed) So look at the return value... it needs to return an int so:

int sum(int value1, int value2)
     int result;
     // calculate sum here
     return result;

This says, the function returns an int, it's name is sum, and it takes 2 parameters, an int called value1 and an int called value2. You should be able to go from there.

EDIT: when you want to use the function from like, main. You need to call it like this:

int x = 5;
int y = 5;
int retval = sum(x, y); // retval will be the sum of 5 and 5

Alright thanks for the help, i couldn't understand the c++ tutorial site.


Just remember, you must either put this function above the main function, or you must put a function prototype above the main function (so main knows how it's supposed to look when you try to use it).


ok, thanks. this should help a lot with the 3 laps i have to makeup

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.