Arduino Function Example with Plain Speak


1. Function Declaration


int myAddFunction(int num1, int num2){

  • int - This function will return an integer (whole number)
  • myAddFunction - This function name is myAddFunction
  • ( - Here is the beginning of the function parameters.
  • int num1 - Make a variable to hold integers and give it the name num1. This is the first parameter of this function.
  • int num2 - Make a variable to hold integers and give it the name num2. This is the second parameter of this function.
  • ) - Here is the end of the function parameters.
  • { - Here is the beginning of the tasks that this function performs

int result;

  • int result - Make a variable to hold integers and give it the name result.
  • ; - This is the end of this task.

result = num1 + num2;

  • result = num1 + num2 - Add the value of num1 to the value of num2 and put the answer into the variable named result.
  • ; - This is the end of this task.

return result;

  • return result - Send the value of the variable result back to the place that called this function.
  • ; - This is the end of this task.

}

  • } - Here is the end of the tasks that this function performs

2. Calling the Function


int additionVariable;

  • int additionVariable - Make a variable to hold integers and give it the name additionVariable.
  • ; - This is the end of this task.

additionVariable = myAddFunction(3, 2);

  • myAddFunction(3,2) - Call up the function named myAddFunction and give it the parameters 3 and 2.
  • additionVariable = - Put whatever is returned from the function myAddFunction into the variable named additionVariable.
  • ; - This is the end of this task.

[Back to the Function Section of Programming Principles]