include iostream include cstdlib using namespace std void
區 域 變 數 #include <iostream> #include <cstdlib> using namespace std; void func(void); int main(void) { int a=100; cout <<“before the func, a=” <<a <<endl; func(); cout <<“after the func, a=” <<a <<endl; system(“PAUSE”); return 0; } void func(void) { int a=300; cout <<“in the func, a=” <<a <<endl; return; } 執行結果: before the func, a=100 in the func, a=300 after the func, a=100
靜 態 區 域 變 數 #include <iostream> #include <cstdlib> using namespace std; void func(void); int main(void) { func(); system(“PAUSE”); return 0; } 執行結果: in the func, a=10 in the func, a=30 in the func, a=50 void func(void) { static int a=10; cout <<“in the func, a=” <<a <<endl; a+=20; return; }
外 部 變 數 #include <iostream> #include <cstdlib> using namespace std; void func(void); int main(void) { extern int a; cout <<“before the func, a=” <<a <<endl; func(); cout <<“after the func, a=” <<a <<endl; system(“PAUSE”); return 0; } int a=100; void func(void) { a=300; cout <<“in the func, a=” <<a <<endl; return; } 執行結果: before the func, a=100 in the func, a=300 after the func, a=300
引數傳遞 執行結果: before the add 10: a=3 b=5 after the add 10: a=3 b=5 #include <iostream> #include <cstdlib> using namespace std; void add 10(int, int); int main(void) { int a=3, b=5; cout << “before the add 10: n”; cout <<“ a=” <<a <<“ b=” <<b <<endl; add 10(a, b); cout << “after the add 10: n”; cout <<“ a=” <<a <<“ b=” <<b <<endl; system(“PAUSE”); return 0; } void add 10(int a, int b) { a=a+10; b=b+10; return; }
前置處理器 #define(續) n n 利用#define定義簡單的函數 ( 稱為巨 集)。 如: #define POWER i*i*i #define POWER(X) X*X*X #define POWER(X) (X)*(X)
#define #include <iostream> #include <cstdlib> using namespace std; #define PI 3. 1415926 double area(double); int main(void) { double r=1. 0; cout << “PI=” << PI << endl; cout << “area=” << area(r) << endl; system(“PAUSE”); return 0; } 執行結果: PI=3. 1416 area=3. 142 double area(double R) { return PI*R*R; }
#define 使用巨集(一) #include <iostream> #include <cstdlib> using namespace std; #define POWER i*i*i int main(void) { int i; cout << “Input an integer : ” << endl; cin >> i; //計算並印出 i 的 3 次方 cout <<i <<“*” <<i <<“=” <<POWER <<endl; system(“PAUSE”); return 0; } 執行結果: Input an integer : 3 3*3*3=27
#define 使用巨集(二) #include <iostream> #include <cstdlib> using namespace std; #define POWER(X) X*X*X int main(void) { int i; cout << “Input an integer : ” << endl; cin >> i; //計算並印出 i 的 3 次方 cout <<i <<“*” <<i <<“=” <<POWER(i) <<endl; system(“PAUSE”); return 0; } 執行結果: Input an integer : 2 2*2*2=8
#define 使用巨集(三) #include <iostream> #include <cstdlib> using namespace std; #define POWER(X) X*X*X int main(void) { int i; cout << “Input an integer : ” << endl; cin >> i; //計算並印出 i 的 3 次方 cout <<i+1 <<“*”<<i+1 <<“=”<<POWER(i+1) <<endl; system(“PAUSE”); return 0; } 執行結果: Input an integer : 2 3*3*3=7 POWER(i+1) = i+1*i+1 = 2+1*2+1
#define 使用巨集(四) #include <iostream> #include <cstdlib> using namespace std; #define POWER(X) (X)*(X) int main(void) { int i; cout << “Input an integer : ” << endl; cin >> i; //計算並印出 i 的 3 次方 cout <<i+1 <<“*”<<i+1 <<“=”<<POWER(i+1) <<endl; system(“PAUSE”); return 0; } 執行結果: Input an integer : 2 3*3*3=27 POWER(i+1) = (i+1)*(i+1) = (2+1)*(2+1)
- Slides: 18