return

終止函式的執行,同時也可以將一個值傳回至之前呼叫的函式。

語法


return;
return value; // 兩種語法都成立

參數

value: 可以是任何變數或常數型別。

範例

一個用來比對感測器輸入是否達到閥值的函式:

int checkSensor(){
    if (analogRead(0) > 400) {
        return 1;
    else{
        return 0;
    }
}

用return的話可以簡單的去測試一段程式碼,而不需要去註解掉一大段很可能有bug的程式碼。

void loop(){

  // 需要被測試的程式碼

  return;

  // 要被略過的程式碼
}

See also

comments


語法參考主頁面

本頁由熱血青年 LBU 譯自英文版。

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.