Homework help

I am having trouble understanding my teacher's explanation. I need help with this assignment.

3.Given the following coded function

double calcBonus( int sold, double bonusRate)
{
double bonusAmount = 0.0;
bonusAmount = sold * bonusRate;
return bonusAmount;
}

code the prototype and the function call.

I have had my teacher try to explain it a couple of times but I just am not understanding it. Please help!
You need to write a function prototype and then
call your function from main.

this is an example:

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
//functionBehavior.cpp
//implement a prototype and function call.
#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

void displayMessage(string message); //function prototype


int main(){

string message="Hello World!\n";

displayMessage(message); //function call

return 0; //indicates success
}//end main

//function implementation
void displayMessage(string message){
        cout<<message<<endl;
}//end function displayMessage 



Eyenrique-MacBook-Pro:C++ Eyenrique$ ./functionBehavior 
Hello World!


So in the example I gave then.

the prototype would be:

double calcBonus(int sold, double bonusRate);

the function call would be:

calcBonus(bonusAmount);


does this look correct?
Your function prototype is right but,
function call NO. calcBonus(bonusAmount);


 
calcBonus(sold,bonusRate);


Think that, your function calcBonus expects sold and bonusRate variables to
calculate bonusAmount.
Last edited on
I have another problem for you to look at please:

given the following data and prototype

double average = 0.0;
double calcAverage (double, double);

and given the following sample function call

average = calcAverage (34.3, 43.3); // any two double values as parameters

code the function named calcAverage( )

Here is what I came up with:

double calcAverage(double, double)
{

double average = 0.0;
cout<<"enter two numbers";
cin>>x >>y;
average = x * y / 2;

}






You need to do a return statement within the function.

1
2
3
4
double calAverage(double x, double y)
{
     return (x * y) /  2;
}


When you call a data type in the front, that isn't void, you must return a value that is that data type. So with double in front of the function name, you must return a number that is a double.

Here is what it would look within a piece of code:

1
2
3
4
5
6
7
8
9
10
//the function that was created previously goes here
int main()
{
   double average = 0.0;
   cout << "enter two numbers: ";
   cin >> x >> y;
   average = calAverage(x, y); //this puts the two values within the function then sets the return value into average
   cout << average; //shows average
   return 0; //end program
}
I can figure out this:

1
2
3
4
5
6
double calcAverage(double value1,double value2){
double sum;
sum=value1+value2;

return sum/2;
}//end function calcAverage 
Topic archived. No new replies allowed.