Function Problems

I wrote the code and ran it before i put it into functions and it worked. I'm terrible at functions and so now i'm stuck, say the variables are not being initialized.
my code is as followed:


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
void CalcMP (float PR, float IM, float Q, float MP, float IY, float P, int NM);
void printinfo (float PR, float IY, float IM, int NY, int NM, float MP);
void printTable (int Month, float balance, float MP, float Interest, float total, int NM, float IM);



int main ()
{

	float total = 0;
	int Month = 1;
	float PR;
	float IY;
	int NY;
	int NM;
	float IM;
	float P;
	float Q;
	float MP;
	float Interest;
	float balance;
	float newbalance;

	printf("Amount of the loan (principal)?\t\t");
	scanf("%f", &PR);
	printf("Intrest rate per year (percent)\t\t");
	scanf("%f", &IY);
	printf("Number of years?\t\t\t");
	scanf("%d", &NY);


	CalcMP (PR, IM, Q, MP, IY, P, NM);

	NM = NY * 12;
	Interest = PR * IM;
	balance = PR;

	printinfo (PR, IY, IM, NY, NM, MP);

	printTable(Month, balance, MP, Interest, total, NM, IM);
	

system ("pause");
return 0;
}


void CalcMP (float PR, float IM, float Q, float MP, float IY, float P, int NM)
{

		IM = (IY / 12) / 100;
		P = pow((1 + IM), NM);
		Q = (P/ (P - 1));
		MP = (PR * IM * Q);

		return;
}
Please don't post the same topic in multiple forums. There is no point.
was hoping more people would see it
Parameters in C++ are passed to functions "by value". This means that the variable name that you use, is effectively "read only" (assigning something to it will NOT make it's way back to the caller). Therefore, in line 48, you're making a bunch of "stand-in" variables PR, IM, Q, MP, IY, P and NM which _won't_ affect those at line 32. Within CalcMP(), you can read the values from them, and use them in expressions, but you can't (usefully) assign to them.

A function in C (and C++) usually computes a single value and returns it as the "return value" (the "void" that you have at the beginning of line 48 indicates 'this function has no return value'). However, if you have a number of elements to return, then you can use the "pass by reference" technique (there is also "pass by pointer", but we won't go there...).

A "reference parameter" is one that says "this parameter is a _reference_ to something else", so that when you modify it within your function, the original one gets modified. This is because the "reference" is not a new variable, but a reference to the one in the call. References are indicated by an "&" after the variable type.

So, in practical terms, try modifying line 48 (and line 1) to read:

void CalcMP (float PR, float& IM, float& Q, float& MP, float IY, float& P, float NM )
which indicates that we're passing IM, Q, MP and P as references (because those are the items that you calculate in CalcMP()).

Also remember that when you declare variables (lines 12-22), they will have garbage values - so only leave them uninitialised if you're about to calculate the value to place in there. (hint line 34, you're calculating NM after you use it...)
Last edited on
Topic archived. No new replies allowed.