Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.

Similar presentations


Presentation on theme: "Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables."— Presentation transcript:

1 Variables and Functions

2 Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.

3 Making changes takes too much time Suppose 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?

4 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?

5 Variables You can take a motor power value and STORE it inside of a variable for later use.

6 Variables

7 Variable Types These can be whole numbers only and are referenced by “int”.

8 Variable Types Unlike integers, these numbers are allowed to have decimal values and are referenced by “float”.

9 Variable Types Referenced by “string”, these can be a combination of letters, numbers, and spaces.

10 Variable Types Single items are called “characters” and are referenced by “char”.

11 Variable Types Variables can even be simply true or false which use Boolean logic and referenced by “bool”.

12 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)

13 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.

14 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.

15 Test The 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.

16 Behaviors and FUNCTIONS

17 Functions Functions 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.

18 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.

19 Behaviors and FUNCTIONS Open the file Labyrinth Auto Straight. Save this file as Labyrinth with functions. Declare the function and name it BEFORE task main.

20 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.

21 Behaviors and FUNCTIONS Then paste the code inside the functions curly braces.

22 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.

23 Behaviors and FUNCTIONS Delete all instances of this “highlighted” code in your program and replace each instance with the call.

24 Let’s test your understanding 1. 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.


Download ppt "Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables."

Similar presentations


Ads by Google