Вы находитесь на странице: 1из 24

Variables and Functions

Open your Encoder program


Lets begin by opening the Labyrinth Auto
Straight code.
Save this file as Labyrinth with variables.

Making changes takes too much time


Suppose your auto straight program doesnt
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.

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

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.

Behaviors and FUNCTIONS

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.

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 FUNCTIONS


Open 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 loops 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 FUNCTIONS


Delete all instances of this highlighted code in your program and replace
each instance with the call.

Lets test your understanding


1.

2.

3.

4.

Create new FUNCTIONS for your right and left


turns.
Compile and test the program to see if it makes
it into the labyrinth.
If successful, use COMMENTS to EXPLAIN
each line of code in YOUR own words.
Be sure to place your NAME in the comments
for grading.

Вам также может понравиться