Function Call with Parameters
Remember that a function can have one or more parameters—data that the function receives and uses when it is called. The diagram below shows the pitch function from the next sketch. It is declared with void pitch(int Hz). Recall from Chapter 1, Activity #3 that the Arduino stores variable values in different data types, with int specifying an integer value in the range of -32,768 to 32,767. Here, the term int Hz in the parentheses defines a parameter for the pitch function; in this case, it declares a local variable Hz of data type int.
Local variables, remember, are declared within a function, and can only be seen and used inside that function. If a local variable is created as a parameter in the function declaration, as void pitch(int Hz) is here, initialize it by passing a value to it each time the function is called. For example, the call pitch(3500) passes the integer value 3500 to the pitch function’s int Hz parameter.
So, when the first function call to pitch is made with pitch(3500), the integer value 3500 gets passed to Hz. This initializes Hz to the value 3500, to be used during this trip through the pitch function’s code block. The second call, pitch(2000), initializes Hz to 2000 during the sketch’s second trip through the pitch function’s code block.
Example Sketch – FunctionCallWithParameter
- Read the sketch, and predict what you will see and hear when you run it.
- Enter and upload FunctionCallWithParameter, then open the Serial Monitor.
- Watch your terminal and listen to the tones.
- Was your prediction correct? If so, great! If not, take a closer look at the sketch and make sure you can follow the code from each function call to the function and back. For clarification, take another look at the diagram above.
// Robotics with the BOE Shield – FunctionCallWithParameter // This program demonstrates a function call with a parameter. void setup() { Serial.begin(9600); Serial.println("Playing higher pitch tone..."); pitch(3500); // pitch function call passes 3500 to Hz parameter delay(1000); Serial.println("Playing lower pitch tone..."); pitch(2000); // pitch function call passes 2000 to Hz parameter delay(1000); } void loop() { } void pitch(int Hz) // pitch function with Hz declared as a parameter { Serial.print("Frequency = "); Serial.println(Hz); tone(4, Hz, 1000); delay(1000); }
Your Turn – Expand Function to Two Parameters
Here is a modified pitch function that accepts two parameters: Hz and ms. This new pitch function controls how long the tone lasts.
void pitch(int Hz, int ms) { Serial.print("Frequency = "); Serial.println(Hz); tone(4, Hz, ms); delay(ms); }
Here are two calls to the modified pitch function, one for a 0.5 second 3500 Hz tone, and the other for a 1.5 second 2000 Hz tone:
pitch(3500, 500); pitch(2000, 1500);
Notice that each of these calls to pitch includes two values, one to pass to the Hz parameter, and one to pass to the ms parameter. The number of values in a function call must match the number of parameters in that function’s definition, or the sketch won’t compile.
- Save FunctionCallWithParameter as FunctionCallWithTwoParameters.
- Replace the pitch function with the two-parameter version.
- Replace the single parameter pitch calls with the two-parameter calls.
- Run the modified sketch and verify that it works.