cs 1 with robots functions institute for personal robots in education (ipre)

25
CS 1 with Robots Functions Institute for Personal Robots in Education

Post on 20-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

CS 1 with Robots

Functions

Institute for Personal Robots in Education

(IPRE)

Aug 29 2007 2

Functions

A function is a piece of code you can use over and over again

Treat it like a black box

You pass it (optional) values, it does some work, and it (optionally) returns valuesYou “call it”,”invoke it”, or “use it” by

using its name and parenthesesThe things you pass it go inside the parenthesesoutput = function( input )

function

input

output

Aug 29 2007 3

Using Simple Functions

forward(1,1)beep(1, 440)

Functions that interact with the robotforward (speed,duration)beep(time, frequency)

Pass in argumentsExecute in sequential order

flow of execution

Aug 29 2007 4

Writing Simple Functions

def nudge():print “going forward”forward(1,1)print “now stopped”

nudge()

Indent

Defining functionsCreates functionDoes not execute/run them

Indenting indicates a “block” of codeCall functions from top-level or other functions

No Indention“Top Level”

Aug 29 2007 5

Format of a function definition

def function-name():statementstatement…statement

Aug 29 2007 6

Writing Functions with Parameters

def nudge(speed):print “Going forward with speed”, speedforward(speed,1)print “stopped”stop()

nudge(0.2)nudge(0.9)nudge(1)

Aug 29 2007 7

Parameters are Variables

When you pass values into functions as parameters, they get assigned to the variable names declared in the definition line of the function.For example, when you call nudge(0.2)The speed variable is assigned (points to) the value 0.2When the code in the function refers to the speed variable, it evaluates to the number 0.2So, when you call nudge(0.2) and the nudge function calls forward(speed, 1), it's the same as if it called forward(0.2,1)

Aug 29 2007 8

Octaves of A

def beepA(length, octave):beep(length, 440 * (2**octave))

beepA(1,0) # A4beepA(2,1) # A5beepA(3,2) # A6

A4 : 440 Hz A5: 880 Hz A6: 1760 Hz A7: 3520 Hz

Aug 29 2007 9

Format of a Function Definition with Parameters

def function-name(list-of-params):statementstatement

…statement

function-name(list-of-params)

Aug 29 2007 10

Using Functions that Return Values

name = getName()print “Hello, your robot is”, name

print “Robot battery voltage”, getBattery()p = takePicture()show(p)

Aug 29 2007 11

Composing Functions

You can use the output (return value) of one function as the input (parameter) to another function.

show( takePicture() )

In this example, the takePicture() function executes first (things inside parenthesis execute before things outside parenthesis)The takePicture() function returns a picture, which is

then given to the show() function as a parameter.

Aug 29 2007 12

Writing Functions that Return Values

def area(radius):return 3.14 * radius**2

def circumference(diameter):return 3.14 * diameter

print “Area of a 3 ft circle”, area(3)print “Circumference”, circumference(2*3)

Aug 29 2007 13

Return Statements

The return statement is used to return a value from a functionThe return statement also affects the flow of executionWhenever the flow of execution hits a return statement it jumps back to the place where the function was calledAll functions have an implicit return statement at the end of the block of indented code, even if you do not specifically place one at the end of your function

Aug 29 2007 14

Functions with Local Variables

def area(radius):a = 3.14 * radius**2return a

def circumference(diameter):c = 3.14 * diameterreturn c

print “Area of a 3 ft circle”, area(3)print “Circumference”, circumference(2*3)

Aug 29 2007 15

Variables in a Function are Local

Variables in a function are privateIncluding the parameters

Each function has its own variablesEven when the names are the same

Allows you to write functions independently without worrying about using the same name

Aug 29 2007 16

Different Variables - Same Name

def area(radius):a = 3.14 * radius**2return a

def circumference(radius):a = 3.14 * 2 * radiusreturn a

a = 20print “Area of a 3 ft circle”, area(3)print “Circumference”, circumference(3)print a

Aug 29 2007 17

Writing Functions with Return Values

def function-name(list-of-params):statementstatement

…statementreturn value

output = function-name(list-of-params)

Aug 29 2007 18

Passing variables to a functions

If you pass a variable to a function, the function gets the value that the variable is pointing at

userInput = raw_input(“Enter a Name”)setName(userInput)print “The Robots new Name is: “, userInput

Aug 29 2007 19

Functions in general

# description of this function# what it expects as input# what is provides as outputdef function (p0, p2, …, pn):

statement …statementreturn value

z = function(a0, a2, …, an)

Aug 29 2007 20

Where’s the Error?

def area:a = 3.14 * radius**2return a

print “Area of a 3 ft circle”, area(3)

Aug 29 2007 21

Where’s the Error?

def area( radius ):a = 3.14 * radius**2return a

print “Area of a 3 ft circle”, area(3)

Aug 29 2007 22

Where’s the Error?

def area(radius):a = 3.14 * radius**2return a

print “Area of a 3 ft circle”, area()

Aug 29 2007 23

Where’s the Error?

def area(radius):a = 3.14 * radius**2

print “Area of a 3 ft circle”, area(3)

Aug 29 2007 24

Where’s the Error?

def area(radius):a = 3.14 * radius**2return a

area(3)print “Area of a 3 ft circle”, a

Aug 29 2007 25

What’s the result?

def area(radius):a = 3.14 * radius**2return a

v = area(3)a = 16print “Area of a 3 ft circle”, vprint “value of a”, a