# Thread: functions and passing data

1. Your best bet is to use a structure to contain all the variables you need, then you don't have to have very long lists of parameters.

Here is an example.
Study what is going on, then expand it to match your needs.
Code:
```#include <stdio.h>
#include <math.h>

typedef struct loan {
int         TermLoan;
int         NumberofMonths;
double      interestRate;
} loan;

void CollectInput( loan *details );
void Calculate ( loan *details );
void Amortization ( loan *details );

int main()
{
loan details;
printf( "start of program\n" );
CollectInput( &details );
Calculate( &details );
Amortization( &details );
printf( "hit enter to end" );
getchar();
return 0;
}

//****************************************************************
//Start of the function to collect data
//****************************************************************
void CollectInput( loan *details )
{
printf( "Enter termloan\n" );
scanf( "%d", &details->TermLoan );
}

//********************************************************************
//Start of Calculate function
//*************************************************************
void Calculate( loan *details )
{
printf( "Enter #months and rate\n" );
scanf( "%d%lf", &details->NumberofMonths, &details->interestRate );
}

//*********************************************************************
//State of print function
//************************************************************
void Amortization( loan *details )
{
printf("\amortization Schedule\n");
printf( "%d %d %f\n",
details->TermLoan, details->NumberofMonths, details->interestRate );
}

// My output
\$ ./a.exe
start of program
Enter termloan
12
Enter #months and rate
22 33
mortization Schedule
12 22 33.000000
hit enter to end\$```

2. can some one show me how i would break down my statments so that they are not passing as many variables. Is there a way that i can do a funcation within the funcation so that i can make the funcations smaller. Or should i just start all over writing this program. I really need some help

3. What, am I invisible or something?
How much fewer than one variable were you hoping to get?

4. what you showed me helped. But i had to change it to do the math.I need help to get me loop to work. i was able to get the funcations to read. But I'm not able to get the math correct. Can you look at this and tell me what Im doing wrong please.
Code:
```#include <stdio.h>
#include <math.h>

typedef struct loan {
int          c,n, count;
int         TermLoan;
int         Numberofmonths;
double      InterestRate,
LoanAmount,
a, i,
PaymentNumber,
LoanBlance,
PaymentAmount,
AmountPrinciple,
AmountInterest;
} loan;

void CollectInput( loan *details );
void Calculate ( loan *details );
void Amortization ( loan *details );

int main()
{

loan details;
printf( "start of program\n" );
CollectInput( &details );
Calculate( &details );
Amortization( &details );

printf( "hit enter to end" );
getchar();
getchar();
return 0;
}

//****************************************************************
//Start of the function to collect data
//****************************************************************
void CollectInput( loan *details )
{
printf ("Please enter in loan amount, Term of Loan in months and Interest Rate.\n");
while (scanf("%lf%d%lf", &details->LoanAmount, &details->TermLoan, &details->InterestRate) != 3
|| (details->LoanAmount < 0 || details->TermLoan < 0 || details->InterestRate < 0 ))
{
while ((details->c = getchar()) != '\n' && details->c != EOF);
printf ("Please re-enter in loan amount, Term of Loan in months and Interest Rate.\n");
}
}

//********************************************************************
//Start of Calculate function
//*************************************************************
void Calculate( loan *details )
{
details->PaymentAmount = ((details->i/12*details->a)/(1-pow((1+(details->i/12)),-details->n)));

while(details->PaymentNumber < details->Numberofmonths)
{

details->PaymentNumber++; //Start number of months for amortization schedule
details->count++;
details->AmountInterest = details->a  * (details->i/12);
details->AmountPrinciple = details->PaymentAmount - details->AmountInterest;
details->LoanBlance = details->a - details->AmountPrinciple;
}
}

//*********************************************************************
//State of print function
//************************************************************
void Amortization( loan *details )
{
printf("\amortization Schedule\n");
printf ("CurrentBlance %.2f\n", details->a);
printf ("InterestRate %.2f\n", details->InterestRate);
printf ("PaymentNumber %d\n",  details->PaymentNumber);
printf ("LoanBlance  %.2f\n", details->LoanBlance);
printf ("PaymentAmount %.2f\n", details->PaymentAmount);
printf ("AmountPrinciple %.2f\n", details->AmountPrinciple);
printf  ("AmountInterest %.2f\n", details->AmountInterest);
printf ("_______________________\n\n");
details->a = details->LoanBlance;

}```

5. my program run cool. But I have an issues. How do i call my
Amortization( &details ); to print it prints one time around but not the second. I I also for some reason not able to pass the payment number wither any ideas. or do i do another while loop in the main or can i run that funcation with in another funcation

Code:
```#include <stdio.h>
#include <math.h>

typedef struct loan {
int          c,n, count;
int         TermLoan;
int         Numberofmonths;
double      InterestRate,
LoanAmount,
a, i,
PaymentNumber,
LoanBlance,
PaymentAmount,
AmountPrinciple,
AmountInterest,
CurrentBlance;
} loan;

void CollectInput( loan *details );
void Calculate ( loan *details );
void Amortization ( loan *details );

int main()
{

loan details;
printf( "start of program\n" );
CollectInput( &details );
Calculate( &details );
Amortization( &details );

printf( "hit enter to end" );
getchar();
getchar();
return 0;
}

//****************************************************************
//Start of the function to collect data
//****************************************************************
void CollectInput( loan *details )
{
printf ("Please enter in loan amount, Term of Loan in months and Interest Rate.\n");
while (scanf("%lf%d%lf", &details->LoanAmount, &details->TermLoan, &details->InterestRate) != 3
|| (details->LoanAmount < 0 || details->TermLoan < 0 || details->InterestRate < 0 ))
{
while ((details->c = getchar()) != '\n' && details->c != EOF);
printf ("Please re-enter in loan amount, Term of Loan in months and Interest Rate.\n");
}

}

//********************************************************************
//Start of Calculate function
//*************************************************************
void Calculate( loan *details )
{
details->CurrentBlance = details->LoanAmount;
details->Numberofmonths = details->TermLoan;
details->i = details->InterestRate/ 100;
details->n = details->Numberofmonths;
details->a = details->CurrentBlance;
details->PaymentNumber = 0;
details->count = 0;
details->PaymentAmount = ((details->i/12*details->a)/(1-pow((1+(details->i/12)),-details->n)));

while(details->PaymentNumber < details->Numberofmonths)
{

details->PaymentNumber++; //Start number of months for amortization schedule
details->count++;
details->AmountInterest = details->a  * (details->i/12);
details->AmountPrinciple = details->PaymentAmount - details->AmountInterest;
details->LoanBlance = details->a - details->AmountPrinciple;

details->a = details->LoanBlance;

}
}

//*********************************************************************
//State of print function
//************************************************************
void Amortization( loan *details )
{
printf("\amortization Schedule\n");
printf ("CurrentBlance %.2f\n", details->a);
printf ("PaymentNumber %d\n",  details->PaymentNumber);
printf ("LoanBlance  %.2f\n", details->LoanBlance);
printf ("PaymentAmount %.2f\n", details->PaymentAmount);
printf ("AmountPrinciple %.2f\n", details->AmountPrinciple);
printf  ("AmountInterest %.2f\n", details->AmountInterest);
printf ("_______________________\n\n");
details->a = details->LoanBlance;

}```

6. Call it from inside the loop in Calculate() perhaps?

7. I did that and it prints ok. THe only issue that I have knwo is that once i run the program it only prints to payment 8 and the I get a lot of wried letters. i cdan run 6 or 7 months for the payment and the program runs fine. Is there a buffer that is not allowing it to print out more then that many

this is what i get
Code:
```start of program
Please enter in loan amount, Term of Loan in months and Interest Rate.
1000.00 8 8
amortization Schedule
CurrentBlance 877.89
PaymentNumber 1
LoanBlance  877.89
PaymentAmount 128.78
AmountPrinciple 122.11
AmountInterest 6.67
_______________________

amortization Schedule
CurrentBlance 754.96
PaymentNumber 2
LoanBlance  754.96
PaymentAmount 128.78
AmountPrinciple 122.93
AmountInterest 5.85
_______________________

amortization Schedule
CurrentBlance 631.22
PaymentNumber 3
LoanBlance  631.22
PaymentAmount 128.78
AmountPrinciple 123.75
AmountInterest 5.03
_______________________

amortization Schedule
CurrentBlance 506.64
PaymentNumber 4
LoanBlance  506.64
PaymentAmount 128.78
AmountPrinciple 124.57
AmountInterest 4.21
_______________________

amortization Schedule
CurrentBlance 381.24
PaymentNumber 5
LoanBlance  381.24
PaymentAmount 128.78
AmountPrinciple 125.40
AmountInterest 3.38
_______________________

amortization Schedule
CurrentBlance 255.01
PaymentNumber 6
LoanBlance  255.01
PaymentAmount 128.78
AmountPrinciple 126.24
AmountInterest 2.54
_______________________

amortization Schedule
CurrentBlance 127.93
PaymentNumber 7
LoanBlance  127.93
PaymentAmount 128.78
AmountPrinciple 127.08
AmountInterest 1.70
_______________________

amortization Schedule
CurrentBlance -1.#J
PaymentNumber 8
LoanBlance  -1.#J
PaymentAmount 128.78
AmountPrinciple -1.#J
AmountInterest -1.#J
_______________________

hit enter to end```
and here is my program
Code:
```#include <stdio.h>
#include <math.h>

typedef struct loan {
int          c,n, count, PaymentNumber;
int         TermLoan;
int         Numberofmonths;
double      InterestRate,
LoanAmount,
a, i,
LoanBlance,
PaymentAmount,
AmountPrinciple,
AmountInterest,
CurrentBlance;
} loan;

void CollectInput( loan *details );
void Calculate ( loan *details );

int main()
{

loan details;
printf( "start of program\n" );
CollectInput( &details );
Calculate( &details );

printf( "hit enter to end" );
getchar();
getchar();
return 0;
}

//****************************************************************
//Start of the function to collect data
//****************************************************************
void CollectInput( loan *details )
{
printf ("Please enter in loan amount, Term of Loan in months and Interest Rate.\n");
while (scanf("%lf%d%lf", &details->LoanAmount, &details->TermLoan, &details->InterestRate) != 3
|| (details->LoanAmount < 0 || details->TermLoan < 0 || details->InterestRate < 0 ))
{
while ((details->c = getchar()) != '\n' && details->c != EOF);
printf ("Please re-enter in loan amount, Term of Loan in months and Interest Rate.\n");
}
return;
}

//********************************************************************
//Start of Calculate function
//*************************************************************
void Calculate( loan *details )
{
void Amortization ( loan *details );
int count;

details->CurrentBlance = details->LoanAmount;
details->Numberofmonths = details->TermLoan;
details->i = details->InterestRate/ 100;
details->n = details->Numberofmonths;
details->a = details->CurrentBlance;
details->PaymentNumber = 0;
count = 0;
details->PaymentAmount = ((details->i/12*details->a)/(1-pow((1+(details->i/12)),-details->n)));

while(details->PaymentNumber < details->Numberofmonths)
{
details->PaymentNumber++; //Start number of months for amortization schedule
count++;
details->AmountInterest = details->a  * (details->i/12);
details->AmountPrinciple = details->PaymentAmount - details->AmountInterest;
details->LoanBlance = details->a - details->AmountPrinciple;
Amortization( details );
details->a = details->LoanBlance;

}
}

//*********************************************************************
//State of print function
//************************************************************
double Amortization( loan *details )
{
printf("amortization Schedule\n");
printf ("CurrentBlance %.2f\n", details->a);
printf ("PaymentNumber %d\n",  details->PaymentNumber);
printf ("LoanBlance  %.2f\n", details->LoanBlance);
printf ("PaymentAmount %.2f\n", details->PaymentAmount);
printf ("AmountPrinciple %.2f\n", details->AmountPrinciple);
printf  ("AmountInterest %.2f\n", details->AmountInterest);
printf ("_______________________\n\n");
}```

8. Looking at your output, I'd say that you while loop runs once longer than it should.

9. put if i do the same thing with test data 1000.00 loanamount 12for month and 12 interest i get the same result . so its like for some reason I'm running out of room for the numbers. I'm trying to figure out what kind of printf statemnts i can use to show me what causeing the data to get corrupt.

Code:
```start of program
Please enter in loan amount, Term of Loan in months and Interest Rate.
1000.00 12 12
amortization Schedule
CurrentBlance 1000.00
PaymentNumber 1
LoanBlance  921.15
PaymentAmount 88.85
AmountPrinciple 78.85
AmountInterest 10.00
_______________________

amortization Schedule
CurrentBlance 921.15
PaymentNumber 2
LoanBlance  841.51
PaymentAmount 88.85
AmountPrinciple 79.64
AmountInterest 9.21
_______________________

amortization Schedule
CurrentBlance 841.51
PaymentNumber 3
LoanBlance  761.08
PaymentAmount 88.85
AmountPrinciple 80.43
AmountInterest 8.42
_______________________

amortization Schedule
CurrentBlance 761.08
PaymentNumber 4
LoanBlance  679.84
PaymentAmount 88.85
AmountPrinciple 81.24
AmountInterest 7.61
_______________________

amortization Schedule
CurrentBlance 679.84
PaymentNumber 5
LoanBlance  597.79
PaymentAmount 88.85
AmountPrinciple 82.05
AmountInterest 6.80
_______________________

amortization Schedule
CurrentBlance 597.79
PaymentNumber 6
LoanBlance  514.92
PaymentAmount 88.85
AmountPrinciple 82.87
AmountInterest 5.98
_______________________

amortization Schedule
CurrentBlance 514.92
PaymentNumber 7
LoanBlance  431.22
PaymentAmount 88.85
AmountPrinciple 83.70
AmountInterest 5.15
_______________________

amortization Schedule
CurrentBlance 431.22
PaymentNumber 8
LoanBlance  -1.#J
PaymentAmount 88.85
AmountPrinciple -1.#J
AmountInterest -1.#J
_______________________

amortization Schedule
CurrentBlance -1.#J
PaymentNumber 9
LoanBlance  -1.#J
PaymentAmount 88.85
AmountPrinciple -1.#J
AmountInterest -1.#J
_______________________

amortization Schedule
CurrentBlance -1.#J
PaymentNumber 10
LoanBlance  -1.#J
PaymentAmount 88.85
AmountPrinciple -1.#J
AmountInterest -1.#J
_______________________

amortization Schedule
CurrentBlance -1.#J
PaymentNumber 11
LoanBlance  -1.#J
PaymentAmount 88.85
AmountPrinciple -1.#J
AmountInterest -1.#J
_______________________

amortization Schedule
CurrentBlance -1.#J
PaymentNumber 12
LoanBlance  -1.#J
PaymentAmount 88.85
AmountPrinciple -1.#J
AmountInterest -1.#J
_______________________

hit enter to end```

10. > double Amortization( loan *details )
Previously declared as returning void (which is better)

Strange, my results are out of step (almost from yours). Here are my last 3
Code:
```\$ ./a.exe
start of program
Please enter in loan amount, Term of Loan in months and Interest Rate.
1000 8 8
amortization Schedule
CurrentBlance 1000.00
PaymentNumber 1
LoanBlance  877.89
PaymentAmount 128.78
AmountPrinciple 122.11
AmountInterest 6.67
_______________________

<snip>

amortization Schedule
CurrentBlance 381.24
PaymentNumber 6
LoanBlance  255.01
PaymentAmount 128.78
AmountPrinciple 126.24
AmountInterest 2.54
_______________________

amortization Schedule
CurrentBlance 255.01
PaymentNumber 7
LoanBlance  127.93
PaymentAmount 128.78
AmountPrinciple 127.08
AmountInterest 1.70
_______________________

amortization Schedule
CurrentBlance 127.93
PaymentNumber 8
LoanBlance  -0.00
PaymentAmount 128.78
AmountPrinciple 127.93
AmountInterest 0.85
_______________________```
> CurrentBlance -1.#J
Lots of weird stuff can happen when floating point numbers get very close to zero without actually being zero. Rounding errors are just one of the many things which make floating point much more of a challenge than it first seems.

11. Code:
```void Calculate( loan *details )
{
void Amortization ( loan *details );```
Try moving the Amortization prototype outside of your Calculate function. (near the other prototypes)

change double Amortization to void Amortization in the definition.

Also, you have many unused/uneeded variables.
You never use count (well, you increment it but for no reason)
You never use loan->count

Code:
```details->Numberofmonths = details->TermLoan; //after this TermLoan is never used again
details->n = details->Numberofmonths;  //after this Numberofmonths is never used again```
You don't need to (should not) store every variable your program is going to use in your struct.
For example "int c" is nothing more than a temporary variable used by CollectInput, it has nothing to do with loan

Your problem may be with floating point precision, or lack there of.

This should function the same as your current code, but with many of the useless variables removed
Code:
```#include <stdio.h>
#include <math.h>

typedef struct loan {
int         PaymentNumber;
int         TermLoan;
double      InterestRate,
LoanAmount,
LoanBalance,
PaymentAmount,
AmountPrinciple,
AmountInterest;
} loan;

void CollectInput( loan *details );
void Calculate ( loan *details );
void Amortization( loan *details );

int main()
{
loan details;
printf( "start of program\n" );
CollectInput( &details );
Calculate( &details );

printf( "hit enter to end" );
getchar();
return 0;
}

//****************************************************************
//Start of the function to collect data
//****************************************************************
void CollectInput( loan *details )
{
int c;
printf ("Please enter in loan amount, Term of Loan in months and Interest Rate.\n");
while (scanf("%lf%d%lf", &details->LoanAmount, &details->TermLoan, &details->InterestRate) != 3
|| (details->LoanAmount < 0 || details->TermLoan < 0 || details->InterestRate < 0 ))
{
/* "flush stdin" */
while ((c = getchar()) != '\n' && c != EOF);
printf ("Please re-enter in loan amount, Term of Loan in months and Interest Rate.\n");
}
}

//********************************************************************
//Start of Calculate function
//*************************************************************
void Calculate( loan *details )
{
details->InterestRate =  details->InterestRate / 100 / 12;
details->LoanBalance = details->LoanAmount;
details->PaymentNumber = 1;

details->PaymentAmount = ((details->InterestRate * details->LoanAmount) /
(1-pow((1+(details->InterestRate)),-details->TermLoan))
);

while(details->PaymentNumber <= details->TermLoan)
{
details->AmountInterest = details->LoanBalance * (details->InterestRate);
details->AmountPrinciple = details->PaymentAmount - details->AmountInterest;
details->LoanBalance = details->LoanBalance - details->AmountPrinciple;
Amortization( details );
details->PaymentNumber++;
}
}

//*********************************************************************
//Start of print function
//************************************************************
void Amortization( loan *details )
{
printf("Amortization Schedule\n");
printf("CurrentBalance  %.2f\n", details->LoanBalance);
printf("PaymentNumber   %d\n",   details->PaymentNumber);
printf("LoanBalance     %.2f\n", details->LoanBalance);
printf("PaymentAmount   %.2f\n", details->PaymentAmount);
printf("AmountPrinciple %.2f\n", details->AmountPrinciple);
printf("AmountInterest  %.2f\n", details->AmountInterest);
printf("_______________________\n\n");
}```
EDIT: I see the same output as Salem

12. I figured out the double and void problem about 30 minutes ago., THanks for all of your help