ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Function ဆိုတာ အလုပ်တစ်ခုကို နာမည်ပေးပြီး သီးခြားခွဲရေးထားတဲ့ code အပိုင်းလေးပါ။ အဲဒီအလုပ်ကို လိုတဲ့နေရာတိုင်းမှာ နာမည်နဲ့ ခေါ်လိုက်ရုံပါပဲ၊ code ကို ထပ်ကူးစရာ မလိုပါဘူး။ ကော်ဖီစက်လေးလို စဉ်းစားကြည့်ပါ — ရေ၊ ကော်ဖီမှုန့်ကို ထည့်လိုက်ရင် ကော်ဖီတစ်ခွက် ထွက်လာပါတယ်၊ အတွင်းမှာ ဘာဖြစ်နေလဲ သိစရာ မလိုပါဘူး။
C++ မှာ function တိုင်းက သူ့ signature ကို ကြေညာထားပါတယ်: ဘာ type ပြန်ပေးမလဲ (return type)၊ နာမည်၊ ဘာတွေ လက်ခံမလဲ (parameter)။ Parameter တွေက function ရဲ့ အဝင်ပေါက်တွေဖြစ်ပြီး return value က အထွက်ပေါက်ပါ။ အလုပ်ပဲ လုပ်ပြီး ပြန်ပေးစရာ မရှိတဲ့ function ကတော့ void ဖြစ်ပါတယ်။ ဒီ lesson က လေ့လာသူတွေ ဂရုစိုက်ရမယ့် အချက်တွေကို သိစေပါလိမ့်မယ် — argument ကို copy ကူးပေးတာနဲ့ original ကို တိုက်ရိုက် ပေးတာ ကွာခြားပုံ၊ ကြေညာခြင်းအစဉ်၊ အမည်တူ function တွေကို compiler က ရွေးပုံပါ။
Problem → Why → Solution
ပြဿနာ: ဘာမှ မပြောင်းတဲ့ discount function
ဆိုင်တစ်ဆိုင်ရဲ့ program က 2000 ကျပ်တန် ပစ္စည်းတစ်ခုကို applyDiscount() နဲ့ 10 ရာခိုင်နှုန်း လျှော့ပေးပါတယ်။ Function ထဲမှာ တွက်ချက်မှုက မှန်ကန်ပါတယ်၊ compiler ကလည်း -Wall -Wextra နဲ့တောင် warning တစ်ခုမှ မပြပါဘူး။ ဒါပေမယ့် function ကို ခေါ်ပြီးနောက် ဈေးနှုန်းကို print ထုတ်တော့ 2000 အတိုင်းပဲ ရှိနေပါတယ်။ Function က အလုပ်လုပ်သွားတာ အမှန်ပါ — ဘယ်သူ့ကိုမှ မပြောင်းပေးခဲ့တာပါ။
#include <iostream>
void applyDiscount(double price, double percent) {
price = price - price * percent / 100; // 1800, but only inside here
}
int main() {
double price = 2000;
applyDiscount(price, 10);
std::cout << "Price: " << price << '\n'; // Price: 2000
return 0;
}Function က ဘာကြောင့် 1800 ကို ဘယ်သူ့ကိုမှ မပေးခဲ့တာလဲ
C++ က argument တွေကို default အနေနဲ့ copy ကူးပေးပါတယ် (pass by value)။ applyDiscount() ထဲက price ဆိုတာ main ထဲက price နဲ့ နာမည်ပဲ တူတဲ့ သီးခြား variable အသစ်တစ်ခုပါ၊ ခေါ်တဲ့အချိန်မှာ 2000 ကို ကူးထည့်ထားတာပါ။ Function က အဲဒီ copy ကို 1800 ပြောင်းပြီး ပြီးသွားတဲ့အခါ copy ကို ပစ်ပယ်လိုက်ပါတယ်။ Caller ရဲ့ variable ကို ပြောင်းစေချင်ရင် parameter ကို double& လို reference အဖြစ် ကြေညာပါ၊ ဒါမှမဟုတ် ရလဒ်ကို return ပြန်ပြီး caller က သိမ်းစေပါ။
Core concept: signature၊ copy နဲ့ reference
Function တစ်ခုရဲ့ definition မှာ return type၊ နာမည်၊ parentheses ထဲက parameter list နဲ့ braces ထဲက body ပါပါတယ်။ return statement က value ကို caller ဆီ ပြန်ပို့ပြီး function ကို ချက်ချင်း ပြီးဆုံးစေပါတယ်။ void function မှာ value မပါတဲ့ return; ကို သုံးလို့ရပါတယ် (ဒါမှမဟုတ် ပိတ် brace အထိ ရောက်ရုံပဲ)။ Value ပြန်တဲ့ function တိုင်းမှာ လမ်းကြောင်းအားလုံးက return နဲ့ ဆုံးရပါမယ်၊ မဟုတ်ရင် undefined behaviour ဖြစ်ပါတယ်။ main ကိုပဲ compiler က return 0 ကို အလိုအလျောက် ဖြည့်ပေးတဲ့ အထူးအခြေအနေအဖြစ် ဆက်ဆံပါတယ်။
Compiler က ဖိုင်ကို အပေါ်ကနေ အောက်ကို ဖတ်တာမို့ function တစ်ခုကို မခေါ်ခင် ကြေညာထားရပါမယ်။ ဖြေရှင်းနည်းနှစ်ခု ရှိပါတယ်: definition ကို main ရဲ့ အပေါ်မှာ ထားတာ၊ ဒါမှမဟုတ် body မပါတဲ့ prototype (int square(int n);) ကို အပေါ်မှာ ရေးပြီး definition ကို နောက်မှ ထားတာပါ။ Prototype ကြောင့် main ကို အပေါ်ဆုံးမှာ ထားနိုင်တာမို့ ဖိုင်ကို ဖတ်ရတာ ပိုလွယ်သွားတတ်ပါတယ်။
Parameter ပို့နည်း သုံးမျိုးရှိပြီး function ကို ရေးတဲ့အခါ တစ်ခုချင်းစီကို ရွေးချယ်ရပါတယ်။ By value (int n) က copy ကူးပေးပါတယ်၊ int, double, char, bool လို သေးတဲ့ type တွေအတွက် ရိုးရှင်းပြီး မြန်ပါတယ်။ Const reference (const std::string& text) က copy မကူးဘဲ original ကို ဖတ်ခွင့်ပဲ ပေးပါတယ်၊ std::string လို ပိုကြီးတဲ့ type တွေကို ဖတ်ရုံသက်သက် လိုတဲ့အခါ အကောင်းဆုံးပါ။ Non-const reference (double& price) က caller ရဲ့ variable ကို တိုက်ရိုက် ပြင်ခွင့်ပေးပါတယ်။ ပြင်စရာမရှိတဲ့ reference ကို const ထားတာက ဖတ်တဲ့သူကိုလည်း အရေးကြီးတယ်: ပြင်တာကို ကတိမပေးဘူးဆိုတာ ကြိုသိရပါတယ်။
Default argument က parameter ကို optional ဖြစ်စေပါတယ်: int times = 2 လို့ ရေးထားရင် caller က times မပို့ဘဲ ခေါ်လို့ရပါတယ်။ Default တွေက နောက်ဆုံး parameter တွေမှာပဲ ရှိရပြီး (default ပါတဲ့ parameter ရဲ့ နောက်က parameter တိုင်းမှာလည်း default ရှိရမယ်) prototype နဲ့ definition ကြားမှာ တစ်နေရာတည်းမှာပဲ ရေးရပါတယ်။ Overloading ကတော့ function နာမည်တူကို parameter type မတူအောင် အကြိမ်များစွာ ရေးခွင့်ပေးပါတယ်၊ compiler က argument တွေရဲ့ type ကို ကြည့်ပြီး ကိုက်တာကို ရွေးပါတယ်။ Return type တစ်ခုတည်း ကွာတာက overload မဖြစ်ပါဘူး၊ ဘယ် overload ကိုမှ တိတိကျကျ မကိုက်ဘဲ နှစ်ခု ညီတူ ကိုက်နေရင် call က ambiguous ဖြစ်ပြီး compile မဖြစ်ပါဘူး။
| ပို့နည်း | ကြေညာပုံ | ဘယ်အချိန်သုံးမလဲ |
|---|---|---|
| By value | int n | type သေးပြီး copy ကူးဖို့ ကုန်ကျစရိတ် မရှိ |
| Const reference | const std::string& s | ကြီးတဲ့ type ကို ဖတ်ရုံပဲ လိုတယ် |
| Reference | double& price | caller ရဲ့ variable ကို ပြင်ရမယ် |
- Caller က argument တွေကို တွက်
- Parameter တွေကို သူတို့နဲ့ initialise လုပ်
- Body ကို local variable တွေနဲ့ run
- return က value တစ်ခုကို ပြန်ပို့
By value ဆိုရင် function က copy ကို ကိုင်ထားတာမို့ caller ရဲ့ variable ကို မထိနိုင်ပါဘူး၊ reference မှသာ original ကို ထိနိုင်ပါတယ်။
အထွေထွေ ပုံစံ
ReturnType name(Type value, const BigType& readOnly); // prototype
ReturnType name(Type value, const BigType& readOnly) { // definition
// body
return result; // none for void
}
void change(Type& target); // modifies the caller's object
ReturnType name(Type value, Type extra = defaultValue); // optional extra
ReturnType name(OtherType value); // overload: different parametersPrototype က body မပါဘဲ semicolon နဲ့ ဆုံးပြီး definition က body ပါပါတယ်၊ ပထမကို main မတိုင်ခင် ထားနိုင်ပါတယ်။ Parameter type တွေက ပို့နည်းကို ဆုံးဖြတ်ပါတယ် — copy အတွက် Type၊ ဖတ်ရုံအတွက် const Type&၊ ပြင်ဖို့ Type& ။ Default argument ကို တစ်နေရာတည်းမှာပဲ ရေးပါ၊ overload တွေက parameter ကွဲမှသာ ဖြစ်ပါတယ်။
Example: function သုံးမျိုးနဲ့ call တစ်ခုစီ
ဒီ program မှာ function သုံးခုပါပါတယ်: print ထုတ်ရုံသက်သက် လုပ်တဲ့ void function၊ int တစ်ခု ပြန်တဲ့ function နဲ့ double တစ်ခု ပြန်တဲ့ function ပါ။ Definition တွေကို main ရဲ့ အပေါ်မှာ ထားတာမို့ compiler က main မတိုင်ခင် သူတို့ကို သိပြီးသားပါ။ ပြီးရင် main ထဲမှာ call တစ်ခုစီ လုပ်ပြီး ရလဒ်တွေကို ခြေရာခံကြည့်ပါ။
#include <iostream>
#include <string>
void showWelcome(const std::string& name) {
std::cout << "Welcome to the C++ lesson, " << name << "!\n";
}
int add(int a, int b) {
return a + b;
}
double average(int total, int count) {
return static_cast<double>(total) / count;
}
int main() {
showWelcome("Aye");
int total = add(5, 3);
std::cout << "Total: " << total << '\n';
std::cout << "Average of 2 numbers: " << average(total, 2) << '\n';
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- A void function
void showWelcome(const std::string& name) {void က ဒီ function က value မပြန်ဘူးလို့ ဆိုလိုပါတယ်၊ အလုပ်က print ထုတ်ရုံပါ။ const std::string& name က နာမည်ကို copy မကူးဘဲ ဖတ်ခွင့်ပဲ ရတဲ့ parameter ပါ။
- Returning an int
int add(int a, int b) {int နှစ်ခုကို လက်ခံပြီး int တစ်ခု ပြန်ပါတယ်။ a နဲ့ b က by value ဖြစ်လို့ caller ပို့တဲ့ value တွေရဲ့ copy တွေပါ၊ body ထဲမှာ ပြင်ရင်တောင် caller ကို မထိခိုက်ပါဘူး။
- Converting before dividing
return static_cast<double>(total) / count;Return type က double ဖြစ်ပေမယ့် total နဲ့ count က int ပါ။ total ကို အရင် double ပြောင်းထားလို့ division က floating point နဲ့ တွက်ပြီး 8 / 2 အတွက် 4 ရပါတယ်၊ 5 / 2 လို့ဆိုရင် 2.5 ရမှာပါ။ operators lesson မှာ ပြခဲ့တဲ့ integer division ထောင်ချောက်ကို ရှောင်တာပါ။
- Storing a result
int total = add(5, 3);Argument 5 နဲ့ 3 ကို a နဲ့ b ထဲ copy ကူးပြီး add က 8 ကို ပြန်ပါတယ်။ call expression ကိုယ်တိုင်က value 8 ဖြစ်လို့ total ထဲ သိမ်းလို့ ရတာပါ။
- A call as a statement
showWelcome("Aye");void function ရဲ့ ရလဒ်ကို သိမ်းစရာ မရှိလို့ call ကို statement တစ်ခုအနေနဲ့ ရေးရပါတယ်။ "Aye" literal ကို const std::string& အတွက် std::string ယာယီတစ်ခု အလိုအလျောက် ဖန်တီးပေးပါတယ်။
Welcome to the C++ lesson, Aye!
Total: 8
Average of 2 numbers: 4- main က add(5, 3) ကို ခေါ်
- a = 5, b = 3 (copy)
- return a + b က 8 ကို ပြန်ပို့
- main က 8 ကို total ထဲ သိမ်း
Prototype နဲ့ default argument
ဒီ snippet မှာ main က အပေါ်ဆုံးမှာ ရှိပြီး repeat() ကို prototype နဲ့ ကြေညာထားပါတယ်။ Default value တွေ (times = 2 နဲ့ separator = " ") ကို prototype မှာပဲ ရေးထားပါတယ်၊ နောက်က definition မှာ ပြန်မရေးပါဘူး။ Caller က argument တစ်ခု၊ နှစ်ခု ဒါမှမဟုတ် သုံးခု ပို့လို့ ရပြီး ပို့ထားတဲ့ ဘယ်ဘက်ဆုံး argument တွေကို အရင် parameter တွေနဲ့ ဆက်စပ်ပါတယ်၊ ကျန်တာကို default က ဖြည့်ပါတယ်။
#include <iostream>
#include <string>
std::string repeat(const std::string& text, int times = 2,
const std::string& separator = " ");
int main() {
std::cout << repeat("ha") << '\n';
std::cout << repeat("ha", 3) << '\n';
std::cout << repeat("ha", 3, "-") << '\n';
return 0;
}
std::string repeat(const std::string& text, int times, const std::string& separator) {
std::string result;
for (int i = 0; i < times; ++i) {
if (i > 0) {
result += separator;
}
result += text;
}
return result;
}Output က ha ha၊ ha ha ha နဲ့ ha-ha-ha ပါ။ Default တွေကို definition မှာပါ ထပ်ရေးရင် error: default argument given for parameter 2 of ... [-fpermissive] ဖြစ်ပြီး default မပါတဲ့ parameter ကို default ပါတဲ့ parameter ရဲ့ နောက်မှာ ထားရင် error: default argument missing for parameter 2 ... ဖြစ်ပါတယ်။
Overloading: နာမည်တူ၊ parameter မတူ
Overloading ကို သုံးရင် operation တစ်ခုတည်းကို type မတူတဲ့ data တွေအတွက် နာမည်တစ်ခုတည်းနဲ့ ခေါ်နိုင်ပါတယ်။ Compiler က argument ရဲ့ type ကို ကြည့်ပြီး ကိုက်ညီဆုံး overload ကို ကွန်ပိုင်းအချိန်မှာ ရွေးပါတယ်၊ ဒါကြောင့် run time မှာ ကုန်ကျစရိတ် မရှိပါဘူး။ Literal 42 က int၊ 4.2 က double ဖြစ်ပြီး string literal "forty-two" ကိုတော့ const std::string& ဆီ ပြောင်းပြီး ကိုက်ညီစေပါတယ်။
#include <iostream>
#include <string>
void describe(int value) { std::cout << "int: " << value << '\n'; }
void describe(double value) { std::cout << "double: " << value << '\n'; }
void describe(const std::string& value) { std::cout << "string: " << value << '\n'; }
int main() {
describe(42);
describe(4.2);
describe("forty-two");
return 0;
}ညီတူ မကိုက်တဲ့ call က ambiguous ဖြစ်တယ်
int add(int, int) နဲ့ double add(double, double) နှစ်ခုရှိရင် add(5, 2.5) က compile မဖြစ်ပါဘူး — error: call of overloaded 'add(int, double)' is ambiguous။ ပထမ argument အတွက် ပထမ overload က ပိုကိုက်ပြီး ဒုတိယ argument အတွက် ဒုတိယ overload က ပိုကိုက်လို့ compiler က ဘယ်ဟာကိုမှ မရွေးနိုင်ပါဘူး။ Argument တစ်ခုကို static_cast နဲ့ ပြောင်းပါ၊ ဒါမှမဟုတ် overload အသစ်တစ်ခု ထပ်ထည့်ပါ။
Copy၊ const reference နဲ့ reference ကို နှိုင်းယှဉ်ကြည့်ခြင်း
ဒီ snippet က discount function နှစ်မျိုးကို တစ်ခုတည်းသော price variable ပေါ်မှာ run ပြပါတယ်။ ပထမတစ်ခုက copy ကို ပြင်လို့ price က 2000 အတိုင်း ရှိနေပြီး ဒုတိယတစ်ခုက double& ကိုသုံးလို့ caller ရဲ့ variable ကို တိုက်ရိုက် 1800 ပြောင်းပါတယ်။ countVowels() ကတော့ text ကို ဖတ်ရုံပဲ လိုလို့ const std::string& ကို သုံးထားပြီး string တစ်ခုလုံးကို copy မကူးပါဘူး။
#include <iostream>
#include <string>
void discountCopy(double price, double percent) {
price -= price * percent / 100;
}
void discountInPlace(double& price, double percent) {
price -= price * percent / 100;
}
std::size_t countVowels(const std::string& text) {
std::size_t count = 0;
for (char c : text) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
++count;
}
}
return count;
}
int main() {
double price = 2000;
discountCopy(price, 10);
std::cout << "After discountCopy: " << price << '\n';
discountInPlace(price, 10);
std::cout << "After discountInPlace: " << price << '\n';
std::cout << "Vowels: " << countVowels("mingalaba") << '\n';
return 0;
}Output က After discountCopy: 2000၊ After discountInPlace: 1800 နဲ့ Vowels: 4 ပါ။ ဒုတိယ discount က price ကို ပြောင်းလို့ ပထမ discount ရဲ့ ရလဒ်ကို မမြင်ရတော့ပါဘူး၊ ဒါပေမယ့် function တစ်ခုက argument ကို တိတ်တဆိတ် ပြောင်းသွားတာထက် ရလဒ်ကို return ပြန်တာက ဖတ်ရတာ ပိုရှင်းလေ့ရှိပါတယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Real project တွေက ပရိုဂရမ်ကို function တိုတိုလေးတွေအဖြစ် ခွဲပြီး function တစ်ခုစီက အလုပ်တစ်ခုပဲ လုပ်ပါတယ်။ ပိုကြီးတဲ့ code base တွေမှာ function တွေကို prototype တွေအဖြစ် header file (.h ဒါမှမဟုတ် .hpp) ထဲ ထားပြီး definition တွေကို source file (.cpp) ထဲ ထားကြပါတယ်၊ ဒါမှ တခြား ဖိုင်တွေက function ရဲ့ signature ကိုပဲ include လုပ်ပြီး ရေးထားတဲ့ ပုံစံကို မသိရဘဲ ခေါ်လို့ရပါတယ်။ ဖိုင်တစ်ခု ပြောင်းရင် ဖိုင်အားလုံးကို ပြန် compile လုပ်စရာ မလိုတာကြောင့် build ကလည်း မြန်ပါတယ်။
Standard library ကိုယ်တိုင်က overloading ကို အများကြီး သုံးပါတယ်: std::to_string က int, double စတဲ့ type တွေအတွက် overload တွေ ရှိပြီး std::abs တို့ std::max တို့ကလည်း ဒီလိုပါပဲ။ API ကောင်းတွေက ကြီးတဲ့ object တွေကို const& နဲ့ လက်ခံပြီး default argument တွေနဲ့ တောင်းဆိုမှု မရှိမဖြစ်လိုတဲ့ အပိုင်းတွေကိုပဲ မေးပါတယ်၊ ဥပမာ log(message, level = Info) လိုပါ။ Function ကို တိုတိုနဲ့ နာမည်ကောင်းကောင်း ပေးထားရင် test ရေးရတာလည်း လွယ်ပါတယ်၊ input နဲ့ output ကို တိုက်ရိုက် စစ်လို့ရလို့ပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. လမ်းကြောင်းတစ်ခုမှာ return မေ့တာ
int grade(int mark) { if (mark >= 50) { return 1; } } မှာ mark က 50 အောက်ဆိုရင် function က return မပါဘဲ ပြီးသွားပါတယ်၊ ဒါက undefined behaviour ပါ။ g++ -Wall က warning: control reaches end of non-void function [-Wreturn-type] လို့ ပြောပေမယ့် compile လုပ်ပေးလို့ warning ကို မကြည့်ရင် လွတ်သွားပါတယ်။ လမ်းကြောင်းတိုင်းမှာ return ထည့်ပါ၊ -Werror=return-type ကိုလည်း ဖွင့်ထားပါ။
2. By value parameter က caller ကို ပြောင်းမယ်လို့ ထင်တာ
void applyDiscount(double price, ...) က copy ကိုပဲ ပြောင်းလို့ caller ရဲ့ price က မပြောင်းပါဘူး၊ compiler ကလည်း ဘာမှ မပြောပါဘူး။ ပြောင်းစေချင်ရင် double& price လို့ ကြေညာပါ၊ ဒါမှမဟုတ် double ကို return ပြန်ပါ။ ပြောင်းစေမယ်မဟုတ်ရင် ကြီးတဲ့ type တွေကို const& နဲ့ ယူတာက မလိုအပ်တဲ့ copy ကို ရှောင်ပေးပါတယ်။
3. ကြေညာခြင်းမတိုင်ခင် function ကို ခေါ်တာ
main က square(3) ကို ခေါ်ပေမယ့် square ရဲ့ definition က main အောက်မှာ ရှိနေရင် error: 'square' was not declared in this scope ဖြစ်ပါတယ်။ Compiler က အပေါ်ကနေ အောက်ကို ဖတ်ပြီး ခေါ်တဲ့နေရာမှာ square ကို မသိသေးလို့ပါ။ int square(int n); ဆိုတဲ့ prototype ကို main အပေါ်မှာ ထည့်ပါ၊ ဒါမှမဟုတ် definition ကို အပေါ်ကို ရွှေ့ပါ။
4. Overload ambiguous ဖြစ်စေတာ
int add(int, int) နဲ့ double add(double, double) ရှိတဲ့အခါ add(5, 2.5) ကို ခေါ်ရင် error: call of overloaded 'add(int, double)' is ambiguous ဖြစ်ပါတယ်။ Argument type တွေ ကိုက်အောင် ပို့ပါ (add(5.0, 2.5))၊ ဒါမှမဟုတ် ရှင်းလင်းတဲ့ cast ကို သုံးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Function တစ်ခုကို အလုပ်တစ်ခုပဲ ပေးပြီး calculateTotal လို အလုပ်ကို ဖော်ပြတဲ့ နာမည် ပေးပါ။
- သေးတဲ့ type တွေကို by value၊ ကြီးပြီး ဖတ်ရုံပဲ လိုတဲ့ type တွေကို const& နဲ့ ပို့ပါ၊ ပြင်ဖို့ လိုမှ non-const reference သုံးပါ။
- ပိုရှင်းရင် ရလဒ်ကို reference parameter နဲ့ ပြောင်းတာထက် return ပြန်ပါ။
- -Wall -Wextra နဲ့ compile လုပ်ပြီး return လမ်းကြောင်း ကျန်ခဲ့တဲ့ warning ကို error လို သဘောထားပါ။
Mini challenge
Mini challenge: receipt helper များ
ဆိုင်တစ်ဆိုင်ရဲ့ receipt အတွက် function လေးခု ရေးပြီး main ထဲမှာ ပေါင်းသုံးပါ — default argument၊ const reference၊ non-const reference နဲ့ overloading တစ်ခုစီ လေ့ကျင့်ရပါမယ်။
- lineTotal(unitPrice, quantity) — quantity ရဲ့ default က 1 ဖြစ်ရမယ်
- label(name, quantity) — name ကို const std::string& နဲ့ လက်ခံပြီး "Tea x3" လို string ကို return ပြန်ရမယ်
- applyCoupon(total, percent) — total ကို reference နဲ့ လက်ခံပြီး percent ရာခိုင်နှုန်း လျှော့ပြီး caller ရဲ့ variable ကို ပြောင်းရမယ်
- printAmount ကို int နဲ့ double အတွက် overload နှစ်ခု ရေးပါ
#include <iostream>
#include <string>
// TODO: int lineTotal(int unitPrice, ...) quantity defaults to 1
// TODO: std::string label(...) name by const reference
// TODO: void applyCoupon(...) total by reference
// TODO: void printAmount(int ...) and void printAmount(double ...)
int main() {
// TODO: tea: 1500 Ks x 3, biscuits: 800 Ks (default quantity)
// TODO: print the label and line total of the tea
// TODO: sum both lines, apply a 10 percent coupon, print the total
// TODO: print 2.5 with the double overload
return 0;
}ရလဒ်: Output က ဒီလို ထွက်ရပါမယ်: Tea x3: 4500 Ks Total after coupon: 4770 Ks Half portion: 2.5 Ks
အရိပ်အမြွက် ကြည့်ရန်
Default argument ကို parameter list ရဲ့ နောက်ဆုံးမှာ ထားပါ (int quantity = 1)။ "Tea x" + std::to_string(quantity) လို std::to_string ကို သုံးပြီး number ကို string ဖြစ်စေပါ၊ <string> ထဲမှာ ပါပါတယ်။ applyCoupon ထဲမှာ total -= total * percent / 100; လို့ ရေးပါ။
အဖြေ ကြည့်ရန်
#include <iostream>
#include <string>
int lineTotal(int unitPrice, int quantity = 1) {
return unitPrice * quantity;
}
std::string label(const std::string& name, int quantity) {
return name + " x" + std::to_string(quantity);
}
void applyCoupon(int& total, int percent) {
total -= total * percent / 100;
}
void printAmount(int ks) {
std::cout << ks << " Ks\n";
}
void printAmount(double ks) {
std::cout << ks << " Ks\n";
}
int main() {
int tea = lineTotal(1500, 3);
int biscuits = lineTotal(800);
std::cout << label("Tea", 3) << ": " << tea << " Ks\n";
int total = tea + biscuits;
applyCoupon(total, 10);
std::cout << "Total after coupon: ";
printAmount(total);
std::cout << "Half portion: ";
printAmount(2.5);
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Function တစ်ခုမှာ return type၊ နာမည်၊ parameter list နဲ့ body ပါပြီး return က value ကို ပြန်ပို့ကာ function ကို ပြီးဆုံးစေပါတယ်။
- Compiler က အပေါ်ကနေ အောက်ကို ဖတ်လို့ ခေါ်ခင် ကြေညာပါ — definition ကို အပေါ်ထား၊ ဒါမှမဟုတ် prototype ထည့်ပါ။
- Argument တွေက default အနေနဲ့ copy ကူးပါတယ်၊ ဖတ်ရုံအတွက် const&၊ ပြင်ဖို့အတွက် non-const reference ကို သုံးပါ။
- Default argument တွေက နောက်ဆုံး parameter တွေမှာ တစ်နေရာတည်းမှာပဲ ရှိရပြီး overload တွေက parameter type ကွဲမှ ဖြစ်ပါတယ်။
- return လမ်းကြောင်းတိုင်းကို စစ်ပါ၊ -Wall -Wextra နဲ့ compile လုပ်ပါ။
The Current ISO C++ Standard (C++23) — Standard C++ Foundation