C 初學(函式)

2021-08-18 08:51:07 字數 467 閱讀 3842

函式三要素:宣告,定義,返回值。

1. 按照先申明後使用的方法,在呼叫它的函式之前加一句申明。

#includeusing namespace std;

int fun1(int x);

//函式的宣告:可以刪除x; 可寫成void swap(int );

//宣告乙個變數是給編譯器看的,為了建棧用,顧不上名字是什麼,但一定得有型別。

//函式的宣告寫在呼叫該函式的main()之前

void main()

void main()

;//隱式定義空形參列表

void fun2(void) ;//為了與c語言相容,用void顯示的定義空形參列表

int fun3(int x,int y) ; //正確

int fun4(int x, y) ;//錯誤,哪怕y的型別也為int也需要在y前寫上「int」

C 函式初學總結

1 定義的語法形式 資料型別 函式名 形式參數列 函式體 執行語句 注釋說明 資料型別是返回值型別 void無返回值 函式名按照識別符號的取名規則可以任取 形式引數 可有可無,且可以是變數名 陣列名或指標名,作用是實現主調函式和被調函式的關係 函式不允許巢狀定義 函式定義的例子 定義乙個函式,返回兩...

C 初學記錄(sort函式)

sort函式 編譯 include includeusing namespace std define maxn 100 struct noderecord maxn 結構體排序 bool cmp const node a,const node b int main cout 函式理解 sort函式...

C 初學記錄(sort函式)

sort函式 編譯 include includeusing namespace std define maxn 100 struct noderecord maxn 結構體排序 bool cmp const node a,const node b int main cout 函式理解 sort函式...