Functions

Functions allow you to give a name to a block of code, that can then be called wherever you want, and as many times as you want. Functions (sometimes called methods) are an integral part of keeping code manageable and clear.

Consider this code:

void loop(){
  int a = 2;
  int b = 3;

  int k;

  k = a * b;
}

Instead of multiplying a and b, we can send that operation to another part of the code:

void loop(){
  int a = 2;
  int b = 3;

  int k;

  k = doSomethingWith(a, b);
}

For the above to work, we must define what “doSomethingWith” means. *That* is the function definition.:


int doSomethingWith(int a, int b){
  int result;
  result = a * b;
  return result;
}