{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

functions - Functions Institute for Personal Robots in...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 1 with Robots Functions Institute for Personal Robots in Education (IPRE)
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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 values You “call it”,”invoke it”, or “use it” by using its name and parentheses The things you pass it go inside the parentheses output = function( input ) function input output
Image of page 2
Aug 29 2007 3 Using Simple Functions forward(1,1) beep(1, 440) Functions that interact with the robot forward (speed,duration) beep(time, frequency) Pass in arguments Execute in sequential order flow of execution
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Aug 29 2007 4 Writing Simple Functions def nudge (): print “going forward” forward(1,1) print “now stopped” nudge () Indent Defining functions Creates function Does not execute/run them Indenting indicates a “block” of code Call functions from top-level or other functions No Indention “Top Level”
Image of page 4
Aug 29 2007 5 Format of a function definition def function-name(): statement statement statement
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Aug 29 2007 6 Writing Functions with Parameters def nudge ( speed ): print “Going forward with speed”, speed forward( speed ,1 ) print “stopped” stop() nudge ( 0.2 ) nudge ( 0.9 ) nudge ( 1 )
Image of page 6
Aug 29 2007 7 Parameters are Variables
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern