若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。
函式在嘻語言裡無所不在,例如printf就是一個函式,為標準函式之一,若沒提供一些積木的函式給開發者使用,那不就要開發者自行做積木嗎?
函式在嘻語言裡無所不在,例如printf就是一個函式,為標準函式之一,若沒提供一些積木的函式給開發者使用,那不就要開發者自行做積木嗎?
若要自行撰寫函式又要如何鑽寫呢?函式的定義如下:
傳回值型態 函數名稱(參數一型態 參數一名稱, 參數二型態 參數二名稱, ....)
{
變數宣告
程式碼
return 傳回值;
}
例如下面是y=x^2的函式:
intpower(int x) {
inty = x * x;
returny;
}
範例:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int power(int x); // 這叫函式原型,是用來騙編譯器的 | |
int main(void) { | |
int result = power(3); | |
printf("printf本身就是函數囉!\n"); | |
printf("3^2 = %d", result); | |
return 0; | |
} | |
int power(int x) { | |
int y = x * x; | |
return y; | |
} |
參考網址:
沒有留言:
張貼留言