//-----------
//Function Tutorial
//copyright Louis H. Katz 2012
//this code is public domain, but I would appreciate credit and an email.
/* 
This will be easier to read if you copy and paste into an Arduino Sketch.
The purpose of this code is to illustrate how data can be passed to and from functions using function parameters and return results as well as a function that requires no parameter and returns no result.
There are two function calls in this program (other than "setup", and "loop" and the library calls "Serial..." and "pow"). 
These two functions are funcName and incrementB.
The first fuction named funcName has the contents of variable "passToFunct" passed to it. 
Inside the function this data is placed in the variable "numberPassedFromLoop". 
funcName returns the result of rasing "numberPassedFromLoop" to the power of "b".
The second function called "incrementB" just increments "b". 
It accepts no input, it is called with the variable type "void".
incrementB returns no value, except that it increments global value "b";
Because the math library function "pow()" returns a float value the variable holding the return value, "power" must be a float.
If it is set as int, then rounding errors occur
When b =33 the pow() function returns 0 as the result is beyond its limit.
*/
//global variables are declared before (above) the "setup" function.
  int sum = 0;         //global variable declaration, may be accessed inside and outside functions
  int product = 0;     //global variable declaration, may be accessed inside and outside functions

  int productNsum =0; //global variable declaration, may be accessed inside and outside functions
  int b=3;            //global variable declaration, may be accessed inside and outside functions
void setup() 
{
  Serial.begin (9600); /* This sets up the Serial library. It really is a function call and sends the function "begin"
  a value of 9600 baud.*/
  Serial.println("Function Use Demo");/*This calls the println function in the serial library and tells 
it to print the value "Function Use Demo"*/
}
void loop(){
   int passToFunct = 2; //declaration inside the loop function can only be used inside the loop function
   float power= functName(passToFunct); // "power" is the result from the function
 Serial.print ("a= ");Serial.print (passToFunct); Serial.print (" , "); Serial.print ("b= ");Serial.println (b);
 Serial.print ("a+b="); Serial.print (sum); Serial.print ("a*b= "); Serial.println (product);
 Serial.print ("a**b ="); Serial.println (power);
 incrementB();
}
float functName (int numberPassedFromLoop) // This block defines the function named "funcName"
{
  float powerInside; //this variable's scope is just this function. It was declared inside so it cannot be used outside.
  sum = b + numberPassedFromLoop;
  product = b * numberPassedFromLoop;
  powerInside = pow(numberPassedFromLoop,b);
  delay (6000);

  return powerInside;
}
void incrementB ()
{ b++ ;
Serial.println ("--------");
Serial.println (b);
return;
}
//-----------