Katz.FunctionTutorial History

Hide minor edits - Show changes to output - Cancel

Added line 6:
This will be easier to read if you copy and paste into an Arduino Sketch.
Changed lines 19-22 from:
//
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
to:
//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
Changed line 24 from:
int b=3; //global variable declaration, may be accessed inside and outside functions
to:
int b=3; //global variable declaration, may be accessed inside and outside functions
Changed lines 28-29 from:
a value of 9600 baud*/
Serial.println("Function Use Demo");//This to calls a function in the serial library to print the value "Function Use Demo"
to:
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"*/
Changed line 7 from:
There are two function calls (other than "setup", and "loop" and the library calls "Serial..." and "pow").
to:
There are two function calls in this program (other than "setup", and "loop" and the library calls "Serial..." and "pow").
Changed line 2 from:
// Function Tutorial
to:
//Function Tutorial
Changed line 4 from:
//this code is public domain, but I would appreciate credit and an email. Any comments can be sent to myfurstname.last@tamucc.edu
to:
//this code is public domain, but I would appreciate credit and an email.
Changed lines 6-7 from:
The purpose of this code is to illustrate how data can be passed to and from functions.
There are two
function calls other than "setup", and "loop" and the library calls "Serial..." and "pow".
to:
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 (other than "setup", and "loop" and the library calls "Serial..." and "pow").
These two functions are funcName and incrementB
.
Deleted lines 17-19:

Added line 19:
//
Changed lines 24-28 from:
int b=3;
void setup() {
Serial.begin (9600);
Serial.println("Function Use Demo"
);
to:
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 to calls a function in the serial library to print the value "Function Use Demo"
Changed line 39 from:
float functName (int numberPassedFromLoop)
to:
float functName (int numberPassedFromLoop) // This block defines the function named "funcName"
Added line 1:
[@//-----------
Changed line 55 from:
to:
//-----------@]
Added lines 1-56:
// Function Tutorial
//copyright Louis H. Katz 2012
//this code is public domain, but I would appreciate credit and an email. Any comments can be sent to myfurstname.last@tamucc.edu
/*
The purpose of this code is to illustrate how data can be passed to and from functions.
There are two function calls other than "setup", and "loop" and the library calls "Serial..." and "pow".
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.



*/
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;
void setup() {
Serial.begin (9600);
Serial.println("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)
{
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;
}