- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Defining new functions in Arduino
Defining new functions in Arduino is equivalent to defining the functions in C.
The syntax is −
Syntax
return_type function_name(arg_type arg)
The only difference is that in C, the function needs to be declared at the top, if it is invoked before its definition. No such constraint exists in Arduino. The following code demonstrates this −
Example
void setup() { Serial.begin(9600); Serial.println(); } void loop() { // put your main code here, to run repeatedly: for (int i = 0; i < 10; i++) { long int w = square(i); Serial.println(w); delay(1000); } } long int square(int a) { return (a * a); }
The Serial Monitor output is shown below −
Output
As you can see, even though the function was invoked before being defined, Arduino did not throw up an error. The execution worked just as expected.
- Related Articles
- What are different ways of defining functions in JavaScript?
- Add a new board in Arduino
- Download a new library in Arduino IDE
- Add new header files in Arduino IDE
- Print new line and tab in Arduino
- Installing a new library in Arduino IDE 2.0
- Adding new column after a specific column and defining a default in MySQL?
- Store a new file in SD Card connected to Arduino
- What are the major new features in Arduino IDE 2.0?
- Defining Colors in CSS3
- Defining Keyframes in CSS3
- Defining static members in C++
- Defining a Function in Python
- Defining Class Methods in Perl
- PHP Defining namespace

Advertisements