目次
Returning Values from a Function
Recursive functions, or functions that call themselves, offer considerable practical value to the programmer and are used to divide an otherwise complex problem into a simple case, reiterating that case until the problem is resolved.
Practically every introductory recursion example involves factorial computation. Yawn. Let’s do something a tad more practical and create a loan payment calculator. Specifically, the following example uses recursion to create a payment schedule, telling you the principal and interest amounts required of each payment installment to repay the loan. The recursive function, amortizationTable(),It takes as input four arguments:
paymentNum, which identifies the payment number, periodicPayment, which carries the total monthly payment, balance, which indicates the remaining loan balance, and monthlyInterest, which determines the monthly interest percentage rate. These items are designated or deter- mined in the script listed below here:
Shows sample output, based on monthly payments made on a 30-year fixed loan of $200,000.00 at 6.25 percent interest. For reasons of space conservation, just the first 10 payment iterations are listed.
Employing a recursive strategy often results in significant code savings and promotes reusability. Although recursive functions are not always the optimal solution, they are often a welcome addition to any language’s repertoire.

PHPに基づく償却表

Aug 08, 2016 am 09:20 AM
Balance gt lt the

Amortization Table

The Function of Recursive Function

What’s the Recursive Function?

Returning Values from a 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.

Recursive functions, or functions that call themselves, offer considerable practical value to the programmer and are used to divide an otherwise complex problem into a simple case, reiterating that case until the problem is resolved.

Practically every introductory recursion example involves factorial computation. Yawn. Let’s do something a tad more practical and create a loan payment calculator. Specifically, the following example uses recursion to create a payment schedule, telling you the principal and interest amounts required of each payment installment to repay the loan. The recursive function, amortizationTable(),It takes as input four arguments:

paymentNum, which identifies the payment number, periodicPayment, which carries the total monthly payment, balance, which indicates the remaining loan balance, and monthlyInterest, which determines the monthly interest percentage rate. These items are designated or deter- mined in the script listed below here:

<code><span><?php</span><span><span>function</span><span>amortizationTable</span><span>(<span>$paymentNum</span>,<span>$periodicPayment</span>,<span>$balance</span>,<span>$monthlyInterest</span>)</span>
    {</span><span>$paymentInterest</span>=round(<span>$balance</span>*<span>$monthlyInterest</span>,<span>2</span>);
        <span>$paymentPrincipal</span>=round(<span>$periodicPayment</span>-<span>$paymentInterest</span>,<span>2</span>);
        <span>$newBalance</span>=round(<span>$balance</span>-<span>$paymentPrincipal</span>,<span>2</span>);
        <span>print</span><span>"  <tr>
                    <td>$paymentNum</td>
                    <td>\$"</span>.number_format(<span>$balance</span>,<span>2</span>).<span>"</td>
                    <td>\$"</span>.number_format(<span>$periodicPayment</span>,<span>2</span>).<span>"</td>
                    <td>\$"</span>.number_format(<span>$paymentInterest</span>,<span>2</span>).<span>"</td>
                    <td>\$"</span>.number_format(<span>$paymentPrincipal</span>,<span>2</span>).<span>"</td>
                </tr>"</span>;
        <span>#If balance not yet zero ,recursively call amortizationTable()</span><span>if</span>(<span>$newBalance</span>><span>0</span>)
        {
            <span>$paymentNum</span>++;
            amortizationTable(<span>$paymentNum</span>,<span>$periodicPayment</span>,<span>$newBalance</span>,<span>$monthlyInterest</span>);
        }
        <span>else</span>
        {
            <span>exit</span>;
        }
    }<span>#end amortizationTable()</span><span>?></span><span><?php</span><span>#load balance</span><span>$balance</span>=<span>200000.0</span>;

    <span>#load interest rate</span><span>$interestRate</span>=<span>.0575</span>;

    <span>#monthly interest rate</span><span>$monthlyInterest</span>=<span>.0575</span>/<span>12</span>;

    <span>#Term length of the load, in years.</span><span>$termLength</span>=<span>30</span>;

    <span>#Number of payments per year.</span><span>$paymentsPerYear</span>=<span>12</span>;

    <span>#payment iteration</span><span>$paymentNumber</span>=<span>1</span>;

    <span>#Perform preliminary calculations</span><span>$totalPayments</span>=<span>$termLength</span>*<span>$paymentsPerYear</span>;
    <span>$intCal</span>=<span>1</span>+<span>$interestRate</span>/<span>$paymentsPerYear</span>;
    <span>$periodicPayment</span>=<span>$balance</span>*pow(<span>$intCal</span>,<span>$totalPayments</span>)*(<span>$intCal</span>-<span>1</span>)/(pow(<span>$intCal</span>,<span>$totalPayments</span>)-<span>1</span>);
    <span>$periodicPayment</span>=round(<span>$periodicPayment</span>,<span>2</span>);

    <span>#create table</span><span>echo</span><span>"<table width='50%' align ='center' border='1'>"</span>;
    <span>print</span><span>"<tr>
          <th>Payment Number</th><th>Balance</th>
          <th>Payment</th><th>Interest</th><th>Principal</th>
          </tr>"</span>;
    <span>#call recursive function</span>
    amortizationTable(<span>$paymentNumber</span>,<span>$periodicPayment</span>,<span>$balance</span>,<span>$monthlyInterest</span>);
    <span>#close table</span><span>print</span><span>"</table>"</span>;</code>
ログイン後にコピー

While I WAS compiling in PHPSTORM

Shows sample output, based on monthly payments made on a 30-year fixed loan of $200,000.00 at 6.25 percent interest. For reasons of space conservation, just the first 10 payment iterations are listed.

Here is the result in my Safari Browser

Employing a recursive strategy often results in significant code savings and promotes reusability. Although recursive functions are not always the optimal solution, they are often a welcome addition to any language’s repertoire.

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了Amortization Table base on PHP,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Huawei GT3 ProとGT4の違いは何ですか? Huawei GT3 ProとGT4の違いは何ですか? Dec 29, 2023 pm 02:27 PM

多くのユーザーはスマートウォッチを選ぶときにファーウェイブランドを選択しますが、その中でもファーウェイ GT3pro と GT4 は非常に人気のある選択肢であり、多くのユーザーはファーウェイ GT3pro と GT4 の違いに興味を持っています。 Huawei GT3pro と GT4 の違いは何ですか? 1. 外観 GT4: 46mm と 41mm、材質はガラスミラー + ステンレススチールボディ + 高解像度ファイバーバックシェルです。 GT3pro: 46.6mm および 42.9mm、材質はサファイアガラス + チタンボディ/セラミックボディ + セラミックバックシェルです。 2. 健全な GT4: 最新の Huawei Truseen5.5+ アルゴリズムを使用すると、結果はより正確になります。 GT3pro: ECG 心電図と血管と安全性を追加

2か月後、人型ロボットWalker Sが服をたたむことができるようになった 2か月後、人型ロボットWalker Sが服をたたむことができるようになった Apr 03, 2024 am 08:01 AM

Machine Power Report 編集者: Wu Xin 国内版の人型ロボット + 大型模型チームは、衣服を折りたたむなどの複雑で柔軟な素材の操作タスクを初めて完了しました。 OpenAIのマルチモーダル大規模モデルを統合したFigure01の公開により、国内同業者の関連動向が注目を集めている。つい昨日、中国の「ヒューマノイドロボットのナンバーワン株」であるUBTECHは、Baidu Wenxinの大型モデルと深く統合されたヒューマノイドロボットWalkerSの最初のデモを公開し、いくつかの興味深い新機能を示した。 Baidu Wenxin の大規模モデル機能の恩恵を受けた WalkerS は次のようになります。 Figure01 と同様に、WalkerS は動き回るのではなく、机の後ろに立って一連のタスクを完了します。人間の命令に従って服をたたむことができる

修正: Windows 11 で Snipping ツールが機能しない 修正: Windows 11 で Snipping ツールが機能しない Aug 24, 2023 am 09:48 AM

Windows 11 で Snipping Tool が機能しない理由 問題の根本原因を理解すると、適切な解決策を見つけるのに役立ちます。 Snipping Tool が正しく動作しない主な理由は次のとおりです。 フォーカス アシスタントがオンになっている: これにより、Snipping Tool が開かなくなります。破損したアプリケーション: 起動時にスニッピング ツールがクラッシュする場合は、破損している可能性があります。古いグラフィック ドライバー: 互換性のないドライバーは、スニッピング ツールに干渉する可能性があります。他のアプリケーションからの干渉: 実行中の他のアプリケーションが Snipping Tool と競合する可能性があります。証明書の有効期限が切れています: アップグレード プロセス中のエラーにより、この問題が発生する可能性があります。これらの簡単な解決策は、ほとんどのユーザーに適しており、特別な技術知識は必要ありません。 1. Windows および Microsoft Store アプリを更新する

iPhoneでApp Storeに接続できないエラーを修正する方法 iPhoneでApp Storeに接続できないエラーを修正する方法 Jul 29, 2023 am 08:22 AM

パート 1: 最初のトラブルシューティング手順 Apple のシステムステータスを確認する: 複雑な解決策を掘り下げる前に、基本から始めましょう。問題はデバイスにあるのではなく、Apple のサーバーがダウンしている可能性があります。 Apple のシステム ステータス ページにアクセスして、AppStore が適切に動作しているかどうかを確認してください。問題があれば、Apple が修正してくれるのを待つしかありません。インターネット接続を確認します。「AppStore に接続できません」問題は接続不良が原因である場合があるため、安定したインターネット接続があることを確認してください。 Wi-Fi とモバイル データを切り替えるか、ネットワーク設定をリセットしてみてください ([一般] > [リセット] > [ネットワーク設定のリセット] > [設定])。 iOS バージョンを更新します。

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

watch4proとGTのどちらが優れていますか? watch4proとGTのどちらが優れていますか? Sep 26, 2023 pm 02:45 PM

Watch4proとgtはそれぞれ特徴や適用シーンが異なりますが、総合的な機能、高性能、スタイリッシュな外観を重視し、価格は高くてもいいという方にはWatch 4 Proの方が適しているかもしれません。高度な機能要件はなく、バッテリー寿命と手頃な価格を重視する場合は、GT シリーズの方が適しているかもしれません。最終的な選択は、個人のニーズ、予算、好みに基づいて決定する必要がありますが、購入する前に自分のニーズを慎重に検討し、さまざまな製品のレビューや比較を参照して、より情報に基づいた選択を行うことをお勧めします。

Balance: ブロックチェーンと AI テクノロジーに基づいて構築された Web3 ゲーム エコシステム Balance: ブロックチェーンと AI テクノロジーに基づいて構築された Web3 ゲーム エコシステム Sep 10, 2024 am 12:38 AM

Web3 ゲーム エコシステムの構築に向けた重要な取り組みとして E-PAL チームによって作成された Balance は、ブロックチェーン ベースのゲームを中心としたプラットフォームです。

See all articles