Thread: functions and passing data

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What do you mean you're having "issues" calling the functions? You don't know how? So what then, you "found some code", have no idea how to call a function, and want us to explain it to you? Grab a C book.
    Code:
    void foo( int bar )
    {
        ...do stuff...
    }
    
    ...
    
    int x;
    
    foo( x );
    There. Now you know how to call a function and pass it an argument. Try staying awake in class, or actually reading your book instead of just ripping off code you find to get a grade.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    I'm taking an online class. So all of this is just out of the book. So sorry I'm asking so many question?

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    But you're not asking questions. Explain, in a full paragraph, exactly what the problem is. You know, like as if you needed help and actually were trying to let someone know what was wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    ok here is my issues.
    Lets start with the first issues. Im still having issues running my funcations. I have tried to changed the funcation. I get the error message 27 C:\Dev-Cpp\tmitchellwk2.cpp [Warning] statement is a reference, not call, to function `Calculate'

    I also added a retrun statement at the end of each function. Ther return statement says wat information I want to pass to the next funcation. Not sure if those are correct.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    double CollectInput ( double i, double n, double Numberofmonths,  double PaymentNumber, int count);
    double Calculate (int n, double a, double i,  int PaymentNumber,  int count); 
    double Amortization (double a, double InterestRate, double LoanBlance, double PaymentNumber, 
            double AmountPrinciple, double AmountInterest, int InterestRate); 
    main  ()
    {
    int   TermLoan,  // length of the loan
             PaymentNumber,  //number of payments made
              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 
      
            CollectInput ();               
             Calcaulate ();
              Amortization ();   
               
             
               
    printf ("hit enter to end");
    getchar ();
    getchar ();
    return 0;         
    }
    
    //****************************************************************      
    //Start of the function to collect data
    //****************************************************************
    double CollectInput (double a, double i, int n, int Numberofmonths,  double PaymentNumber, int count)
         {
          int TermLoan,
               c ;
          double LoanAmount,
                 InterestRate,
                 CurrentBlance;
                           
            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; 
                    
                    return i, n, a, PaymentNumber, count; 
       }  //end of collect data
    
    
    //********************************************************************
    //Start of Calculate function
    //*************************************************************
     double Calculate (int n, double a, double i,  int PaymentNumber,  int count)               
       {
          double PaymentAmount,
                 AmountInterest,
                 AmountPrinciple,
                 LoanAmount,
                 LoanBlance,
                 InterestRate;
          int Numberofmonths; 
          
             
             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; 
                 return a, InterestRate, PaymentNumber, LoanBlance, PaymentAmount, 
                       AmountPrinciple, AmountInterest; 
                    }//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

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, so now go back and look at my example. A function, such as foo above, has the following parts:

    1 - a return value (one item it returns), or void (to return nothing).
    2 - A name.
    3 - A list of arguments you pass it, or void (to pass nothing).
    4 - A block of its content, surrounded by a pair of { }

    Like so:
    Code:
    int foo( char c, float f )
    {
        ...stuff here...
    }
    This function:
    1 - Returns one integer (functions only ever return one thing).
    2 - Has the name 'foo'.
    3 - Takes a 'char' and a 'float' as arguments.
    4 - Has the function body wrapped in { }

    Now to call the function, you'd do something like:
    Code:
    x = foo( 'c', 2.345 );
    Here, we would put the returned integer into x, pass it the character 'c', and the floating point value 2.345 as its arguments. In short, if you have any arguments other than void, then to call that function, you must supply all of those expected arguments.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    ok im having issues getting my funcation to read from the mail program. Here is the error message that I"m getting. Can you help me to figure it out please.

    C:\Dev-Cpp\tmitchellwk2.cpp In function `int main()':
    11 C:\Dev-Cpp\tmitchellwk2.cpp too many arguments to function `double Amortization(double, double, double, int, double, double)'
    34 C:\Dev-Cpp\tmitchellwk2.cpp at this point in file


    Am I trying to read in less data then what i say i have
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    double CollectInput ( double i, int n, double Numberofmonths,  double PaymentNumber, int count);
    double   Calculate (int n,double a, double i,double PaymentAmount,double PaymentNumber, 
             double Numberofmonths, double PaymentAmount,  int count, double AmountInterest, 
             double AmountPrinciple, double LoanBlance );; 
    double Amortization (double a, double InterestRate, double LoanAmount, int PaymentNumber, 
                 double AmountPrinciple, double AmountInterest, double LoanBlance, double PaymentAmount); 
    
    
    main ()
    {
    int   TermLoan,  // length of the loan
             PaymentNumber,  //number of payments made
              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
              y,x,u, a,i,
               PaymentAmount;  //amount paid to loan 
      
      CollectInput (i, n, Numberofmonths, PaymentNumber, count);
      Calculate (n, a, i, PaymentAmount, PaymentNumber, Numberofmonths,
             PaymentAmount, count, AmountInterest, AmountPrinciple, LoanBlance );
       Amortization (a, InterestRate, LoanAmount, PaymentNumber, AmountPrinciple, 
                      AmountInterest, InterestRate);                         
               
             
               
    printf ("hit enter to end");
    getchar ();
    getchar ();
    return 0;         
    }
    
    //****************************************************************      
    //Start of the function to collect data
    //****************************************************************
    double CollectInput (double a, double i, int n, int Numberofmonths,  double PaymentNumber, int count)
         {
          int TermLoan,
               c ;
          double LoanAmount,
                 InterestRate,
                 CurrentBlance;
                           
            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; 
                    
                    return i, n, a, PaymentNumber, count; 
       }  //end of collect data
    
    
    //********************************************************************
    //Start of Calculate function
    //*************************************************************
     double Calculate (int n, double a, double i,  int PaymentNumber,  int count)               
       {
          double PaymentAmount,
                 AmountInterest,
                 AmountPrinciple,
                 LoanAmount,
                 LoanBlance,
                 InterestRate;
          int Numberofmonths; 
          
             
             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; 
                 return a, InterestRate, PaymentNumber, LoanBlance, PaymentAmount, 
                       AmountPrinciple, AmountInterest; 
                    }//end of while loop 
       } //end of Calculate Function
    
    
    //*********************************************************************
    //State of print function
    //************************************************************
    double Amortization (double a, double InterestRate, double LoanAmount,  int PaymentNumber, 
            double AmountPrinciple, double AmountInterest, double LoanBlance, double PaymentAmount)
    {
              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

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    ok i got the above error taken care of. KNow I'm getting this error

    C:\DOCUME~1\EMITCH~1\LOCALS~1\Temp\ccA9caaa.o(.tex t+0x50) In function `main':
    [Linker error] undefined reference to `CollectInput(double, int, double, double, int)'
    [Linker error] undefined reference to `Calculate(int, double, double, double, double, double, double, int, double, double, double)'
    C:\DOCUME~1\EMITCH~1\LOCALS~1\Temp\ccA9caaa.o(.tex t+0x50) ld returned 1 exit status

    any ideas

  8. #23
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, count the number of parameters you're passing, and compare it with the number the function is expecting (the definition of it, not just the prototype).

    Don't you get any warnings telling you that the prototype and definition do not agree?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    in still having issues reading them funcation any idea here is the currect code
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    double CollectInput ( double a, double i, int n, int Numberofmonths,  int PaymentNumber, int count);
    double Calculate (int n,double a, double i,double PaymentAmount,double PaymentNumber, 
             double Numberofmonths, int count, double AmountInterest, 
             double AmountPrinciple, double LoanBlance); 
    double Amortization (double a, double InterestRate, double LoanAmount, int PaymentNumber, 
                 double AmountPrinciple, double AmountInterest, double LoanBlance, double PaymentAmount); 
    
    
    main ()
    {
    int   TermLoan,  // length of the loan
             PaymentNumber,  //number of payments made
              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
              y,x,u, a,i,
               PaymentAmount;  //amount paid to loan 
      
      printf ("start of program");
      CollectInput (a, i, n, Numberofmonths, PaymentNumber, count);
      Calculate (n, a, i, PaymentAmount, PaymentNumber, Numberofmonths, count, 
                AmountInterest, AmountPrinciple, LoanBlance );
       Amortization (a, InterestRate, LoanAmount, PaymentNumber, AmountPrinciple, 
                     AmountInterest, LoanBlance, PaymentAmount);                         
               
             
               
    printf ("hit enter to end");
    getchar ();
    getchar ();
    return 0;         
    }
    
    //****************************************************************      
    //Start of the function to collect data
    //****************************************************************
    double CollectInput ( double a, double i, int n, int Numberofmonths,  int PaymentNumber, int count)
         {
          int TermLoan,
               c ;
          double LoanAmount,
                 InterestRate,
                 CurrentBlance;
                           
            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; 
                    
                    return i, n, a, PaymentNumber, count; 
       }  //end of collect data
    
    
    //********************************************************************
    //Start of Calculate function
    //*************************************************************
     double Calculate (int n,double a, double i,double PaymentAmount,double PaymentNumber, 
             double Numberofmonths, 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; 
                 return a, PaymentNumber, LoanBlance, PaymentAmount, 
                       AmountPrinciple, AmountInterest; 
                    }//end of while loop 
       } //end of Calculate Function
    
    
    //*********************************************************************
    //State of print function
    //************************************************************
    double Amortization (double a, double InterestRate, double LoanAmount,  int PaymentNumber, 
            double AmountPrinciple, double AmountInterest, double LoanBlance, double PaymentAmount)
    {
              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

  10. #25
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    OK, here is your code with what passes for formatting
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    double CollectInput(double a, double i, int n, int Numberofmonths,
                        int PaymentNumber, int count);
    double Calculate(int n, double a, double i, double PaymentAmount,
                     double PaymentNumber, double Numberofmonths, int count,
                     double AmountInterest, double AmountPrinciple,
                     double LoanBlance);
    double Amortization(double a, double InterestRate, double LoanAmount,
                        int PaymentNumber, double AmountPrinciple,
                        double AmountInterest, double LoanBlance,
                        double PaymentAmount);
    
    
    main()
    {
        int TermLoan,               // length of the loan
         PaymentNumber,             //number of payments made
         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
         y, x, u, a, i, PaymentAmount;  //amount paid to loan 
    
        printf("start of program");
        CollectInput(a, i, n, Numberofmonths, PaymentNumber, count);
        Calculate(n, a, i, PaymentAmount, PaymentNumber, Numberofmonths, count,
                  AmountInterest, AmountPrinciple, LoanBlance);
        Amortization(a, InterestRate, LoanAmount, PaymentNumber,
                     AmountPrinciple, AmountInterest, LoanBlance,
                     PaymentAmount);
    
    
    
        printf("hit enter to end");
        getchar();
        getchar();
        return 0;
    }
    
    //****************************************************************      
    //Start of the function to collect data
    //****************************************************************
    double CollectInput(double a, double i, int n, int Numberofmonths,
                        int PaymentNumber, int count)
    {
        int TermLoan, c;
        double LoanAmount, InterestRate, CurrentBlance;
    
        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;
    
        return i, n, a, PaymentNumber, count;
    }                               //end of collect data
    
    
    //********************************************************************
    //Start of Calculate function
    //*************************************************************
    double Calculate(int n, double a, double i, double PaymentAmount,
                     double PaymentNumber, double Numberofmonths, 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;
            return a, PaymentNumber, LoanBlance, PaymentAmount,
                AmountPrinciple, AmountInterest;
        }                           //end of while loop 
    }                               //end of Calculate Function
    
    
    //*********************************************************************
    //State of print function
    //************************************************************
    double Amortization(double a, double InterestRate, double LoanAmount,
                        int PaymentNumber, double AmountPrinciple,
                        double AmountInterest, double LoanBlance,
                        double PaymentAmount)
    {
        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
    I get these warnings
    Code:
    $ gcc -W -Wall new.c
    new.c:18: warning: return type defaults to `int'
    new.c: In function `main':
    new.c:19: warning: unused variable `TermLoan'
    new.c:22: warning: unused variable `c'
    new.c:26: warning: unused variable `CurrentBlance'
    new.c:28: warning: unused variable `y'
    new.c:28: warning: unused variable `x'
    new.c:28: warning: unused variable `u'
    new.c: In function `CollectInput':
    new.c:71: warning: left-hand operand of comma expression has no effect
    new.c:71: warning: left-hand operand of comma expression has no effect
    new.c:71: warning: left-hand operand of comma expression has no effect
    new.c:71: warning: left-hand operand of comma expression has no effect
    new.c: In function `Calculate':
    new.c:96: warning: left-hand operand of comma expression has no effect
    new.c:96: warning: left-hand operand of comma expression has no effect
    new.c:96: warning: left-hand operand of comma expression has no effect
    new.c:96: warning: left-hand operand of comma expression has no effect
    new.c:96: warning: left-hand operand of comma expression has no effect
    new.c:98: warning: control reaches end of non-void function
    new.c: At top level:
    new.c:104: warning: unused parameter 'LoanAmount'
    new.c: In function `Amortization':
    new.c:120: warning: control reaches end of non-void function
    The first thing is that return a,b does NOT return two values.

    If you want to return multiple results from a function, you basically have to use pointers
    Code:
    #include <stdio.h>
    
    void foo ( int a ) {
      printf("Enter a number\n");
      scanf("%d", &a);
      printf("You entered %d\n", a );
    }
    
    void bar ( int *a ) {
      printf("Enter a number\n");
      scanf("%d", a);
      printf("You entered %d\n", *a );
    }
    
    int main ( ) {
      int result = 0;
      printf( "Result = %d\n", result );
      foo ( result );
      printf( "Result = %d\n", result );
      bar ( &result );
      printf( "Result = %d\n", result );
      return 0;
    }
    
    $ gcc -W -Wall -ansi new.c
    $ ./a.exe
    Result = 0
    Enter a number
    123
    You entered 123
    Result = 0
    Enter a number
    456
    You entered 456
    Result = 456
    Note in foo() that the integer is passed by value, so anything you do to it inside the function does NOT affect the value in main().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    104

    Unhappy

    ok. so whqt you are saying is that i need to write a funcation to call the data from one funcation and pass it to the next. wouldn't it have been eaiser to use an array to hold the data then print it. am i al least gettin on the right path. I just having an issues understand read a funcation in. I there somewhere that i can go to get a better understanding. I understand that i have to define the funcation. Then we need to read it in. That is where I am lost. Why can't I just write the funcation into main and it read the funcation. Isn't a funcation kind of the same as a procedure in pascal. Do i have my funcations doing too many things. Or is there a simplier way to write them that would not be so hard.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    double CollectInput (double a, double i, int n, int Numberofmonths,  int PaymentNumber, int count);
    double Calculate (int n,double a, double i,double PaymentAmount,double PaymentNumber, 
             double Numberofmonths, int count, double AmountInterest, 
             double AmountPrinciple, double LoanBlance); 
    double Amortization (double a, double InterestRate, double LoanAmount, int PaymentNumber, 
                 double AmountPrinciple, double AmountInterest, double LoanBlance, double PaymentAmount); 
    
    
    main ()
    {
    int   TermLoan,  // length of the loan
             PaymentNumber,  //number of payments made
              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
              returnedValue,  a,i,
               PaymentAmount;  //amount paid to loan 
                   
      printf ("start of program");
      CollectInput  (a, i, n, Numberofmonths, PaymentNumber, count);
      returnedValue = CollectInput (a, i, n, Numberofmonths, PaymentNumber, count);
      Calculate (n, a, i, PaymentAmount, PaymentNumber, Numberofmonths, count, 
                AmountInterest, AmountPrinciple, LoanBlance );
             
     
       Amortization (a, InterestRate, LoanAmount, PaymentNumber, AmountPrinciple, 
                     AmountInterest, LoanBlance, PaymentAmount);                         
               
             
               
    printf ("hit enter to end");
    getchar ();
    getchar ();
    return 0;         
    }
    
    //****************************************************************      
    //Start of the function to collect data
    //****************************************************************
    double CollectInput (double a, double i, int n, int Numberofmonths, int PaymentNumber, int count)
         {
          int TermLoan,
               c ;
          double LoanAmount,
                 InterestRate,
                 CurrentBlance;
                           
            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; 
                    
                    return i, n, a, PaymentNumber, count; 
       }  //end of collect data
    
    
    //********************************************************************
    //Start of Calculate function
    //*************************************************************
     double Calculate (int n,double a, double i,double PaymentAmount,double PaymentNumber, 
             double Numberofmonths, 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; 
                 return a, PaymentNumber, LoanBlance, PaymentAmount, 
                       AmountPrinciple, AmountInterest; 
                    }//end of while loop 
       } //end of Calculate Function
    
    
    //*********************************************************************
    //State of print function
    //************************************************************
    double Amortization (double a, double InterestRate, double LoanAmount,  int PaymentNumber, 
            double AmountPrinciple, double AmountInterest, double LoanBlance, double PaymentAmount)
    {
              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

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    104

    Question

    Ok so i cahanged it again and put my return valuse so that it would pass teh data. Then I wrote a while statment to call teh funcations let me know if im on the right track. I still cant get passed it returnedValue statments in the program when i run it so i know that i still have something off here is the new code
    Code:
     
    #include <stdio.h>
    #include <math.h>
    
    
    double CollectInput (double a, double i, int n, int Numberofmonths,  int PaymentNumber, int count);
    double Calculate (int n,double a, double i,double PaymentAmount,double PaymentNumber, 
             double Numberofmonths, int count, double AmountInterest, 
             double AmountPrinciple, double LoanBlance); 
    double Amortization (double a, double InterestRate, double LoanAmount, int PaymentNumber, 
                 double AmountPrinciple, double AmountInterest, double LoanBlance, double PaymentAmount); 
    
    
    main ()
    {
         char data;
         int   TermLoan,  // length of the loan
             PaymentNumber,  //number of payments made
              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 ("This startes you loan program");
              
         double  returnedValue = CollectInput (a, i, n, Numberofmonths, PaymentNumber, count);          
         double returedValue1 = Calculate (n, a, i, PaymentAmount, PaymentNumber, Numberofmonths, count, 
                AmountInterest, AmountPrinciple, LoanBlance); 
     
         printf ("Do you have data to add?");
         scanf ("%c", &data);
          
          while (data == 'y' || data == 'Y');
          {
           CollectInput (a, i, n, Numberofmonths, PaymentNumber, count);
             Calculate (n, a, i, PaymentAmount, PaymentNumber, Numberofmonths, count, 
                AmountInterest, AmountPrinciple, LoanBlance);
           Amortization (a, InterestRate, LoanAmount,  PaymentNumber, 
            AmountPrinciple, AmountInterest, LoanBlance, PaymentAmount);                   
           }   
             
               
    printf ("hit enter to end");
    getchar ();
    getchar ();
    return 0;         
    }
    
    //****************************************************************      
    //Start of the function to collect data
    //****************************************************************
    double CollectInput (double a, double i, int n, int Numberofmonths, int PaymentNumber, int count)
         {
          int TermLoan,
               c ;
          double LoanAmount,
                 InterestRate,
                 CurrentBlance;
                           
            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; 
                    
                    return i, n, a, PaymentNumber, count; 
       }  //end of collect data
    
    
    //********************************************************************
    //Start of Calculate function
    //*************************************************************
     double Calculate (int n,double a, double i,double PaymentAmount,double PaymentNumber, 
             double Numberofmonths, 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; 
                 return a, PaymentNumber, LoanBlance, PaymentAmount, 
                       AmountPrinciple, AmountInterest; 
                    }//end of while loop 
       } //end of Calculate Function
    
    
    //*********************************************************************
    //State of print function
    //************************************************************
    double Amortization (double a, double InterestRate, double LoanAmount,  int PaymentNumber, 
            double AmountPrinciple, double AmountInterest, double LoanBlance, double PaymentAmount)
    {
              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 0;
    } //end of Amortization print function

  13. #28
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> double Amortization
    Doesn't really have to return anything. It's just printin'.

    >> main()
    should return something -> int main ()

    >> return i, n, a, PaymentNumber, count;
    ... erm no. Just no! Google or search for passing by reference, or even make a struct of the info to make the parameters look nicer.

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    how can i do that using funcations

  15. #30
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    It will not allow me to have double Amortization with out anything behind it it tells me that it is not defined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem passing data between functions
    By manutdfan in forum C Programming
    Replies: 9
    Last Post: 12-10-2006, 04:32 PM
  2. passing data between functions
    By nesagsar in forum C++ Programming
    Replies: 11
    Last Post: 12-08-2006, 12:48 PM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Replies: 4
    Last Post: 04-01-2003, 12:49 AM