Last active
December 14, 2015 17:49
-
-
Save arunenigma/5124813 to your computer and use it in GitHub Desktop.
Reference Parameters in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Reference Parameters in C++ | |
| #include <iostream> | |
| using namespace std; | |
| /* Withdraws the amount from the given balance, or withdraws | |
| a penalty if the balance is insufficient | |
| @param balance: the balance from which to make the withdrawal | |
| @param amount: the amount to withdraw | |
| */ | |
| void withdraw(double &balance, double amount) { // &balance ==> parameter referencing to decrement value | |
| // void withdraw(double balance, double amount) ==> will fail to decrement | |
| const double PENALTY = 10; | |
| if (balance >= amount) { | |
| balance = balance - amount; | |
| } | |
| else { | |
| balance = balance - PENALTY; | |
| } | |
| } | |
| int main() { | |
| double hari_account = 1000; | |
| double arun_account = 10000; | |
| withdraw(hari_account, 100); | |
| withdraw(arun_account, 50000); | |
| withdraw(arun_account, 500); | |
| cout << "Hari's Account: " << hari_account << endl; | |
| cout << "Arun's Account: " << arun_account << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment