Reference.FunctionDeclaration History

Show minor edits - Show changes to markup

September 24, 2011, at 01:21 AM by Tom Igoe -
Added line 36:
Changed lines 40-42 from:
to:
September 24, 2011, at 01:20 AM by Tom Igoe -
Changed lines 57-58 from:
to:
Changed lines 77-78 from:
to:
September 24, 2011, at 01:20 AM by Tom Igoe -
Changed lines 63-64 from:
to:
Changed lines 80-81 from:
to:
September 24, 2011, at 01:18 AM by Tom Igoe -
Changed line 27 from:

[@

to:
Changed line 35 from:

@]

to:
Changed line 39 from:

[@void setup(){

to:
Changed lines 57-58 from:

}@]

to:
Changed line 63 from:

[@int ReadSens_and_Condition(){

to:
Changed lines 75-76 from:

}@]

to:
Changed lines 79-80 from:

[@int sens;

to:
Changed lines 82-84 from:

@]

to:
February 21, 2011, at 11:09 PM by David A. Mellis -
Changed line 43 from:

void loop{

to:

void loop() {

June 13, 2010, at 10:45 PM by David A. Mellis -
Changed lines 65-66 from:
  int sval;
to:
  int sval = 0;
February 14, 2008, at 07:08 AM by Paul Badger -
Changed lines 3-6 from:

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).

to:

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).

December 03, 2007, at 04:59 AM by Paul Badger -
Added line 34:

}

Changed lines 36-37 from:

Our function needs to be declared outside any other function, so "myMultiplyFunction()" can go above or below the "loop()" function.

to:

Our function needs to be declared outside any other function, so "myMultiplyFunction()" can go either above or below the "loop()" function.

November 29, 2007, at 06:11 AM by Paul Badger -
November 29, 2007, at 06:07 AM by Paul Badger -
Changed lines 60-61 from:

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.

to:

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.

November 06, 2007, at 12:00 AM by Paul Badger -
Changed lines 60-61 from:

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.

to:

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.

November 05, 2007, at 11:58 PM by Paul Badger -
Changed lines 58-59 from:

A more complex example

to:

Another example

November 05, 2007, at 11:53 PM by Paul Badger -
Changed lines 64-65 from:
to:
  int sval;
Changed line 67 from:
    i = i + analogRead(0);    // sensor on analog pin 0
to:
    sval = sval + analogRead(0);    // sensor on analog pin 0
Changed lines 70-73 from:
  i = i / 5;    // average
  i = i / 4;    // scale to 8 bits (0 - 255)
  i = 255 - i;  // invert output
  return i;
to:
  sval = sval / 5;    // average
  sval = sval / 4;    // scale to 8 bits (0 - 255)
  sval = 255 - sval;  // invert output
  return sval;
November 05, 2007, at 11:50 PM by Paul Badger -
Changed lines 69-70 from:
  i = i / 5;  // average
  i = i / 4;  // scale to 8 bits (0 - 255)
to:
  i = i / 5;    // average
  i = i / 4;    // scale to 8 bits (0 - 255)
November 05, 2007, at 11:49 PM by Paul Badger -
Changed line 66 from:
    i = i +  analogRead(0);
to:
    i = i + analogRead(0);    // sensor on analog pin 0
Deleted lines 82-83:

@]

November 05, 2007, at 11:48 PM by Paul Badger -
Changed lines 62-78 from:

[@ 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:
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();

Added lines 81-84:

@]

November 05, 2007, at 11:45 PM by Paul Badger -
Changed lines 5-6 from:

For programmers accustomed to using BASIC, functions in Arduino provide (and extend) the utility of using subroutines (GOSUB).

to:

For programmers accustomed to using BASIC, functions in Arduino provide (and extend) the utility of using subroutines (GOSUB in BASIC).

Changed lines 25-26 from:

To "call" our simple multiply function we pass it the datatypes it is expecting:

to:

To "call" our simple multiply function, we pass it parameters of the datatype that it is expecting:

Changed lines 60-68 from:

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++;
  }
to:

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);
Changed lines 68-71 from:

if (test_score > y) {

  if (tuition == "paid") {
    student_grade++;
  }
to:
 i = i / 5;  // average
 i = i / 4;  // scale to 8 bits (0 - 255)
 i = 255 - i;  // invert output
 return i;
Added lines 74-78:

Deleted lines 79-100:

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);
}

November 05, 2007, at 11:21 PM by Paul Badger -
November 05, 2007, at 11:20 PM by Paul Badger -
Deleted line 21:
Deleted lines 24-25:
Changed lines 38-40 from:

[@

void setup(){

to:

[@void setup(){

November 05, 2007, at 11:17 PM by Paul Badger -
Changed line 44 from:

Serial.begin(9600);

to:
  Serial.begin(9600);
Changed lines 48-54 from:

int i = 2; int j = 3; int k;

k = myMultiplyFunction(i, j); // k now contains 6 Serial.println(k); delay(500);

to:
  int i = 2;
  int j = 3;
  int k;

  k = myMultiplyFunction(i, j); // k now contains 6
  Serial.println(k);
  delay(500);
Changed lines 58-60 from:

int result; result = x * y; return result;

to:
  int result;
  result = x * y;
  return result;
November 05, 2007, at 11:16 PM by Paul Badger -
Deleted line 36:
Deleted lines 37-38:
Changed line 40 from:

The entire sketch would look like this:

to:

The entire sketch would then look like this:

Changed lines 60-69 from:

}

@]

to:

return result; }@]

November 05, 2007, at 11:13 PM by Paul Badger -
Changed lines 7-8 from:

Standardizing code fragments into functions has severaladvantages:

to:

Standardizing code fragments into functions has several advantages:

Changed lines 11-12 from:
  • They codify one action in one place so that the function only has to be thought out and debugged once.
to:
  • Functions codify one action in one place so that the function only has to be thought out and debugged once.
Added lines 20-22:

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.

November 05, 2007, at 11:07 PM by Paul Badger -
Changed line 57 from:

int myMultiplyFunction(int i, int j){

to:

int myMultiplyFunction(int x, int y){

Changed lines 59-65 from:

result =

to:

result = x * y; }

November 05, 2007, at 10:58 PM by Paul Badger -
Changed lines 58-64 from:

to:

int result; result =

November 05, 2007, at 10:57 PM by Paul Badger -
Added lines 23-24:
Changed lines 29-32 from:

int: i = 2; int: j = 3; int: k;

to:

int i = 2; int j = 3; int k;

Changed lines 38-44 from:
A more complex example

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:

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:

Changed lines 42-45 from:

if (student_age > x) {

  if (tuition == "paid") {
    student_grade++;
  }
to:

void setup(){ Serial.begin(9600);

Changed lines 46-49 from:

if (test_score > y) {

  if (tuition == "paid") {
    student_grade++;
  }
to:

void loop{ int i = 2; int j = 3; int k;

k = myMultiplyFunction(i, j); // k now contains 6 Serial.println(k); delay(500);

Added lines 56-64:

int myMultiplyFunction(int i, int j){

Changed lines 66-70 from:

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:

to:

A more complex example

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.

Changed lines 75-77 from:

// tell us the type of data the function expects void tuitionTest(int tuition_balance) {

  if (tuition_balance < 100) {
to:

if (student_age > x) {

  if (tuition == "paid") {
Added lines 80-84:

if (test_score > y) {

  if (tuition == "paid") {
    student_grade++;
  }

}

Changed lines 86-90 from:

And our code looks like this:

to:

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:

Changed lines 92-93 from:

if (student_age > x) {

  tuitionTest(tuition_balance);
to:

// tell us the type of data the function expects void tuitionTest(int tuition_balance) {

  if (tuition_balance < 100) {
    student_grade++;
  }
Deleted lines 97-99:

if (test_score > y) {

  tuitionTest(tuition_balance);

}

Added lines 99-107:

And our code looks like this:

if (student_age > x) {
  tuitionTest(tuition_balance);
}
if (test_score > y) {
  tuitionTest(tuition_balance);
}

October 15, 2007, at 02:48 PM by Paul Badger -
Changed lines 23-24 from:

To "call" our simple multiply function we pass it the datatypes is it expecting:

to:

To "call" our simple multiply function we pass it the datatypes it is expecting:

October 15, 2007, at 02:45 PM by Paul Badger -
Changed lines 21-22 from:
to:
October 15, 2007, at 01:04 PM by Paul Badger -
Changed lines 23-25 from:

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:

To "call" our simple multiply function we pass it the datatypes is it expecting:

Changed lines 26-35 from:

if (student_age > x) {

  if (tuition == "paid") {
    student_grade++;
  }

} if (test_score > y) {

  if (tuition == "paid") {
    student_grade++;
  }

}

to:

void loop{ int: i = 2; int: j = 3; int: k;

k = myMultiplyFunction(i, j); // k now contains 6

Changed lines 34-38 from:

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:

to:

A more complex example

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.

Changed lines 44-46 from:

// tell us the type of data the function expects void tuitionTest(int tuition_balance) {

  if (tuition_balance < 100) {
to:

if (student_age > x) {

  if (tuition == "paid") {
Added lines 49-53:

if (test_score > y) {

  if (tuition == "paid") {
    student_grade++;
  }

}

Changed lines 55-59 from:

And our code looks like this:

to:

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:

Changed lines 61-62 from:

if (student_age > x) {

  tuitionTest(tuition_balance);
to:

// tell us the type of data the function expects void tuitionTest(int tuition_balance) {

  if (tuition_balance < 100) {
    student_grade++;
  }
Deleted lines 66-68:

if (test_score > y) {

  tuitionTest(tuition_balance);

}

Added lines 68-76:

And our code looks like this:

if (student_age > x) {
  tuitionTest(tuition_balance);
}
if (test_score > y) {
  tuitionTest(tuition_balance);
}

October 15, 2007, at 12:57 PM by Paul Badger -
Changed lines 21-23 from:

to:

October 15, 2007, at 12:57 PM by Paul Badger -
Changed lines 21-23 from:

to:

October 15, 2007, at 12:57 PM by Paul Badger -
Changed lines 7-10 from:

Standardizing code fragments into functions has several advantages:

  • Functions help the programmer stay organized. Often this helps to concpetualize the program.
to:

Standardizing code fragments into functions has severaladvantages:

  • Functions help the programmer stay organized. Often this helps to conceptualize the program.
Changed lines 21-25 from:

%width=50pxAttach:FunctionAnatom.gif

to:

October 15, 2007, at 12:51 PM by Paul Badger -
Changed lines 22-25 from:

to:

%width=50pxAttach:FunctionAnatom.gif

October 15, 2007, at 12:48 PM by Paul Badger -
Changed lines 22-25 from:
to:

October 15, 2007, at 12:46 PM by Paul Badger -
Changed lines 23-25 from:

[[FunctionAnatom.gif | Anatomy of a function]

to:
October 15, 2007, at 12:46 PM by Paul Badger -
Added line 22:
Added line 25:
October 15, 2007, at 12:46 PM by Paul Badger -
Changed lines 22-23 from:
to:

[[FunctionAnatom.gif | Anatomy of a function]

October 15, 2007, at 12:44 PM by Paul Badger -
Added lines 22-23:
July 17, 2007, at 08:28 PM by David A. Mellis - removing prototyping note... i hope no one is still using arduino 0003 or earlier
Deleted lines 57-67:

Prototyping, prior to 0004

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.

July 17, 2007, at 08:27 PM by David A. Mellis -
Changed lines 5-6 from:

For programmers accustomed to using BASIC, functions in C provide (and extend) the utility of using subroutines (GOSUB).

to:

For programmers accustomed to using BASIC, functions in Arduino provide (and extend) the utility of using subroutines (GOSUB).

July 17, 2007, at 01:30 PM by Paul Badger -
Changed lines 3-4 from:

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.

to:

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).

July 17, 2007, at 01:25 PM by Paul Badger -
Changed lines 11-12 from:
  • This reduces chances for errors in debugging and modification, if the code needs to be changed.
to:
  • This also reduces chances for errors in modification, if the code needs to be changed.
July 17, 2007, at 06:02 AM by Paul Badger -
Changed lines 15-17 from:
  • They make it easier to reuse code in other programs. Funcitons make programs more modular and flexible.

to:
  • They make it easier to reuse code in other programs by making it more modular, and as a nice side effect, using functions also often makes the code more readable.

July 17, 2007, at 05:51 AM by Paul Badger -
Changed lines 3-6 from:

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.

to:

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:

  • Functions help the programmer stay organized. Often this helps to concpetualize the program.

  • They codify one action in one place so that the function only has to be thought out and debugged once.

  • This reduces chances for errors in debugging and modification, if the code needs to be changed.

  • Functions make the whole sketch smaller and more compact because sections of code are reused many times.

  • They make it easier to reuse code in other programs. Funcitons make programs more modular and flexible.

Example

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.

April 16, 2007, at 04:30 PM by Paul Badger -
April 16, 2007, at 04:25 PM by Paul Badger -
Added line 51:
April 16, 2007, at 04:24 PM by Paul Badger -
Changed lines 1-2 from:

Functions

to:

Functions

Deleted lines 50-51:
October 01, 2006, at 12:54 PM by Clay Shirky -
Deleted line 18:
Changed line 24 from:
to:

[@

Changed line 31 from:
to:

@]

Deleted line 32:
October 01, 2006, at 12:53 PM by Clay Shirky - Updated example and prototyping note
Changed lines 3-4 from:

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:

to:

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.

Changed lines 8-17 from:

displayNumber(value);

to:

if (student_age > x) {

  if (tuition == "paid") {
    student_grade++;
  }

} if (test_score > y) {

  if (tuition == "paid") {
    student_grade++;
  }

}

Changed lines 20-21 from:

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:

to:

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:

Changed lines 36-39 from:

void displayNumber(int incomingValue){

  printInteger(incomingValue);
  // other code in the function

}

to:

if (student_age > x) {

  tuitionTest(tuition_balance);

} if (test_score > y) {

  tuitionTest(tuition_balance);

}

Changed lines 43-47 from:

Important (Prototyping)

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.

to:

Prototyping, prior to 0004

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.

Changed lines 52-53 from:

This prepares C to know what kind of function you are calling and what arguments it will pass.

to:

This tells Arduino what kind of function you are calling and what arguments it will pass.

March 26, 2006, at 06:11 PM by David A. Mellis - Added "void" to function as C++ (in release 0004) will require it.
Changed line 12 from:

displayNumber(int incomingValue){

to:

void displayNumber(int incomingValue){

Changed line 23 from:

displayNumber(int incomingValue);

to:

void displayNumber(int incomingValue);

March 25, 2006, at 12:41 PM by David A. Mellis - Prototypes end with semicolons.
Changed lines 20-21 from:

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.

to:

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.

Changed line 23 from:

displayNumber(int incomingValue)

to:

displayNumber(int incomingValue);

March 25, 2006, at 12:27 AM by Jeff Gray -
Changed line 5 from:

[=

to:

[@

Changed lines 7-8 from:

=]

to:

@]

Changed line 22 from:

[=

to:

[@

Changed lines 24-25 from:

=]

to:

@]

March 25, 2006, at 12:27 AM by Jeff Gray -
Changed lines 16-17 from:
to:

@]

March 25, 2006, at 12:26 AM by Jeff Gray -
Changed lines 1-2 from:

Functions

to:

Functions

March 25, 2006, at 12:26 AM by Jeff Gray -
Added lines 1-27:

Functions

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

}

Important (Prototyping)

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.

Reference Home

Share