- Consider again our example function
|
#include <iostream>
using namespace std;
int add(int a, int b) {
int sum = a + b;
return sum;
}
int main() {
cout << "Enter two numbers to add: ";
int num1, num2;
cin >> num1 >> num2;
int total = add(num1, num2);
cout << "Sum=" << total << endl;
return 0;
}
|
- Note the placement of the curly braces
- There are two common styles of curly brace placement for functions:
- Place the opening brace on the same line as the function heading:
int myFunction() {
// statements of the function
}
- Place the opening brace under and lined up with the first letter of the return type:
int myFunction()
{
// statements of the function
}
- We can use either style as long as we are consistent
- Also notice the indentation of the statements inside the function
- As before, we always indent 3-4 more spaces after an opening curly brace
- After the closing curly brace, we no longer indent the extra 3-4 spaces
- Indenting makes it easier to see the block of code
- In addition, function names always start with a lower case letter like variables
- We can tell the difference between function and variable name because functions have parenthesis
Activity 16.2: My Salary (10 pts) - Open up CodeBlocks and create a new C++ file called salary.cpp.
- Copy and paste the starter code below into the file.
- NOTE: Do not change the starter code! All you need to do is add to it.
/* * Name
* Section
*/
#include <iostream>
#include <iomanip>
using namespace std;
//write your functions here
int main() {
int hours; double hourly_wage;
double weekly_salary = 0;
double monthly_salary = 0;
double yearly_salary = 0;
cout << "This program will calculate your weekly, monthly and yearly salary!\n";
cout << "Please enter your hourly wage: ";
cin >> hourly_wage;
cout << "Please enter the number of hours you work each week: ";
cin >> hours;
//call functions below weekly_salary = //function call goes here monthly_salary = //function call goes here yearly_salary = //function call goes here cout << fixed << setprecision(2);
cout << "You make $" << weekly_salary << " per week.\n";
cout << "You make $" << monthly_salary << " per month.\n";
cout << "You make $" << yearly_salary << " per year.\n";
}
- Now write three functions as follows and place them above main:
Takes in an integer argument for the number of hours worked each week
Takes in a double argument for the hourly wage
Returns a double for the weekly salary
Takes in an integer argument for the number of hours worked each week
Takes in a double argument for the hourly wage
Returns a double for the monthly salary
Takes in an integer argument for the number of hours worked each week
Takes in a double argument for the hourly wage
Returns a double for the yearly salary
Assume that you work 50 weeks a year (2 weeks vacation)
- Add a function call and store the result as the appropriate variable.
weekly_salary = //function call goes here monthly_salary = //function call goes here yearly_salary = //function call goes here
- Now, run your code and verify that it prints the correct values for each salary type.
- When you are finished, upload your file to Catalyst.
- Your output should look identical to the following (except user input will vary):

|