-2

I want to be able to use the "costOfCar" function in the "InterestPaid" function. I would appreciate any help.

namespace Functions9
{
    class Program
    {
        static void Main(string[] args)
        {
            Double userCarCost, userDownPayment, userInterestRatePercentage, test; //declares "userCarCost" , "userDownPayment variable", "userInterestRatePercentage"
            Console.WriteLine("This program will do some calculations for you about a car loan."); //explains the purpose and function of program to user
            Console.WriteLine(); //adds an empty line
            Console.WriteLine("What is the cost of your new car?"); //prompt user for input
            userCarCost = Convert.ToDouble(Console.ReadLine()); //converts user input regarding car cost to double and stores in "userCarCost" variable
            Console.WriteLine(); //adds an empty line
            Console.WriteLine("How much money will you put as a down payment");
            userDownPayment = Convert.ToDouble(Console.ReadLine()); //converts user input regarding down payment to double and stores in "userDownPayment" variable
            Console.WriteLine();
            Console.WriteLine("What is your interest rate percentage? Put a point before the number");
            userInterestRatePercentage = Convert.ToDouble(Console.ReadLine()); //converts user input regarding interest rate to double and stores in "userInterestRatePercentage" variable
            Console.WriteLine(); //adds an empty line
            Console.WriteLine("How many years before you pay back the loan?");

            CostOfCar(userCarCost, userDownPayment);
        }
        static double CostOfCar(double number1, double number2)
        {

            double costOfCar = number1 - number2; //"costOfCar" variable is determined by subtracting "userCarCost", "userDownPayment"
            return costOfCar; // returns the cost of the car to the main method
        }

        static double InterestPaid(double number1, double number2)
        {
            double interestPaid = number1 * number2;
            return interestPaid;


        }


    }
}
2
  • Are you having a problem calling the method? If so, please show what errors you're getting. Jan 15, 2015 at 16:41
  • 1
    use the return value of the function. Jan 15, 2015 at 16:42

3 Answers 3

3

Just call it?

    static double InterestPaid (double number1, double number2)
    {
        double interestPaid = number1 * number2 * CostOfCar(number1, number2);
        return interestPaid;
    }

Or with the correct formula you need.

(update according remark):

    static double InterestPaid (double number1, double number2)
    {
        var costOfCar = CostOfCar(number1, number);
        double interestPaid = number1 * number2 * costOfCar;
        return interestPaid;
    }

Btw, it is better to name functions with a verb, like CalculateCostOfCar.

3
  • Calling it does not capture the returned value - unless the function is called correctly. The calling class must set up a variable to which the computed value is returned.
    – Thomas
    Jan 15, 2015 at 16:56
  • I cannot see it in the original answer, but I adapted the answer accordingly. Jan 15, 2015 at 17:15
  • 1
    Oops, sorry. Yeah, I see what you are saying. Look at OP call to CostOfCar(), though. I cleaned up the question a bit. It looks like the OP also doesn't understand how to capture values returned by a function, e.g. double cost = functionCall().
    – Thomas
    Jan 15, 2015 at 18:18
1

You need something to capture the value.

In Main, instead of

CostOfCar(userCarCost, userDownPayment);

write

double cost = CostOfCar(userCarCost, userDownPayment);

Here, the variable cost is capturing the returned value from the function for later use.

0
    static double InterestPaid (double number1, double number2)
    {            
        var val=Program.CostOfCar(number1, number2);
        double interestPaid = number1 * number2;
        var total=val+interestpaid;
        return total;
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.