函數外部聲明有什麼作用? 讓我們定義的函數應用範圍更廣,生命更長久。共用。 也就是說所有的外部函數都可以直接調用。 ...
1 #include <iostream> 2 using namespace std; 3 4 int main(){ 5 //求兩數的和? 6 7 8 int a,b,s; 9 cout<<"請你輸入兩個整型的數字:"<<endl; 10 cin>>a>>b; 11 int sum(int x ,int y); 12 s=sum(a,b);//實際參數 ,代表具體數值,在()當中 13 cout<<"The sum of a and b is:"<<sum<<endl; 14 //system("pause"); 15 return 0; 16 } 17 //首先確定函數是否需要返回值?需要返回值的話要寫返回值類型 如果不需要返回值則寫void 18 int sum(int x ,int y){//形式參數 //變數的生命周期 接收實際參數的賦值 int x=a,int y=b; 19 return x+y; 20 }
函數外部聲明有什麼作用?
讓我們定義的函數應用範圍更廣,生命更長久。共用。
也就是說所有的外部函數都可以直接調用。
#include <iostream> using namespace std; int sum(int x ,int y); int main(){ //求兩數的和? int a,b,s; cout<<"請你輸入兩個整型的數字:"<<endl; cin>>a>>b; s=sum(a,b);//實際參數 ,代表具體數值,在()當中 cout<<"The sum of a and b is:"<<sum<<endl; //system("pause"); return 0; } //首先確定函數是否需要返回值?需要返回值的話要寫返回值類型 如果不需要返回值則寫void int sum(int x ,int y){//形式參數 //變數的生命周期 接收實際參數的賦值 int x=a,int y=b; return x+y; }