Functions Institute for Personal Robots in Education (IPRE)‏

Slides:



Advertisements
Similar presentations
Do I need a robot today? You only need a robot if your team has not shown me your octagon and/or the octagon in a function.
Advertisements

Functions Jay Summet. Aug 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)
CS 1 with Robots Robot Sensors & Actuators Institute for Personal Robots in Education (IPRE)‏
CS 1 with Robots Functions Institute for Personal Robots in Education (IPRE)‏
CS 1 with Robots IDLE and Myro Institute for Personal Robots in Education (IPRE)‏
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
CompSci Today’s topics Robots Myro Loops Notes from the Institute for Personal Robots in Education Upcoming ä More Python Reading Learning Computing.
CH07: Writing the Programs Does not teach you how to program, but point out some software engineering practices that you should should keep in mind as.
Functions and subroutines – Computer and Programming.
Course A201: Introduction to Programming 11/04/2010.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are.
Simple Functions and Names Sec 9-4 Web Design. Objectives The student will: Know how to create a simple function in Python. Know how to call a function.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Sep CS O'Hara1 CS 1301 Review Keith O’Hara
Python Functions.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
1 Functions, Part 1 of 2 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function Header Comments.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Programming for GCSE Topic 8.1: Functions T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen Mary University.
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
CS0004: Introduction to Programming
Do I need a robot today? You only need a robot if your team has not shown me your octagon and/or the octagon in a function.
Topics Introduction to Functions Defining and Calling a Void Function
Course A201: Introduction to Programming
Review What is an object? What is a class?
Web Design II Flat Rock Community Schools
Functions Jay Summet.
© 2010 David A Watt, University of Glasgow
Variables A piece of memory set aside to store data
Function There are two types of Function User Defined Function
Functions CIS 40 – Introduction to Programming in Python
Functions.
Using local variable without initialization is an error.
Review.
Python Primer 2: Functions and Control Flow
Chapter 4 void Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
Topics Introduction to Functions Defining and Calling a Void Function
Functions Institute for Personal Robots in Education (IPRE)‏
Variables, Data Types & Math
Functions Jay Summet.
Introduction to Value-Returning Functions: Generating Random Numbers
Topics Introduction to Functions Defining and Calling a Function
Functions, Part 1 of 3 Topics Using Predefined Functions
15-110: Principles of Computing
Functions Institute for Personal Robots in Education (IPRE)‏
COMPUTER PROGRAMMING SKILLS
Functions Jay Summet.
Predefined Functions Revisited
Introduction to Computer Science
Functions, Part 1 of 3 Topics Using Predefined Functions
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
Methods/Functions.
Top-Down Design with Functions
def-ining a function A function as an execution control structure
Corresponds with Chapter 5
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Top-Down Design with Functions
Functions Jay Summet.
Parameters and Arguments
Scope Rules.
Week 7 - Friday CS 113.
Functions, Part 1 of 2 Topics Using Predefined Functions
Presentation transcript:

Functions Institute for Personal Robots in Education (IPRE)‏

output A function is a piece of code you can use over and over again Functions input 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 output Aug 29 2007

Using Simple Functions Functions that interact with the robot forward (speed,duration)‏ beep(time, frequency)‏ Pass in arguments Execute in sequential order flow of execution forward(1,1)‏ beep(1, 440)‏ Aug 29 2007

Writing Simple Functions Defining functions Creates function Does not execute/run them Indenting indicates a “block” of code Call functions from top-level or other functions def nudge(): print “going forward” forward(1,1)‏ print “now stopped” nudge()‏ Indent No Indention “Top Level” Aug 29 2007

Format of a function definition def function-name(): statement … Aug 29 2007

Writing Functions with Parameters def nudge(speed): print “Going forward with speed”, speed forward(speed,1)‏ print “stopped” nudge(0.2)‏ nudge(0.9)‏ nudge(1)‏ Aug 29 2007

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.2 When the code in the function refers to the speed variable, it evaluates to the number 0.2 So, 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

A4 : 440 Hz A5: 880 Hz A6: 1760 Hz A7: 3520 Hz Octaves of A def beepA(length, octave): beep(length, 440 * (2**octave))‏ beepA(1,0) # A4 beepA(2,1) # A5 beepA(3,2) # A6 Aug 29 2007

Format of a Function Definition with Parameters def function-name(list-of-params): statement … function-name(list-of-params)‏ Aug 29 2007

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

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

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

The return statement is used to return a value from a function Return Statements The return statement is used to return a value from a function The return statement also affects the flow of execution Whenever the flow of execution hits a return statement it jumps back to the place where the function was called All 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

Functions with Local Variables def area(radius): a = 3.14 * radius**2 return a def circumference(diameter): c = 3.14 * diameter return c print “Area of a 3 ft circle”, area(3)‏ print “Circumference”, circumference(2*3)‏ Aug 29 2007

Variables in a Function are Local Variables in a function are private Including the parameters Each function has its own variables Even when the names are the same Allows you to write functions independently without worrying about using the same name Aug 29 2007

Different Variables - Same Name def area(radius): a = 3.14 * radius**2 return a def circumference(radius): a = 3.14 * 2 * radius a = 20 print “Area of a 3 ft circle”, area(3)‏ print “Circumference”, circumference(3)‏ print a Aug 29 2007

Writing Functions with Return Values def function-name(list-of-params): statement … return value output = function-name(list-of-params)‏ Aug 29 2007

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

Functions in general # description of this function # what it expects as input # what is provides as output def function (p0, p2, …, pn): statement … return value z = function(a0, a2, …, an)‏ Aug 29 2007

Where’s the Error? def area: a = 3.14 * radius**2 return a print “Area of a 3 ft circle”, area(3)‏ Aug 29 2007

print “Area of a 3 ft circle”, area(3)‏ Where’s the Error? def area( radius ): a = 3.14 * radius**2 return a print “Area of a 3 ft circle”, area(3)‏ Aug 29 2007

print “Area of a 3 ft circle”, area()‏ Where’s the Error? def area(radius): a = 3.14 * radius**2 return a print “Area of a 3 ft circle”, area()‏ Aug 29 2007

print “Area of a 3 ft circle”, area(3)‏ Where’s the Error? def area(radius): a = 3.14 * radius**2 print “Area of a 3 ft circle”, area(3)‏ Aug 29 2007

print “Area of a 3 ft circle”, a Where’s the Error? def area(radius): a = 3.14 * radius**2 return a area(3)‏ print “Area of a 3 ft circle”, a Aug 29 2007

print “Area of a 3 ft circle”, v print “value of a”, a What’s the result? def area(radius): a = 3.14 * radius**2 return a v = area(3)‏ a = 16 print “Area of a 3 ft circle”, v print “value of a”, a Aug 29 2007