# Thread: functions and passing data

1. ## functions and passing data

Im trying to take this code and modify it so that i have three funcations and pass data between the funcations. I have started to change the code and want to know if im on the right track,

Code:
```/*************************************************************
File Name:     Tmitchellwk2
Description:   Display Loan Amortization Schedule via functions
Date:          Spetembet 16, 2006
Designer;      Tabatha Mitchell
Assignment:    week2
Functions:	int CollectInput; This will collect the input and                  validate Input values
int Calculate; this is where all of the calculations
Will be done
int Amortization:  This is where all of the data
will be printed
**************************************************************/
#include <stdio.h>
#include <math.h>

double CollectInput (double LoanAmount,  int TermLoan, double InerestRate);
double Calculate;
double Amortization;

main  ()
{

int   TermLoan,  // length of the loan
Numberofmonths, //Number of months in the loan
count,
n,c; //Letters used for the calculations
double   AmountPrinciple, //Amount paid on principle
AmountInterest,  //amount paid to interest
LoanBlance, //amount owed on loan\ count,
CurrentBlance,
LoanAmount, //amount of the loan
InterestRate, //interest rate on loan
a,i,
PaymentAmount;  //amount paid to loan

/****************************************************************
//Start of the function to collect data
/****************************************************************
double CollectInput (double LoanAmount,  int TermLoan, double InerestRate);
{

printf ("Please enter in loan amount, Term of Loan in months and Interest Rate.\n");

while (scanf("%lf%d%lf", &LoanAmount, &TermLoan, &InterestRate) != 3
|| (LoanAmount < 0 || TermLoan < 0 || InterestRate < 0 ))
{
while ((c = getchar()) != '\n' && c != EOF);
printf ("Please re-enter in loan amount, Term of Loan in months and Interest Rate.\n");
}
CurrentBlance = LoanAmount;
Numberofmonths = TermLoan;
i = InterestRate/ 100;
n = Numberofmonths;
a = CurrentBlance;
PaymentNumber = 0;
count = 0;
}  //end of collect data

/********************************************************************
Start of Calculate function
/*************************************************************
int Calculate ( int n, double a, double i);
{

PaymentAmount = ((i/12*a)/(1-pow((1+(i/12)),-n)));

while(PaymentNumber < Numberofmonths)
{

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

a = LoanBlance;
}//end of while loop
} //end of Calculate Function

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

} //end of Amortization print function

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

2. Code:
```printf ("hit enter to end");
getchar ();
getchar ();```
At least you have the printf() before the getchars() now. But I still think you should use this instead:
Code:
```int c;
while((c = getchar()) != '\n' && c != EOF);
getchar();```
The variable c in CollectInput() isn't declared. Declare it as int c at the top of the function.

Code:
```int Amortization;
{```
->
Code:
```int Amortization(arguments)
{```
+
Code:
``` double CollectInput (double LoanAmount,  int TermLoan, double InerestRate);
{```
->
Code:
``` double CollectInput (double LoanAmount,  int TermLoan, double InerestRate)
{```
+
Code:
```double CollectInput (double LoanAmount,  int TermLoan, double InerestRate);
double Calculate;
double Amortization;```
->
Code:
```double CollectInput (double LoanAmount,  int TermLoan, double InerestRate);
double Calculate(arguments);
double Amortization(arguments);```
Functions have to be declared outside other functions; all your functions are currently inside main(). Move them outside, perhaps after main().

3. Ok so i chagned the code. Can you also look at the while statments. I was told taht how i had it before was not checking the data. So i chagned it can you tell me if it is correct.

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

double CollectInput (double LoanAmount,  int TermLoan, double InerestRate);
double Calculate(arguments);
double Amortization(arguments);
main  ()
{

int   TermLoan,  // length of the loan
Numberofmonths, //Number of months in the loan
count,
n,c; //Letters used for the calculations
double   AmountPrinciple, //Amount paid on principle
AmountInterest,  //amount paid to interest
LoanBlance, //amount owed on loan\ count,
CurrentBlance,
LoanAmount, //amount of the loan
InterestRate, //interest rate on loan
a,i,
PaymentAmount;  //amount paid to loan

/****************************************************************
//Start of the function to collect data
/****************************************************************
double CollectInput (double LoanAmount,  int TermLoan, double InerestRate)
{
printf ("Please enter in loan amount, Term of Loan in months and Interest Rate.\n");
scanf("%lf%d%f", &LoanAmount, &TermLoan, &InterestRate);

while (Interest <= 0)
{
printf ("Invalid entry. Re-enter Interest Rate:\n");
scanf ("\n%f", &InterestRate);
}

while (LoanAmount <= 0)
{
printf ("Invalid entry. Re-enter Loan Amount:\n")
scanf ("\n%f", &LoanAmount);
}

while (TermLoan <= 0)
{
printf ("Invalid entry. Re-enter Term of Loan:\n");
scanf ("\n%d", &TermLoan);
}

while ((c = getchar()) != '\n' && c != EOF);
{
printf ("Please re-enter in loan amount, Term of Loan in months and Interest Rate.\n");
}
CurrentBlance = LoanAmount;
Numberofmonths = TermLoan;
i = InterestRate/ 100;
n = Numberofmonths;
a = CurrentBlance;
PaymentNumber = 0;
count = 0;
}  //end of collect data

/********************************************************************
Start of Calculate function
/*************************************************************
int Calculate ( int n, double a, double i);
{

PaymentAmount = ((i/12*a)/(1-pow((1+(i/12)),-n)));

while(PaymentNumber < Numberofmonths)
{

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

a = LoanBlance;
}//end of while loop
} //end of Calculate Function

/**********************************************************************
State of print function
/*************************************************************
int Amortization(arguments);
{
printf ("\amortization Schedule\n");
printf ("_____________________\n");
printf ("CurrentBlance %.2f\n", a);
printf ("InterestRate %.2f\n", InterestRate);
printf ("PaymentNumber %d\n",  PaymentNumber);
printf ("LoanBlance  %.2f\n", LoanBlance);
printf ("PaymentAmount %.2f\n", PaymentAmount);
printf ("AmountPrinciple %.2f\n", AmountPrinciple);
printf  ("AmountInterest %.2f\n", AmountInterest);
printf ("_______________________\n\n");

} //end of Amortization print function

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

4. A bit better, but you still have stray semicolons after the function defintions
Code:
```int Amortization(arguments);
{```
and the functions are still all inside main(). And I didn't mean to put "arguments" there, I meant replace "arguments" with whatever arguments the function takes, such as nothing or "int x" or "int x, int y" or something.

Code:
``` while ((c = getchar()) != '\n' && c != EOF);
{
printf ("Please re-enter in loan amount, Term of Loan in months and Interest Rate.\n");
}```
I think you misunderstood what that loop does. It's equivalent to this:
Code:
```for(;;) {
c = getchar();
if(c == '\n' || c == EOF) break;
}```
That printf() isn't inside the loop. Notice the semicolon.
[/edit]

5. ok i changed it again. I moved the statment so that it will look to see if there are any letter let me know if i did it right. I also moved my ; off of the funcations.

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

double CollectInput (double LoanAmount,  int TermLoan, double InerestRate);
double Calculate(arguments);
double Amortization(arguments);
main  ()
{

int   TermLoan,  // length of the loan
Numberofmonths, //Number of months in the loan
count,
n,c; //Letters used for the calculations
double   AmountPrinciple, //Amount paid on principle
AmountInterest,  //amount paid to interest
LoanBlance, //amount owed on loan\ count,
CurrentBlance,
LoanAmount, //amount of the loan
InterestRate, //interest rate on loan
a,i,
PaymentAmount;  //amount paid to loan

/****************************************************************
//Start of the function to collect data
/****************************************************************
double CollectInput (double LoanAmount,  int TermLoan, double InerestRate)
{
printf ("Please enter in  Term of Loan “);
scanf ("\n%d", &TermLoan);
while (TermLoan <= 0)
{
while ((c = getchar()) != '\n' && c != EOF);
printf ("Invalid entry. Re-enter Term of Loan:\n");
scanf ("\n%d", &TermLoan);
}

printf ("Please enter in  Interest “);
scanf ("\n%f", &InterestRate);
while (Interest <= 0)
{
while ((c = getchar()) != '\n' && c != EOF);
printf ("Invalid entry. Re-enter Interest Rate:\n");
scanf ("\n%f", &InterestRate);
}

printf ("Please enter in  LoanAmount “);
scanf ("\n%f", &LoanAmount);
while (LoanAmount <= 0)
{
while ((c = getchar()) != '\n' && c != EOF);
printf ("Invalid entry. Re-enter Loan Amount:\n")
scanf ("\n%f", &LoanAmount);
}

CurrentBlance = LoanAmount;
Numberofmonths = TermLoan;
i = InterestRate/ 100;
n = Numberofmonths;
a = CurrentBlance;
PaymentNumber = 0;
count = 0;
}  //end of collect data

/********************************************************************
Start of Calculate function
/*************************************************************
int Calculate (arguments)
{

PaymentAmount = ((i/12*a)/(1-pow((1+(i/12)),-n)));

while(PaymentNumber < Numberofmonths)
{

PaymentNumber++; //Start number of months for amortization schedule
count++;
AmountInterest = a  * (i/12);
AmountPrinciple = PaymentAmount - AmountInterest;
LoanBlance = a - AmountPrinciple;
a = LoanBlance;
}//end of while loop
} //end of Calculate Function

/**********************************************************************
State of print function
/*************************************************************
int Amortization(arguments)
{
printf ("\amortization Schedule\n");
printf ("_____________________\n");
printf ("CurrentBlance %.2f\n", a);
printf ("InterestRate %.2f\n", InterestRate);
printf ("PaymentNumber %d\n",  PaymentNumber);
printf ("LoanBlance  %.2f\n", LoanBlance);
printf ("PaymentAmount %.2f\n", PaymentAmount);
printf ("AmountPrinciple %.2f\n", AmountPrinciple);
printf  ("AmountInterest %.2f\n", AmountInterest);
printf ("_______________________\n\n");

} //end of Amortization print function

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

6. Okay, good, the semicolons are gone.

But this code
Code:
```printf ("hit enter to end");
getchar ();
getchar ();
return 0;
}```
is part of main, so any function between the start of main and this are inside main(). You need to move the whole of every function, from type name(){ to }, beyond this point.

And you still have arguments instead of whatever your arguments are. If you don't intend to pass any arguments to the function, leave the space where arguments is empty.

7. for the calculation it you be
Code:
` int Calculate (int n, double a, double i`
fro the Amoritization it woule be
Code:
` int Amortization(double a, InterestRate, LoanBlance, PaymentNumber, AmountPrinciple, AmountInterest, Int InterestRate)`
the way that i have it set up is that it should get the data check it of its correct. pass it on to the calculate . then the ccalculate will pass it on to the print.

wnd i also moved the return statement to this
Code:
```main  ()
{

int   TermLoan,  // length of the loan
Numberofmonths, //Number of months in the loan
count,
n,c; //Letters used for the calculations
double   AmountPrinciple, //Amount paid on principle
AmountInterest,  //amount paid to interest
LoanBlance, //amount owed on loan\ count,
CurrentBlance,
LoanAmount, //amount of the loan
InterestRate, //interest rate on loan
a,i,
PaymentAmount;  //amount paid to loan

printf ("hit enter to end");
getchar ();
getchar ();
return 0;
}```
then i have the funcation below it. is this correct. Can i keep the doubles and the int defined in this main part of the program.

8. Yes, your moving of the end of main() up to the top is correct. You can only use variables defined in main() in other functions if you pass those variables to the function. The changes made by the function won't be noticed in main() unless you pass a pointer to the variable or return the modified value.

9. how do i pass my function from on to the other. So my program is still off in that matter

10. so i need to add a return to send the data to the next function In this prgram can i move my while statement out of the funcation have have it do a while statement the pull the funcations

Code:
```/*************************************************************
File Name:     Tmitchellwk2
Description:   Display Loan Amortization Schedule via functions
Date: 	     Spetembet 16, 2006
Designer;      Tabatha Mitchell
Assignment:    week2
Functions:	int CollectInput; This will collect the input and
validate Input values
int Calculate; this is where all of the calculations
Will be done
int Amortization:  This is where all of the data
will be printed
**************************************************************/
#include <stdio.h>
#include <math.h>

double CollectInput (double LoanAmount,  int TermLoan, double InerestRate);
double Calculate (int n, double a, double i);
double Amortization(double a, InterestRate, LoanBlance, PaymentNumber, AmountPrinciple, AmountInterest, Int InterestRate);
main  ()
{

int   TermLoan,  // length of the loan
Numberofmonths, //Number of months in the loan
count,
n,c; //Letters used for the calculations
double   AmountPrinciple, //Amount paid on principle
AmountInterest,  //amount paid to interest
LoanBlance, //amount owed on loan\ count,
CurrentBlance,
LoanAmount, //amount of the loan
InterestRate, //interest rate on loan
a,i,
PaymentAmount;  //amount paid to loan

PaymentAmount = ((i/12*a)/(1-pow((1+(i/12)),-n)));

while(PaymentNumber < Numberofmonths)
{
double Calculate (int n, double a, double i);
double Amortization(double a, InterestRate, LoanBlance, PaymentNumber, AmountPrinciple, AmountInterest, Int InterestRate);
double Calculate (int n, double a, double i);
double Amortization(double a, InterestRate, LoanBlance, PaymentNumber, AmountPrinciple, AmountInterest, Int InterestRate);
}
printf ("hit enter to end");
getchar ();
getchar ();
return 0;
}

/****************************************************************
//Start of the function to collect data
/****************************************************************
double CollectInput (double LoanAmount,  int TermLoan, double InerestRate)
{
printf ("Please enter in  Term of Loan “);
scanf ("\n%d", &TermLoan);
while (TermLoan <= 0)
{
while ((c = getchar()) != '\n' && c != EOF);
printf ("Invalid entry. Re-enter Term of Loan:\n");
scanf ("\n%d", &TermLoan);
}

printf ("Please enter in  Interest “);
scanf ("\n%f", &InterestRate);
while (Interest <= 0)
{
while ((c = getchar()) != '\n' && c != EOF);
printf ("Invalid entry. Re-enter Interest Rate:\n");
scanf ("\n%f", &InterestRate);
}

printf ("Please enter in  LoanAmount “);
scanf ("\n%f", &LoanAmount);
while (LoanAmount <= 0)
{
while ((c = getchar()) != '\n' && c != EOF);
printf ("Invalid entry. Re-enter Loan Amount:\n")
scanf ("\n%f", &LoanAmount);
}

CurrentBlance = LoanAmount;
Numberofmonths = TermLoan;
i = InterestRate/ 100;
n = Numberofmonths;
a = CurrentBlance;
PaymentNumber = 0;
count = 0;
return;
}  //end of collect data

/********************************************************************
Start of Calculate function
/*************************************************************
int Calculate (int n, double a, double i)
{

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

return;
} //end of Calculate Function

/**********************************************************************
State of print function
/*************************************************************
int Amortization(double a, InterestRate, LoanBlance, PaymentNumber, AmountPrinciple, AmountInterest, Int InterestRate)
{
printf ("\amortization Schedule\n");
printf ("_____________________\n");
printf ("CurrentBlance %.2f\n", a);
printf ("InterestRate %.2f\n", InterestRate);
printf ("PaymentNumber %d\n",  PaymentNumber);
printf ("LoanBlance  %.2f\n", LoanBlance);
printf ("PaymentAmount %.2f\n", PaymentAmount);
printf ("AmountPrinciple %.2f\n", AmountPrinciple);
printf  ("AmountInterest %.2f\n", AmountInterest);
printf ("_______________________\n\n");
return;
} //end of Amortization print function```

11. Code:
```            while(PaymentNumber < Numberofmonths)
{
double Calculate (int n, double a, double i);
double Amortization(double a, InterestRate, LoanBlance, PaymentNumber, AmountPrinciple, AmountInterest, Int InterestRate);
double Calculate (int n, double a, double i);
double Amortization(double a, InterestRate, LoanBlance, PaymentNumber, AmountPrinciple, AmountInterest, Int InterestRate);
}```
That's not how you call a function. How do you call printf()? puts()? Leave out the data type (double a, int interestrate) and the return type.

12. i need help. I have the program written with no code. But I'm having an issue getting to the functions . Need some help please
Code:
```

#include <stdio.h>
#include <math.h>

double CollectInput (double LoanAmount,  int TermLoan, double InterestRate, int c);
double Calculate (int n, double a, double i)   ;
double Amortization(double a, double InterestRate, double LoanBlance, double PaymentNumber,
double AmountPrinciple, double AmountInterest, int InterestRate);
main  ()
{

int   TermLoan,  // length of the loan
Numberofmonths, //Number of months in the loan
count,
n,c; //Letters used for the calculations
double   AmountPrinciple, //Amount paid on principle
AmountInterest,  //amount paid to interest
LoanBlance, //amount owed on loan\ count,
CurrentBlance,
LoanAmount, //amount of the loan
InterestRate, //interest rate on loan
a,i,
PaymentAmount;  //amount paid to loan

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

//****************************************************************
//Start of the function to collect data
//****************************************************************
double CollectInput (double LoanAmount,  int TermLoan, double InterestRate, int c, double i, double n,
double CurrentBlance,double Numberofmonths, double a, double PaymentNumber, int count)
{

while (scanf("%lf%d%lf", &LoanAmount, &TermLoan, &InterestRate) != 3
|| (LoanAmount < 0 || TermLoan < 0 || InterestRate < 0 ))
{
while ((c = getchar()) != '\n' && c != EOF);
printf ("Please re-enter in loan amount, Term of Loan in months and Interest Rate.\n");
}

CurrentBlance = LoanAmount;
Numberofmonths = TermLoan;
i = InterestRate/ 100;
n = Numberofmonths;
a = CurrentBlance;
PaymentNumber = 0;
count = 0;
}  //end of collect data

//********************************************************************
//Start of Calculate function
//*************************************************************
double Calculate (int n, double a, double i, double PaymentAmout, double PaymentNumber, int Numberofmonths,
double PaymentAmount, int count, double AmountInterest, double AmountPrinciple, double LoanBlance )
{

PaymentAmount = ((i/12*a)/(1-pow((1+(i/12)),-n)));

while(PaymentNumber < Numberofmonths)
{

PaymentNumber++; //Start number of months for amortization schedule
count++;
AmountInterest = a  * (i/12);
AmountPrinciple = PaymentAmount - AmountInterest;
LoanBlance = a - AmountPrinciple;
a = LoanBlance;
}//end of while loop
} //end of Calculate Function

//*********************************************************************
//State of print function
//************************************************************
double Amortization(double a, double LoanBlance, double PaymentNumber, double PaymentAmount,
double AmountPrinciple, double AmountInterest, int InterestRate)
{
printf ("\amortization Schedule\n");
printf ("_____________________\n");
printf ("CurrentBlance %.2f\n", a);
printf ("InterestRate %.2f\n", InterestRate);
printf ("PaymentNumber %d\n",  PaymentNumber);
printf ("LoanBlance  %.2f\n", LoanBlance);
printf ("PaymentAmount %.2f\n", PaymentAmount);
printf ("AmountPrinciple %.2f\n", AmountPrinciple);
printf  ("AmountInterest %.2f\n", AmountInterest);
printf ("_______________________\n\n");

} //end of Amortization print function```

13. i need help. I have the program written with no code. But I'm having an issue getting to the functions . Need some help please
What do u mean "written with no code". i dont see any function with no code. Your question dosn't make any real sense

more over your program is not intended properly.

ssharish2005

14. im having issues calling the funcations into the program. So I need some help to see where I missed a step.