# 函數 - 自製指令 - 將一段程式碼用指定語法包裝起來,變成一個可以方面重複呼叫的指令 - 語法 - 宣告設計你的指令 ```c= 修飾字 名字(參數組合){程式} ``` ```c= void love2(){ puts("love"); puts("love") } ``` - 呼叫`love2();` - 備註void是不用回傳 - C語言程式大概可以分成四區 - 先行處理 - 公共宣告區 - 主程式 - 補充區 - 參數就是給{}中程式碼用的變數 ```c= void lovec(int C){ ... } ``` 傳參數 ```c= #include <stdio.h> void hello(int x) { int y; for (y = 0; y < x; y++) { puts("hihihi"); } } int main() { int user; puts("輸入:"); scanf("%d", &user); hello(user); return 0; } ``` - 補充:字串指標 `char *x;` `%s`(string) - char *x="x" - 老師的範例: ```c= void action_count(char *action, int count) { int i; for (i = 0; i < count; i++) { printf("I %s YOU\n", action); } } ``` 引用(使用?): ```c=+ action_count("Mew",10); ``` - 還可以自製標頭檔 - 引用方法`#include "自製.h"` - 內容寫法跟一般程式一樣 - 備註Cache快照?緩存? - Sync同步 - 有老師骰子寫的標頭檔 ```c= #include <stdlib.h> #include <time.h> int throw_dice() //回傳值會自己傳到這個括號中 { srand(time(NULL)); int x = rand() % 6 + 1; return x; //注意這個回傳值 } ``` 在一個程式中引用 ```c= #include <stdio.h> #include "jgl.h" /*不用再寫 #include <stdlib.h> #include <time.h> 因為已經包含在標頭檔jgl.h裡面 */ int main(int argc, char const *argv[]) { printf("隨意擲個骰子得到%d\n", throw_dice()); return 0; } ```