Trying to learn how to write a function


Hi, I am trying to write a program for a school assignment, that does 2 functions, 1 of calculating a monthly mortgage payment, and the other function calculates total interest over the life of the loan.

From my reading, it appears that you prototype a function and its variables above main(), then call it somewhere in main() and underneath of main you write the function? (I have my function above main() in my code because I kept getting strange errors when it was below main() .)

Then for the function, you define the variable type you want returned, then give the function a name and put the required variables to be passed into the function, inside of the functions parenthesis, like so:

int _functionname($funcvar1, $funcvar2, $funcvar3)
{
$outputvalue = $funcvar1 * $funcvar2 * $funcvar3 ;
return($outputvalue) ;
}

Is that correct? If so, when I call the function, do I have to include the assignment type like so:

int _functionname($input1, $input2, $input3)

or when I call it, should it be

_functionname($input1, $input2, $input3)

It appears this is done like this, so I could call the function multiple times, with different variables, and whatever is inside of the parenthesis when I call it, gets assigned to the values inside of the parenthesis when I write/define the function? To be more specific in this case,

$input1 = $funcvar1
$input2 = $funcvar2
$input3 = $funcvar3

when I called it above? And then I could pass a completely different set of values to funcvar1, funcvar2, funcvar3, if I called it a second or third time?


If this is the case, can anyone tell me why my _testfunc fails when it is below main() and why my code below as is, fails when it is run, after being compiled? (it does not give an error message in VS2008?)

Thanks for reading!

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

// finalproject.cpp : main project file.

 

#include"stdafx.h" 

#include<iostream> 

#include <string>

 

using namespace System; 

using namespace std ; 

 

int main() 

{
			// prototype of variables


            string $firstname ;
			
			char $pmiresponse ;
 

			int $yearsleft = 0 ; 

            int $monthsleft = 0 ;

            char $changeoption ;

            int $currenthomevalue = 0 ; 

            int $mortgageterminyears = 0 ; 

            int $newmortgageterminyears = 0 ;
			
			

 

            float $totalinterest = 0.0f ;

			float $monthlypayment = 0.0f ;

            float $principleamount = 0.0f ; 

            float $newinterestrate = 0.0f ;

            float $pmipaymentamount = 0.0f ;

            float $extrapaymentamount = 0.0f ;

            float $currentinterestrate = 0.0f ;

			float $currentpaymentamount = 0.0f ; 

			
			// prototype of functions

				int $testfunc1 = 0 ;
				int $testfunc2 = 0 ;
			int _testfunc(int $testfunc1,int $testfunc2) ;
			{
				$currentpaymentamount = $testfunc1 + $testfunc2 ;
				return($currentpaymentamount) ;

			}

			

			float _calculatecurrentmortgage(float $calculatecurrentmortgagevar1, float $calculatecurrentmortgagevar2) ; // , int $calculatecurrentmortgagevar3, int $calculatecurrentmortgagevar4) ; 
					// $monthlypaymentnumerator = principle (interestrate) * ((1 + interestrate) * numberofmonths)
					// $monthlypaymentdenominator = ((1 + interestrate) * numberofmonths) - 1
					// float variable 1 is the principle amount		$principleamount
					// float variable 2 is the current interest rate	$currentinterestrate
					// int variable 3 is the loan term in years		$mortgageterminyears
					// int variable 4 is unused currently
					float $calculatecurrentmortgagevar1 = 0 ;
					float $calculatecurrentmortgagevar2 = 0 ;
					float $calculatecurrentmortgagevar3 = 0 ;
					float $calculatecurrentmortgagevar4 = 0 ;

			float _calculatetotalinterest(float $calculatetotalinterestvar1, float $calculatetotalinterestvar2) ; // , int $calculatetotalinterestvar3, int $calculatetotalinterestvar4) ;  // has to bring in these 4 variables, all are option, and need to have defaults if they aren't passed
					// $totalinterest =  ($currentpaymentamount * ($mortgageterminyears * 12)) - $principleamount
					// float variable 1 is the principle amount		$principleamount
					// float variable 2 is the current interest rate	$currentinterestrate
					// int variable 3 is the loan term in years		$mortgageterminyears
					// int variable 4 is unused currently
					float $calculatetotalinterestvar1 = 0 ;
					float $calculatetotalinterestvar2 = 0 ;
					float $calculatetotalinterestvar3 = 0 ;
					float $calculatetotalinterestvar4 = 0 ;

 

            cout << "Welcome ... [program introduction text removed to be within forum character limit] ... payment." << endl ; 

            cout << endl ;

 

            cout << "Hello, what is your first name?" << endl ; // first name entry

            cin >> $firstname ;

            cout << $firstname << endl ;


			cout << "Thank you " << $firstname << ", now onto the calculations part of this program. Let’s start with your current mortgage, how many years was your mortgage for, originally?" << endl ; // original term of mortgage entry
				cin >> $mortgageterminyears ;
			cout << $mortgageterminyears << endl ;

			
			cout << "How many years do you have left on your current mortgage?" << endl ; // years left on mortgage entry
				cin >> $yearsleft ;
			cout << $yearsleft << endl ;


			cout << "How many months left?" << endl ; // months left on mortgage entry
				cin >> $monthsleft ;
			cout << $monthsleft << endl ;


			cout << "How much money did you borrow from the bank? (this is also known as your principle)" << endl ; // principle loan amount entry
				cin >> $principleamount ;
			cout << $principleamount << endl ;


			cout << "What is your current fixed interest rate?" << endl ; // current interest rate entry
				cin >> $currentinterestrate ;
			cout << $currentinterestrate << endl ;


			cout << "What is your homes current value?" << endl ; // current value of home entry
				cin >> $currenthomevalue ;
			cout << $currenthomevalue << endl ;

			_testfunc($currenthomevalue, $yearsleft) ; // , $currenthomevalue, $currenthomevalue) ;
			


			cout << "Thanks. Your principle + interest payment is " << $currentpaymentamount << " per month. You will pay " << $totalinterest << " over the life of the loan. Do you currently pay private mortgage insurance?" << endl ; // current payment output and pmi question is answered
				cin >> $pmiresponse ;

				if ($pmiresponse = 'Y')
				{
					cout << "How much is your private mortgage insurance per month?" << endl ;
					cin >> $pmipaymentamount ;
				}
				else
				{
					$pmipaymentamount = 0 ;
				}

			cout << $pmipaymentamount << endl ;

			restart:
			cout << "Now what would you like to change? Press 1 for your monthly payment amount. Press 2 for the term (length) of the loan. Press 3 for the interest rate. Press ‘r’ to restart and ‘x’ to quit." << endl ;
				cin >> $changeoption ;
			cout << $changeoption << endl ;

			switch ($changeoption)
			{
				
				case 1: 
					cout << "How much extra would you like to pay in addition to the " << $currentpaymentamount << " that you pay each month?" << endl ;
						cin >> $extrapaymentamount ;
						// _recalculate($extrapaymentamount + $currentpaymentamount, , ) ;
					break;


				case 2: 
					cout << "How many years would you like the term of your loan to be for?" << endl ;
						cin >>  $newmortgageterminyears ;
						// _recalculate(,$newmortgageterminyears,) ;
					break;


				case 3: 
					cout << "What would you like your new interest rate to be calculated at?" << endl ;
						cin >> $newinterestrate ;
						// _recalculate(,,$newinterestrate) ;
					break;


				case 4: 
					cout << "Starting calculations over." << endl ;
						// cin >> ;
						goto restart ; 
					break;


				default : 
					cout << "Thank you for using this tool." << endl ;
						// cin >> ;
						return(0) ;
					break;
			}

			
			float _calculatecurrentmortgage(float $calculatecurrentmortgagevar1, float $calculatecurrentmortgagevar2) ; // , int $calculatecurrentmortgagevar3, int $calculatecurrentmortgagevar4) ;  // has to bring in these 4 variables, all are option, and need to have defaults if they aren't passed
					// variable 1 is the principle amount		$principleamount
					// variable 2 is the current interest rate	$currentinterestrate
					// variable 3 is the loan term in years		$mortgageterminyears

				//M = P [ i(1 + i)n ] / [ (1 + i)n - 1
			{
				// $monthlypaymentnumerator = principle (interestrate) * ((1 + interestrate) * numberofmonths)
				// $monthlypaymentdenominator = ((1 + interestrate) * numberofmonths) - 1

				// $monthlypaymentnumerator = $principleamount ($currentinterestrate / 12) * ((1 + ($currentinterestrate / 12)) * ($mortgageterminyears *12))
				// $monthlypaymentdemoninator = ((1 + ($currentinterestrate/12)) * ($mortgageterminyears * 12)) - 1

				// $currentpaymentamount = $monthlypaymentnumerator/$monthlypaymentdenominator

				$currentpaymentamount = $calculatecurrentmortgagevar1 + $calculatecurrentmortgagevar2 ;


				// has to return $currentpaymentamount to the rest of the program


				// QUESTIONS
				// Do I have to delcare the variables local or global?
				// What happens if I pass a int value to a float variable?

			return($currentpaymentamount) ;


			}


			float _calculatetotalinterest(float $calculatetotalinterest1, float $calculatetotalinterest2) ; // , int $calculatetotalinterest3, int $calculatetotalinterest4) ; 
			{

				// variable 1 is the principle amount		$principleamount
				// variable 2 is the current interest rate	$currentinterestrate
				// variable 3 is the loan term in years	$mortgageterminyears



				// $totalinterest =  ($currentpaymentamount * ($mortgageterminyears * 12)) - $principleamount


				// has to return $totalinterest to the rest of the program

				$totalinterest = 5.5 ;

			return($totalinterest) ;

			}



            system("PAUSE") ; 

}

 
In case someone else comes across this post, I should have put my function above main, and the variable names when defining the function, have to be different then any variable names outside of the function
In C++ you don't need the $ before the variable name nor the _ before the function name.
You can't implement a function within another function
eg:
68
69
70
71
72
73
			int _testfunc(int $testfunc1,int $testfunc2) ; //This lines declares a global function ( does not define it )
			{ // all the other lines will be executed in main
				$currentpaymentamount = $testfunc1 + $testfunc2  ;
				return($currentpaymentamount) ;

			}

144
145
			_testfunc($currenthomevalue, $yearsleft) ; //_testfunc won't be defined
 

See http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
Topic archived. No new replies allowed.