Week 4 - Section 2 - Shape up

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.


import Ed

Ed.EdisonVersion = Ed.V2

Ed.DistanceUnits = Ed.CM

#--------Your code below-----------

#calling code

#user defined function
def moveOnClap(direction,speed,distance):
    while Ed.ReadClapSensor() == Ed.CLAP_NOT_DETECTED:

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(?):



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.