Upload
jesse-gray
View
222
Download
0
Embed Size (px)
Citation preview
Variables and Functions
Open your Encoder program
Let’s begin by opening the “Labyrinth Auto Straight” code.
Save this file as Labyrinth with variables.
Making changes takes too much timeSuppose your auto straight program doesn’t
exactly work with the motor powers we selected.
Change these as needed.
But you need to change these everywhere. Is that practical?
Behaviors and Variables
What if we could change the power levels in ONE place and that would change the power levels in ALL the places at one time?
Variables
You can take a motor power value and STORE it inside of a variable for later use.
Variables
Variable Types
These can be whole numbers only and are referenced by “int”.
Variable Types
Unlike integers, these numbers are allowed to have decimal values and are referenced by “float”.
Variable Types
Referenced by “string” , these can be a combination of letters, numbers, and spaces.
Variable Types
Single items are called “characters” and are referenced by “char”.
Variable Types
Variables can even be simply true or false which use Boolean logic and referenced by “bool”.
Let’s use the “int” variable
When you name and “integer” type variable you cannot use:•Spaces•Characters(!)•Numbers(1)•Already reserved/recognized terms(int)
The “int” variable
Here we place the integer just after the task main command. The name refers to how we will remember its function.After we name it, we then STORE the integer we want.
Replace all motor speeds with integer “names”
Now change all motor powers to the appropriate variable name. Do this for the entire program. Then save, compile, download, and test.
TestThe idea behind this is that you can now
“change” the motor powers in ONE place instead of 12 places as you test. This is just one way to program efficiently.
Behaviors and FUNCTIONS
FunctionsFunctions are pieces of code that come BEFORE task main. We use “void” to begin our functions. The function itself is then given a name, in this case moveStraight. There is a set of empty parenthesis after the name. Then the commands are placed inside the functions curly braces.
The “call”Then, when you are ready to use the code, you simply “call” it from within task main.
So in summary, you first have to DECLARE the function and give it a name. Then you must CALL the function to use it.
Behaviors and FUNCTIONSOpen the file Labyrinth Auto Straight. Save this file as Labyrinth with functions.
Declare the function and name it BEFORE task main.
Behaviors and FUNCTIONS
Highlight the code starting with the FIRST IF statement and END with the last IF statement Right click and select copy.
Make sure you DO NOT copy the while loop’s curly braces.
Behaviors and FUNCTIONS
Then paste the code inside the functions curly braces.
Behaviors and FUNCTIONS
Then go back and delete the code you highlighted under task main. Replace the code with the CALL. Make sure the call is spelled and capitalized exactly as the function name. Also make sure there is a semicolon after the call as this is now a command or simple statement with the main program.
Behaviors and FUNCTIONSDelete all instances of this “highlighted” code in your program and replace each instance with the call.
Let’s test your understanding1. Create new FUNCTIONS for your right and left
turns.
2. Compile and test the program to see if it makes it into the labyrinth.
3. If successful, use COMMENTS to EXPLAIN each line of code in YOUR own words.
4. Be sure to place your NAME in the comments for grading.