Get Help
Functions
Commands That Make Stuff Happen
Functions are a way to group commands together into chunks, so that code can be re-used. Here’s what we’ll be covering on this page: You’ve probably already been using functions and you might not have known it. The sparki library is made up of a whole bunch of different functions. When Sparki sees a call to a function in its code it jumps to wherever that function is stored in the code, executes the function, and then jumps back to where it was inside the loop( ) code. This way you can use the function multiple times but you (or someone else) only had to write it once. Functions are kind of like robots- they sometimes take input (like a robot gets input from its sensors), sometimes create output (like a robot will beep or turn motors) and you can easily use them over and over again once the original function code has been written.Calling a Function
Using a function is referred to as “calling” a function. You call a function by typing its name, open and closed parentheses, then a semicolon like so:
1 |
functionName(); |
1 |
functionName(parameter); |
1 |
functionName(parameter1, parameter2, parameter3); |
Returning Data
Somer functions can also give back, or return data. You can think of this as if our robotic function leaves a note with some information on it once the robot is done performing its tasks. One thing you have to be sure of is that the type of variable you are storing the return information in (in this case returnValue) matches the information that the function is spitting out. Here is an example of a function that returns an integer-
1 2 |
int returnValue; returnValue = functionName(parameter); |
Writing a Function
Writing a function is pretty easy. Once you write out a function it is called a function definition and you can call it from anywhere in the code that contains the function. You’ll just need to get comfortable with the beginning of a function, which always looks more or less the same. This part of the function is called the function “header.” It has a bunch of different parts of you can see below- You can create your own function using the following format:
1 2 3 4 |
type name (variableType1 parameter1, variableType2 parameter2, ...) { //statements } |
Vocabulary
Let’s talk about the various parts of the function above- –function header:
these are the first couple lines of a function. It will always contain a return variable type (or void) a name, parentheses and an open curly bracket. It may contain variable types and names inside the parentheses if the function expects to receive any information when it is called.
– type:
the variable type of the data returned by the function. If you are not returning any data, use void as the type. So if you want your function to spit out a character after it is done running your would need to write “char” here at the beginning of the function.
– name
:
the name by which the function can be called. In the examples above, the name of the function is functionName. You could name your function whatever you like as long as it is one word (or multiple words with no spaces). That can get confusing though, so programmers have agreed that all functions should start with a lower case letter. Also, your function name should be related to whatever your function does. It’s a little confusing if you call your function puppiesAndKittens when the function code makes your robot beep. People reading your code will have a tough time figuring out what is going on.
– parentheses:
the parameters that the function expects to be passed are contained inside the open and closed parentheses.
– variable type:
the variable type of the parameter that comes after this part of the code. So if parameter1 is an integer, where the example reads variableType1 you would type “int.” If parameter1 is a character you would type “char.”
– parameters:
(as many as needed): these are the pieces of information that get passed to the function. Each new parameter has a comma before it. You declare each variable just like you would in any other code as we discussed above.
Because you are declaring new variables to hold the information entering the function, while this is the same information as the variables that are passed to the function, they are stored separately. That means that any changes you make to the parameter variables inside the function will not effect the original variables outside the function.
Each variable declared at the beginning of the function is deleted after the function is finished. If you want to use the data in the variable, you can return it or you can include code in the statements portion of your function that reassigns the original variable value.
– curly brackets:
are used to indicate the beginning and end of the code statements that actually make the function perform actions. There is an open curly bracket { at the beginning of the statement code and a closing curly bracket at the end of the function so the computer or robot knows the function is done.
– statements:
is where the statement code for the function lives, it is also called the function “body.” It is a block of statements surrounded by curly brackets { } that specify what the function actually does. This is where you’ll write all the code that changes variables, move wheels, reads sensors, beeps or whatever you want your function to do.
A Robot as a Function
Functions are really important parts of code and can be confusing if it’s your first time trying to understand them so here is an example using our robot friend as an analogy. Here is code of an example function definition that takes in two different integers, and returns an integer:
1 2 3 4 5 6 |
int addition( int a , int b ) { int r; r = a + b; return r; } |
1 2 |
int sum; sum = addition(3, 5); |
Try it with Sparki
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <Sparki.h> // include the sparki library void setup() // code inside these brackets runs first, and only once { } void loop() // code inside these brackets runs over and over forever { sparki.clearLCD(); int sum; sum = addition(3, 5); sparki.println(sum); sparki.updateLCD(); } int addition( int a, int b ) { int r; r = a + b; return r; } |