Monday, October 19, 2009
Functions... 101... for tech dummies... my notes
What is a Function?
A subprogram that can act on data and return a value.
*main() is a function. When your program starts, main() is automatically called and run. There can be multiple functions within main().
*global functions -- functions not part of an object.
*each function has its own name. When that name shows up in the code, the function executes.
***well designed functions perform a single, specific, and easily understood task, identified by the function name.
*built-in functions: part of compiler package
*user-defined functions: defined / written by user (duh)
===================
Return Value: result of work done by a program or function
int MyFunction(); //will return an integer value
int myFunction(int someValue, float someFloat); //will take two values and return an int.
Parameter List: description of values you send into a function (the value/number types, int and variable name, or float and variable name (may be other options but these are all I know so far))
Arguments: values (numbers, etc) passed through a function
int theValueReturned = myFunction (5, 6.7);
myFunction is the function, values 5 and 6.7 are the arguments, and the type of arguments must match the declared parameter type -- (int someValue, float SomeFloat);
====================================
prototype: declaration of a function (calling the function to run within the program)
3 ways to declare a function:
-1 write your prototype into a file and use #include to include it in your program
-2 write the prototype into the file in which your function is used.
-3 define the function before it is called by any other function. The function acts as its own prototype using this method. (But don't do this because it requires functions to be in a particular order which ruins most of the point of using functions in the first place.)
Function prototype: a statement that consists of the function's return type and signature (the name and parameter list for the function)
- if you don't want your function to return a value you can write void in front of it (I don't understand why you'd want to do that, but i find out I'll let you know)
Function definition: function header and body. the header looks like the function prototype except the parameters must be named and no semicolon at the end is used. The body is the set of statements enclosed in braces.
=========================
According to my professor, the proper way to write a function is:
1) write function call
(call for the function you haven't written yet)
2) draw box with parameters and return type
3) write prototype AND comment
4) write the full function definition
========================================
Ok... now I'm going to try to tackle my assignment again now that I understand functions a little more.
Tuesday, October 13, 2009
Getting Started on my "Functions" Assignment
Before I get into it, a HUGE shout out to my friends Sam & Brian who helped me figure out some of the final bugs in my last program. I got 5/5 on the assignment. (Yeay) -- my professor noted that my indentation was not accurate and I had one redundant note to the program, but she led that slide this time. Phew.
Ok, so this week's assignment, posted in the last entry, is about functions.
I am supposed to write a program that --
1) asks user how much she will put into savings account
2) asks user what the annual interest rate is
3) ask user how long she'll be leaving the money in the savings account
4) tell the user how much money they will have at the end of that period
Ok, sounds simple enough. I could do that WITHOUT functions, but that's not the assignment. So...
My program must define and use a function that performs this calculation.
According to the formula to do that given in class yesterday, I need to...
1) write function call for the function you haven't written yet
2) draw box with the parameters and return types
3) write prototype AND comment for what it's doing
4) write the full function definition
(half of that is still not English to me, so I'm going to take this one step at a time.)
Of course in the assignment, my teacher gave us the prototype (which is supposed to be STEP 3) so... I don't know if that's good or bad. The whole assignment instructions contradicts the professors order of doing this. Eeks.
OK, I'm just going to do this the way she said to in class... because this type of question is apparently going to be on the midterm.
1. Write a function call for the function you haven't written yet
First... what's a function call?
The way to use a function is with a "function call" expression, which consists of the function name followed by a list of "arguments" in parentheses. Another explaination: A function call is an expression containing the function name followed by the function call operator, (). If the function has been defined to receive parameters, the values that are to be sent into the function are listed inside the parentheses of the function call operator. The argument list can contain any number of expressions separated by commas. It can also be empty.
Say What?
Ok. So, what are parameters?
"Parameters provide a simple mechanism for defining and setting the valuesof inputs that are required by a form at startup. Form parameters are variables of type char,number,date that you define at design time." -- GeekInterview
Or, from my class notes: "parameter is a local variable that gets initialized with the function call"
(Still greek to me, but let's move on...)
Sunday, October 11, 2009
Next week's assignment
Write a reusable C++ class
Material from: Day 5, 6; String handouts starting on page 29.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Part 1)
Write a program that asks the user how much she will put into a savings account, what the annual interest rate is, and what period of time she plans to keep the money in the savings account. After these values are read in, report back to the user how much money she will have in her account at the end of the period.
The following formula calculates how an investmentAmount grows when left in a bank account for some years which compounds interest monthly at monthlyInterestRate:
futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate) years*12
Your program must define and use a function that performs this calculation.
Here is the prototype, and you must write the comment telling what the function does:
double futureInvestmentValue(double investmentAmount,
double monthlyInterestRate, int years);
Prompts and messages must be in CORRECT ENGLISH. The main() is the only one that reads and prints. There are no cin or cout statements inside the function that returns the futureInvestmentValue().
Plan of Attack
1) Write a function with the following prototype:
double futureInvestmentValue(double investmentAmount,
double monthlyInterestRate, int years);
First thing to do is write a comment for this function.
Before proceeding to step 2, you must have your main() call
futureInvestmentValue(1000, 3.25/100/12, 1)
An annual interest rate of 3.25% is equivalent to a monthlyInterestRate of 3.25/100/12.
The function call above must return 1032.99.
2) Now change only your main() to read values for initial Investment and annual interest rate using cin and cout.
3) Use the following values in the run that you turn into me:
Deposit = 1000, Annual Interest Rate = 4%
4) Be careful of whether your interest rate is monthly, and whether it needs to be divided by 100.
Part 2)
Write a class that can be used to create very short form letters concerning an election. One object represents one form letter, so name your class FormLetter. Each FormLetter object might have different values for these String fields:
• the name of the person the letter is addressed to (to)
• the name of the candidate you want the addressee to vote for (candidate)
• the name of the person the letter is from (from)
If you were to create a new FormLetter object and set the following values for the fields:
to: Hildegard
candidate: The Terminator
from: Brunhilda
And then you were to print that object, your output would look like this:
_____________________________________________
Dear Hildegard,
I would like you to vote for The Terminator
because I think he is best for this state.
Sincerely,
Brunhilda
_____________________________________________
Therefore, class FormLetter needs three member variables: to, candidate, and from.
And class FormLetter needs two member functions: setValues() and printLetter(). Note that the member function called setValues() must have three parameters to accept the new values for the three member variables. The member function printLetter() needs no parameters, because all the data is already inside the object when printLetter() is called.
In order to test this class FormLetter, you must write a main() outside of the class. Every time you write one reusable class, you will always write a main() that will simply declare an object of the reusable class, and call its member functions on the object to see if they work.
So inside main():
1. declare a variable of class FormLetter
2. call setValues() with your chosen values to store inside the object
3. call printLetter() to see if the letter looks right.
In order to receive full credit for this assignment:
• You must go through the above TEST TWICE in the same main(), so that you can see whether the member function setValues() can change the values inside an existing FormLetter object, based on values sent from main().
• This program gets no data from the user's keyboard, all data is included as string literals in the main().
Final Insurance Rate Program
Finished the insurance fee program. I think it's accurate. It's not copy & pasting right but this shows where I fixed the errors I was confused about.
/* This program prints the annual insurance fee for a person based on their car value, age, and number of tickets they have received*/
#include
#include
using namespace std;
int main ()
{
int carValue, age, numberOfTickets;
cout << "Enter your car value: \n";
cin >> carValue;
cout << "Enter your age: \n";
cin >> age;
cout << "How many tickets have you received? \n";
cin >> numberOfTickets;
float baseRate;
baseRate = (carValue*.05);
float ageRate;
if (age<=24)
ageRate=(carValue*.0575);
else if ((age>=25) && (age<=29))
ageRate=(carValue*.055);
else
ageRate=baseRate;
switch (numberOfTickets)
{
case 0: cout << "Your annual insurance rate is " <<>"\n";
break;
case 1: cout << "Your annual insurance rate is " <<>1.10 << "\n";
break;
case 2: cout << "Your annual insurance rate is " <<>1.25 << "\n";
break;
case 3: cout << "Your annual insurance rate is " <<>1.50 << "\n";
break;
default: cout << "Hop on a bike and start peddling. You are so denied.";
}
return 0;
}
/* program output */
Running…
Enter your car value:
10000
Enter your age:
35
How many tickets have you received?
1
Your annual insurance rate is 550
Running…
Enter your car value:
15000
Enter your age:
29
How many tickets have you received?
2
Your annual insurance rate is 1031.25
Running…
Enter your car value:
850
Enter your age:
19
How many tickets have you received?
3
Your annual insurance rate is 73.3125
Running…
12500
Enter your age:
81
How many tickets have you received?
4
Hop on a bike and start peddling. You are so denied.
Building an Insurance Cost Calculator in C++
using namespace std;
int main(void)
int carValue;
cout << "Enter your car value: ";
cin >> carValue;
using namespace std;
int main(void)
int carValue;
cout << "Enter your car value: ";
cin >> carValue;
#include
#include
using namespace std;
int main ()
{
int carValue, age, numberOfTickets;
cout << "Enter your car value: \n";
cin >> carValue;
cout << "Enter your age: \n";
cin >> age;
cout << "How many tickets have you received? \n";
cin >> numberOfTickets;
float baseRate;
baseRate = (carValue*.05);
float ageRate;
if (age<=24)
ageRate=(carValue*.0575);
else if ((age<=24) && (age>=29))
ageRate=(carValue*.055);
else
ageRate=baseRate;
float finalRate
switch (finalRate)
{
case 0: cout << "Your annual insurance rate is ageRate \n";
break;
case 1: cout << "Your annual insurance rate is >> ageRate*1.10 \n";
break;
case 2:cout << "Your annual insurance rate is >> ageRate*1.25 \n";
break;
case 3:cout << "Your annual insurance rate is >> ageRate*1.50 \n";
break;
}
if (numberOfTickets >=4)
cout << "Hop on a bike. You are so denied.";
return 0;
}
================
/*more when i get this figured out*/