Call-By-Value
參照到副本值
不可修改
效率慢
Call-By-Reference
參照記憶體位址
可修改
效率快
#include <iostream> using namespace std; //Call-By-Value double price(int number, int money); //Call-By-Reference double price2(int& number, int money); int main() { int money = 0, number = 0; cout << "請輸入物品數量:" ; cin >> number; cout << "請輸入價錢:" ; cin >> money; double m_price = price(number, money); cout << "總金額:" << m_price << endl << "物品數量"<< number << endl; m_price = price2(number, money); cout << "\n\n\n"; cout << "總金額:" << m_price << endl << "物品數量"<< number << endl; system("pause"); return 0; } double price(int number,int money){ int sum = 0; number*=2; sum = number * money; return sum; } double price2(int& number, int money){ int sum = 0; number*=2; sum = number * money; return sum; }
結果: