return

설명

함수를 마치고 원한다면 함수를 부른 함수로 값을 반환한다.

문법

return; return value; // 두가지 모두 타당

매개변수

  • value: 변수 또는 상수

예제 코드

센서 입력을 임계치와 비교하는 함수

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

return 키워드는 버그 있을 수 있는 코드의 큰 섹션을 "주석 처리" 않고도 섹션을 테스트 하는데 편하다

void loop() { // 빛나는 코드 아이디어 테스트는 여기에 return; // 나머지 역기능 스케치는 여기에 // 이 코드는 실행 안 됨 }

Testing Example

void setup() { Serial.begin(9600); printMessage(); } void loop() { } void printMessage() { Serial.println("Hello, ArduinoGetStarted.com"); return; Serial.println("Hi, ArduinoGetStarted.com"); }

시리얼 모니터에 결과:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Hello, ArduinoGetStarted.com
Ln 11, Col 1
Arduino Uno on COM15
2

As we can see, the text after return is not printed.

더보기

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables .

※ OUR MESSAGES