2. Chapter 12.2 - Functions

Once you have been programming for some time you will want to make your own commands or functions. Watch the code.org video for a good explanation of why functions are useful.


In Python, functions look like the following:

def function_name(parameter_1, parameter_2):

    {this is the code in the function}

    {more code}

    return {value to return to the program}

{this code isn’t in the function}

{because it isn’t indented}

You call a function from your code with or without parameters and the function usually returns a value to the calling code. You do not always have to return a value.

Write the following program.


#-------------Setup---------------- import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_MEDIUM #--------Your code below----------- directionToMove=Ed.SPIN_LEFT speedToMoveAt=Ed.SPEED_7 distanceToMove=10 #calling code moveOnClap(directionToMove,speedToMoveAt,distanceToMove) #user defined function def moveOnClap(direction,speed,distance): Ed.ReadClapSensor() while Ed.ReadClapSensor() == Ed.CLAP_NOT_DETECTED: pass Ed.Drive(direction,speed,distance)


You can download the code from here

Fill in the missing words in the function below to make this function drive the edison robot in a square shape.

def driveInaSquare(direction,speed,distance):

    for i in range(?):

        Ed.Drive(direction,?,?)

        Ed.Drive(?,speed,?)

Write the syntax for code to call this function.

An example can be downloaded from here

Now incorporate this code into a program which calls your function more than once to drive multiple squares and download and run it.

Does the program do as you expect?  Describe any errors you had in getting your program to work.