COSC175 functions: void vs value-returning · void function: name

advertisement
COSC175 functions: void vs value-returning


void function: name begins with a verb
vale-returning function name is noun or adjective describing the returned result
Is each of the following void or value-returning?
1. DisplayMenu
2. CalcSquare
3. Square
4. CalcSum
5. Sum
6. GetDimensions
7. PrintDimensions
8. CalcPerimeter
9. Perimeter
10. Max
void
void
value-returning
Write the declaration for each:
1. DisplayMenu
2. CalcSquare
3. Square
4. CalcSum
5. Sum
6. GetDimensions
7. PrintDimensions
8. CalcPerimeter
9. Perimeter
10. Max
void DisplayMenu
void CalcSquare
int Square
void function
Name begins with Verb
May return 0, 1, or many values as arguments
General purpose
Standalone call
value-returning function
Name usually a noun or adjective
Returns 1 value only
Usually mathematical
Call is part of an expression
Some example value-returning functions:
Min, Max, Cos, Sin, Sum, Avg, Diff
Some example void functions:
ShowDiff. CalcAvg, GetIput, ValidateDate, DisplayHeader
Arguments:
 Formal arguments are declared with the function
 A type (int, float, char, etc) must be included with the formal argument
 There may be 0, 1, or many arguments
 Specify arguments as /* in */ /* out */ /* in-out */
 In - pass by value
o passed into the function
o default
o values are known before calling the function
o values are not changed inside the function
o example: DisplayName (/* in */string name)
o


Out - pass by reference
o passed out of the function
o values are not known before calling the function
o values are known after calling the function
o values are changed inside the function
o example: InputName (/* out */string& name)
In/Out - pass by reference
o passed into and out of the function
o values are changed inside the function
o example: IncrementX (/* in/out */ int& x)
Finish the declarations from above, fully specifying each argument
1. DisplayMenu - none
2. CalcSquare – num,square
void DisplayMenu()
void CalcSquare ( /* in*/ int num,
/* out*/ int& square)
int Square (/* in */ int num)
3. Square – num
4. CalcSum -num1,num2, sum
5. Sum - num1,num2
6. GetDimensions - length,width
7. PrintDimensions - length,width
8. CalcPerimeter - length, width,perim
9. Perimeter-length,width
10. Max - num1,num2
Function Definition
 Include declaration and body
 body should contain code to implement what their name describes (no more – no less)
 value-returning functions must have return statement
 Any variables used must be declared as arguments or local variables
Write the code for the functions/procedures
1. DisplayMenu
void DisplayMenu()
{
cout << “Choose a menu option” << endl;
cout << “1. Spanish” << endl;
cout << “2. French” << endl;
cout << “3. German” << endl;
cout << “4. English” << endl;
cout << “5. Exit” << endl;
}
2. CalcSquare
void CalcSquare(/* in */int num,/* out */ int& square)
{
square = num * num
}
3. Square
int Square (/* in */) int num)
{
int result;
result = num * num
return result
}
4. CalcSum
5. Sum
6. GetDimensions
7. PrintDimensions
8. CalcPerimeter
9. Perimeter
10. Max
Calling a Funtion
 To call a function, you send actual arguments
 Actual arguments do not need to have the same name as the formal arguments
 Formal arguments and actual arguments must match in number and in type
 void function calls are standalone:
exa:
DisplayMenu();
CalcSquare(x, square) ;
CalcCalcSquare(5,ans);
 Function calls are part of an expression, the value returned must be used
Exa:
cout << “The square is “ Square(x);
cout << “The square is “ Square(5);
x = Square(5);
y = 2 * Square(x) + x + 3;
Show a sample call to each of the procedures/functions you have implemented above:
1. DisplayMenu
2. CalcSquare
3. Square
4. CalcSum
5. Sum
6. GetDimensions
7. PrintDimensions
8. CalcPerimeter
9. Perimeter
10. Max
DisplayMenu();
CalcSquare(x, square);
cout << “The square is “ Square(a);
x = Square(5);
or
Sample Problem:
ProcessSalaries
ReceiveSalary
CalcTaxRate
CalcIncomeTax
int main()
{
float salary, taxRate, tax
ReceiveSalary(salary);
while (salary <> 0)
{
CalcTaxRate(salary, taxRate);
CalcIncomeTax(salary,taxRate,tax);
cout << “For Salary of “ << salary << “ Tax rate is “ << taxRate << “ and tax is
“ << tax ;
ReceiveSalary(salary);
}
return 0;
}
//*****************************************************
void ReceiveSalary(/* out */ float& salary)
{
cout << “Enter Salary, 0 to exit” << endl;
cin >> salary;
}
//************************************************************
void CalcTaxRate (/* in */ float salary, /*out */ float& taxRate)
{
if (salary >= 40000)
taxRate = .3;
else if (salary >= 30000)
taxRate =.25;
else if (salary >= 20000)
taxRate = .2;
else if (salary >= 10000)
taxRate = .15;
else if (salary >= 5000)
taxRate = .06;
else
taxRate = 0;
}
//****************************************************************
void CalcIncomeTax(/* in */ float salary, /* in */ float taxRate, /* out */ float& tax)
{
tax = salary*taxRate;
}
ProcessSalaries
ReceiveSalary
CalcTaxRate
IncomeTax
int main()
{
float salary, taxRate, tax
ReceiveSalary(salary);
while (salary <> 0)
{
CalcTaxRate(salary, taxRate);
cout << “For Salary of “ << salary << “ Tax rate is “ << taxRate << “ and tax is
“ << Tax(salary) ;
ReceiveSalary(salary);
}
return 0;
}
//*****************************************************
void ReceiveSalary(/* out */ float& salary)
{
cout << “Enter Salary, 0 to exit” << endl;
cin >> salary;
}
//************************************************************
void CalcTaxRate (/* in */ float salary, /*out */ float& taxRate)
{
if (salary >= 40000)
taxRate = .3;
else if (salary >= 30000)
taxRate =.25;
else if (salary >= 20000)
taxRate = .2;
else if (salary >= 10000)
taxRate = .15;
else if (salary >= 5000)
taxRate = .06;
else
taxRate = 0;
}
//****************************************************************
float IncomeTax(/* in */ float salary, /* in */ float taxRate)
{
tax = salary*taxRate;
return tax;
}
/
Problem:
Write a modular program that calculates Body Mass Index using the following formula:
BMI =
Weight in Pounds
----------------------------------------------- x 703
((height in inches) * (height in inches))
A BMI below 18.5 indicates Underweight
18.5 – 24.9
Normal
25.0 – 29.9
Overweight
30.0 and Above
Obese
The program will run until a weight of 0 is entered.
To solve the problem:
1. Problem definition
 Identify tasks:
 ProcessBMIs (main), InputHeight, InputWeight,
ConvertHeightToInches, BMI, ShowHealthStatus
 Draw a hierarchy chart
2. Write code
 ProcessBMIs - include a loop
 InputHeight
 InputWeight
 ConvertHeightToInches
 BMI
 ShowHealthStatus
Parameter Passing Practice
What is the output?
What is the paramter passing method used?
Identify the formal arguments.
Identify the actual arguments.
#include <iostream>
void DoCrazyStuff(int x,int y);
using namespace std;
int main()
{
int x = 1;
int y = 2;
cout << x << " " << y << endl;
DoCrazyStuff(x,y);
cout << x << " " << y << endl;
return 0;
}
void DoCrazyStuff(int a, int b)
{
cout << a << " " << b << endl;
a = 3;
b = 4;
cout << a << " " << b << endl;
}
What is the output?
What is the paramter passing method used?
#include <iostream>
void DoCrazyStuff(int& x,int& y);
using namespace std;
int main()
{
int x = 1;
int y = 2;
cout << x << " " << y << endl;
DoCrazyStuff(x,y);
cout << x << " " << y << endl;
return 0;
}
void DoCrazyStuff(int& a, int& b)
{
cout << a << " " << b << endl;
a = 3;
b = 4;
cout << a << " " << b << endl;
}
What is the output?
#include <iostream>
void DoCrazyStuff(int& x,int y);
using namespace std;
int main()
{
int x = 1;
int y = 2;
cout << x << " " << y << endl;
DoCrazyStuff(x,y);
cout << x << " " << y << endl;
return 0;
}
void DoCrazyStuff(int& a, int b)
{
cout << a << " " << b << endl;
a = 3;
b = 4;
cout << a << " " << b << endl;
}
What is the output?
#include <iostream>
void DoCrazyStuff(int x,int& y);
using namespace std;
int main()
{
int x = 1;
int y = 2;
cout << x << " " << y << endl;
DoCrazyStuff(x,y);
cout << x << " " << y << endl;
return 0;
}
void DoCrazyStuff(int& a, int& b)
{
cout << a << " " << b << endl;
a = 3;
b = 4;
cout << a << " " << b << endl;
}
Download