Contenu connexe
Plus de requiemformemories (15)
Ch6 函式
- 6. 參數預設值
★ 範例 - 自由落體:
#include<iostream>
using namespace std;
float freefall(int t,float g=9.8){
return g*t;
}
int main(){
int x1=10;
cout<<freefall(1)<<endl;
cout<<freefall(2,x1)<<endl;
}
預設參數
使用預
設參數
執行結果
注意 !!
預設的參數要集中在
右方宣告喔
- 7. 範例:不回傳 的函式值
★ 範例 - 輸出相同文字:
#include<iostream>
using namespace std;
void x(void){
cout<<"Hello!"<<endl;
}
int main(){
x();
x();
x();
}
執行結果
void 為「空」
之意