时间:2021-07-01 10:21:17 帮助过:15人阅读
The Function of Recursive Function
What’s the Recursive Function?
Often, simply relying on a function to do something is insufficient; a script’s outcome might depend on a function’s outcome, or on changes in data resulting from its execution. Yet variable scoping prevents information from easily being passed from a function body back to its caller, so how can we accomplish this? You can pass data back to the caller by way of the return keyword.
functionamortizationTable($paymentNum,$periodicPayment,$balance,$monthlyInterest)
{$paymentInterest=round($balance*$monthlyInterest,2);
$paymentPrincipal=round($periodicPayment-$paymentInterest,2);
$newBalance=round($balance-$paymentPrincipal,2);
print"
$paymentNum
\$".number_format($balance,2)."
\$".number_format($periodicPayment,2)."
\$".number_format($paymentInterest,2)."
\$".number_format($paymentPrincipal,2)."
";
#If balance not yet zero ,recursively call amortizationTable()if($newBalance>0)
{
$paymentNum++;
amortizationTable($paymentNum,$periodicPayment,$newBalance,$monthlyInterest);
}
else
{
exit;
}
}#end amortizationTable()?>#load balance$balance=200000.0;
#load interest rate$interestRate=.0575;
#monthly interest rate$monthlyInterest=.0575/12;
#Term length of the load, in years.$termLength=30;
#Number of payments per year.$paymentsPerYear=12;
#payment iteration$paymentNumber=1;
#Perform preliminary calculations$totalPayments=$termLength*$paymentsPerYear;
$intCal=1+$interestRate/$paymentsPerYear;
$periodicPayment=$balance*pow($intCal,$totalPayments)*($intCal-1)/(pow($intCal,$totalPayments)-1);
$periodicPayment=round($periodicPayment,2);
#create tableecho"";
print"
Payment Number Balance
Payment Interest Principal
";
#call recursive function
amortizationTable($paymentNumber,$periodicPayment,$balance,$monthlyInterest);
#close tableprint"
";
While I WAS compiling in PHPSTORM
Here is the result in my Safari Browser
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了Amortization Table base on PHP,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。