Show minor edits - Show changes to markup
void loop{
void loop() {
int sval;
int sval = 0;
Functions allow a programmer to create modular pieces of code that performs a defined task and then returns to the area of code from which the function was "called". The typical case for creating a function is when one needs to perform the same action multiple times in a program.
For programmers accustomed to using BASIC, functions in Arduino provide (and extend) the utility of using subroutines (GOSUB in BASIC).
Segmenting code into functions allows a programmer to create modular pieces of code that perform a defined task and then return to the area of code from which the function was "called". The typical case for creating a function is when one needs to perform the same action multiple times in a program.
For programmers accustomed to using BASIC, functions in Arduino provide (and extend) the utility of using subroutines (GOSUB in BASIC).
}
Our function needs to be declared outside any other function, so "myMultiplyFunction()" can go above or below the "loop()" function.
Our function needs to be declared outside any other function, so "myMultiplyFunction()" can go either above or below the "loop()" function.
This function will read a sensor five time with analogRead() and calculate the average of five readings. It then scales the data to 8 bits (0-255), and inverts it, returning the inverted result.
This function will read a sensor five times with analogRead() and calculate the average of five readings. It then scales the data to 8 bits (0-255), and inverts it, returning the inverted result.
The function will read a sensor five time with analogRead() then calculate the average of five readings. It then scales the data to 8 bits (0-255) and inverts it.
This function will read a sensor five time with analogRead() and calculate the average of five readings. It then scales the data to 8 bits (0-255), and inverts it, returning the inverted result.
int sval;
i = i + analogRead(0); // sensor on analog pin 0
sval = sval + analogRead(0); // sensor on analog pin 0
i = i / 5; // average i = i / 4; // scale to 8 bits (0 - 255) i = 255 - i; // invert output return i;
sval = sval / 5; // average sval = sval / 4; // scale to 8 bits (0 - 255) sval = 255 - sval; // invert output return sval;
i = i / 5; // average i = i / 4; // scale to 8 bits (0 - 255)
i = i / 5; // average i = i / 4; // scale to 8 bits (0 - 255)
i = i + analogRead(0);
i = i + analogRead(0); // sensor on analog pin 0
@]
[@ int ReadSens_and_Condition(){ int i;
for (i = 0; i < 5; i++){
i = i + analogRead(0);
}
i = i / 5; // average i = i / 4; // scale to 8 bits (0 - 255) i = 255 - i; // invert output return i;
}
int ReadSens_and_Condition(){
int i;
for (i = 0; i < 5; i++){
i = i + analogRead(0);
}
i = i / 5; // average
i = i / 4; // scale to 8 bits (0 - 255)
i = 255 - i; // invert output
return i;
}
To call our function we just assign it to a variable.
[@int sens;
sens = ReadSens_and_Condition();
@]
For programmers accustomed to using BASIC, functions in Arduino provide (and extend) the utility of using subroutines (GOSUB).
For programmers accustomed to using BASIC, functions in Arduino provide (and extend) the utility of using subroutines (GOSUB in BASIC).
To "call" our simple multiply function we pass it the datatypes it is expecting:
To "call" our simple multiply function, we pass it parameters of the datatype that it is expecting:
In a program to keep track of school records, we move a student up a grade if they are old enough, or if they have passed a test, but only if they have paid their tuition.
[@ if (student_age > x) {
if (tuition == "paid") {
student_grade++;
}
The function will read a sensor five time with analogRead() then calculate the average of five readings. It then scales the data to 8 bits (0-255) and inverts it.
[@ int ReadSens_and_Condition(){ int i;
for (i = 0; i < 5; i++){
i = i + analogRead(0);
if (test_score > y) {
if (tuition == "paid") {
student_grade++;
}
i = i / 5; // average i = i / 4; // scale to 8 bits (0 - 255) i = 255 - i; // invert output return i;
However, if we later want to change tuition to a numerical test showing that they owe us less than a hundred dollars -- tuition < 100; -- we have to change the code in two places, greatly increasing the risk of bugs if we change it one place and forget to change it in the other.
A function helps by giving a block of code a name, then letting you call the entire block with that name. As a result, when you need to changed the named code, you only have to change it in one place.
Our function looks like this:
// tell us the type of data the function expects
void tuitionTest(int tuition_balance) {
if (tuition_balance < 100) {
student_grade++;
}
}
And our code looks like this:
if (student_age > x) {
tuitionTest(tuition_balance);
}
if (test_score > y) {
tuitionTest(tuition_balance);
}
[@
void setup(){
[@void setup(){
Serial.begin(9600);
Serial.begin(9600);
int i = 2; int j = 3; int k;
k = myMultiplyFunction(i, j); // k now contains 6 Serial.println(k); delay(500);
int i = 2; int j = 3; int k;
k = myMultiplyFunction(i, j); // k now contains 6 Serial.println(k); delay(500);
int result; result = x * y; return result;
int result; result = x * y; return result;
The entire sketch would look like this:
The entire sketch would then look like this:
}
@]
return result; }@]
Standardizing code fragments into functions has severaladvantages:
Standardizing code fragments into functions has several advantages:
There are two required functions in an Arduino sketch, setup() and loop(). Other functions must be created outside the brackets of those two functions. As an example, we will create a simple function to multiply two numbers.
int myMultiplyFunction(int i, int j){
int myMultiplyFunction(int x, int y){
result =
result = x * y; }
int result; result =
int: i = 2; int: j = 3; int: k;
int i = 2; int j = 3; int k;
In a program to keep track of school records, we move a student up a grade if they are old enough, or if they have passed a test, but only if they have paid their tuition.
Our function needs to be declared outside any other function, so "myMultiplyFunction()" can go above or below the "loop()" function.
The entire sketch would look like this:
if (student_age > x) {
if (tuition == "paid") {
student_grade++;
}
void setup(){ Serial.begin(9600);
if (test_score > y) {
if (tuition == "paid") {
student_grade++;
}
void loop{ int i = 2; int j = 3; int k;
k = myMultiplyFunction(i, j); // k now contains 6 Serial.println(k); delay(500);
int myMultiplyFunction(int i, int j){
However, if we later want to change tuition to a numerical test showing that they owe us less than a hundred dollars -- tuition < 100; -- we have to change the code in two places, greatly increasing the risk of bugs if we change it one place and forget to change it in the other.
A function helps by giving a block of code a name, then letting you call the entire block with that name. As a result, when you need to changed the named code, you only have to change it in one place.
Our function looks like this:
In a program to keep track of school records, we move a student up a grade if they are old enough, or if they have passed a test, but only if they have paid their tuition.
// tell us the type of data the function expects void tuitionTest(int tuition_balance) {
if (tuition_balance < 100) {
if (student_age > x) {
if (tuition == "paid") {
if (test_score > y) {
if (tuition == "paid") {
student_grade++;
}
}
And our code looks like this:
However, if we later want to change tuition to a numerical test showing that they owe us less than a hundred dollars -- tuition < 100; -- we have to change the code in two places, greatly increasing the risk of bugs if we change it one place and forget to change it in the other.
A function helps by giving a block of code a name, then letting you call the entire block with that name. As a result, when you need to changed the named code, you only have to change it in one place.
Our function looks like this:
if (student_age > x) {
tuitionTest(tuition_balance);
// tell us the type of data the function expects void tuitionTest(int tuition_balance) {
if (tuition_balance < 100) {
student_grade++;
}
if (test_score > y) {
tuitionTest(tuition_balance);
}
And our code looks like this:
if (student_age > x) {
tuitionTest(tuition_balance);
}
if (test_score > y) {
tuitionTest(tuition_balance);
}
To "call" our simple multiply function we pass it the datatypes is it expecting:
To "call" our simple multiply function we pass it the datatypes it is expecting:


In a program to keep track of school records, we move a student up a grade if they are old enough, or if they have passed a test, but only if they have paid their tuition.
To "call" our simple multiply function we pass it the datatypes is it expecting:
if (student_age > x) {
if (tuition == "paid") {
student_grade++;
}
} if (test_score > y) {
if (tuition == "paid") {
student_grade++;
}
}
void loop{ int: i = 2; int: j = 3; int: k;
k = myMultiplyFunction(i, j); // k now contains 6
However, if we later want to change tuition to a numerical test showing that they owe us less than a hundred dollars -- tuition < 100; -- we have to change the code in two places, greatly increasing the risk of bugs if we change it one place and forget to change it in the other.
A function helps by giving a block of code a name, then letting you call the entire block with that name. As a result, when you need to changed the named code, you only have to change it in one place.
Our function looks like this:
In a program to keep track of school records, we move a student up a grade if they are old enough, or if they have passed a test, but only if they have paid their tuition.
// tell us the type of data the function expects void tuitionTest(int tuition_balance) {
if (tuition_balance < 100) {
if (student_age > x) {
if (tuition == "paid") {
if (test_score > y) {
if (tuition == "paid") {
student_grade++;
}
}
And our code looks like this:
However, if we later want to change tuition to a numerical test showing that they owe us less than a hundred dollars -- tuition < 100; -- we have to change the code in two places, greatly increasing the risk of bugs if we change it one place and forget to change it in the other.
A function helps by giving a block of code a name, then letting you call the entire block with that name. As a result, when you need to changed the named code, you only have to change it in one place.
Our function looks like this:
if (student_age > x) {
tuitionTest(tuition_balance);
// tell us the type of data the function expects void tuitionTest(int tuition_balance) {
if (tuition_balance < 100) {
student_grade++;
}
if (test_score > y) {
tuitionTest(tuition_balance);
}
And our code looks like this:
if (student_age > x) {
tuitionTest(tuition_balance);
}
if (test_score > y) {
tuitionTest(tuition_balance);
}




Standardizing code fragments into functions has several advantages:
Standardizing code fragments into functions has severaladvantages:
%width=50pxAttach:FunctionAnatom.gif


%width=50pxAttach:FunctionAnatom.gif
[[FunctionAnatom.gif | Anatomy of a function]
[[FunctionAnatom.gif | Anatomy of a function]
If you are using a version of Arduino prior to 0004, any function you create yourself the in the body of your code needs a function prototype at the beginning of your code, before the setup() code block. This is similar to the declaration of a variable, and essentially is just the first line of your function declaration, with a semicolon at the end.
void displayNumber(int incomingValue);
This tells Arduino what kind of function you are calling and what arguments it will pass.
For programmers accustomed to using BASIC, functions in C provide (and extend) the utility of using subroutines (GOSUB).
For programmers accustomed to using BASIC, functions in Arduino provide (and extend) the utility of using subroutines (GOSUB).
Functions allow you to create modular pieces of code that perform a defined task and then return you to the area of code from which the function was "called". The typical case for creating a function is when you need to perform the same action multiple times in one program.
Functions allow a programmer to create modular pieces of code that performs a defined task and then returns to the area of code from which the function was "called". The typical case for creating a function is when one needs to perform the same action multiple times in a program.
For programmers accustomed to using BASIC, functions in C provide (and extend) the utility of using subroutines (GOSUB).
Functions allow you to create modular pieces of code that perform certain tasks and then return you to the area of code it was executed from. The typical case for creating a function is when you need to perform the same action.
For instance, we move a student up a grade if they are old enough, or if they have passed a test, but only if they have paid their tuition.
Functions allow you to create modular pieces of code that perform a defined task and then return you to the area of code from which the function was "called". The typical case for creating a function is when you need to perform the same action multiple times in one program.
Standardizing code fragments into functions has several advantages:
In a program to keep track of school records, we move a student up a grade if they are old enough, or if they have passed a test, but only if they have paid their tuition.
[@
@]
Functions allow you to create modular pieces of code that perform certain tasks and then return you to the area of code it was executed from. Below is an example of a function being called:
Functions allow you to create modular pieces of code that perform certain tasks and then return you to the area of code it was executed from. The typical case for creating a function is when you need to perform the same action.
For instance, we move a student up a grade if they are old enough, or if they have passed a test, but only if they have paid their tuition.
displayNumber(value);
if (student_age > x) {
if (tuition == "paid") {
student_grade++;
}
} if (test_score > y) {
if (tuition == "paid") {
student_grade++;
}
}
When Arduino executes this line, it looks for this function's declaration somewhere in the code, passes the "value" variable put inside the () as an "argument" to the function. Below is an example of what the function declaration could look like:
However, if we later want to change tuition to a numerical test showing that they owe us less than a hundred dollars -- tuition < 100; -- we have to change the code in two places, greatly increasing the risk of bugs if we change it one place and forget to change it in the other.
A function helps by giving a block of code a name, then letting you call the entire block with that name. As a result, when you need to changed the named code, you only have to change it in one place.
Our function looks like this:
// tell us the type of data the function expects void tuitionTest(int tuition_balance) {
if (tuition_balance < 100) {
student_grade++;
}
}
And our code looks like this:
void displayNumber(int incomingValue){
printInteger(incomingValue); // other code in the function
}
if (student_age > x) {
tuitionTest(tuition_balance);
} if (test_score > y) {
tuitionTest(tuition_balance);
}
In C, any function you create yourself the in the body of your code needs a function prototype at the beginning of your code, before the setup() code block. This is similar to the declaration of a variable, and essentially is just the first line of your function declaration, with a semicolon at the end.
If you are using a version of Arduino prior to 0004, any function you create yourself the in the body of your code needs a function prototype at the beginning of your code, before the setup() code block. This is similar to the declaration of a variable, and essentially is just the first line of your function declaration, with a semicolon at the end.
This prepares C to know what kind of function you are calling and what arguments it will pass.
This tells Arduino what kind of function you are calling and what arguments it will pass.
displayNumber(int incomingValue){
void displayNumber(int incomingValue){
displayNumber(int incomingValue);
void displayNumber(int incomingValue);
In C, any function you create yourself the in the body of your code needs a function prototype at the beginning of your code, before the setup() code block. This is similar to the declaration of a variable, and essentially is just the first line of your function declaration.
In C, any function you create yourself the in the body of your code needs a function prototype at the beginning of your code, before the setup() code block. This is similar to the declaration of a variable, and essentially is just the first line of your function declaration, with a semicolon at the end.
displayNumber(int incomingValue)
displayNumber(int incomingValue);
[=
[@
=]
@]
[=
[@
=]
@]
@]
Functions allow you to create modular pieces of code that perform certain tasks and then return you to the area of code it was executed from. Below is an example of a function being called:
displayNumber(value);
When Arduino executes this line, it looks for this function's declaration somewhere in the code, passes the "value" variable put inside the () as an "argument" to the function. Below is an example of what the function declaration could look like:
[@ displayNumber(int incomingValue){
printInteger(incomingValue); // other code in the function
}
In C, any function you create yourself the in the body of your code needs a function prototype at the beginning of your code, before the setup() code block. This is similar to the declaration of a variable, and essentially is just the first line of your function declaration.
displayNumber(int incomingValue)
This prepares C to know what kind of function you are calling and what arguments it will pass.