Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Programming Module 3 Functions Python Programming, 2/e1.

Similar presentations


Presentation on theme: "Python Programming Module 3 Functions Python Programming, 2/e1."— Presentation transcript:

1 Python Programming Module 3 Functions Python Programming, 2/e1

2 Functions Problems we need to solve over and over: – Calculate sales tax. – Turn the robot 90 degrees to the left. – Convert Celsius to Fahrenheit It would be a pain to include a complete copy of the code every time we wanted to do one of these jobs. We can “package” the commands up as a function and give it a name. Python Programming, 2/e2

3 Functions A function is like a subprogram, a small program inside of a program. The basic idea – we write a sequence of statements and then give that sequence a name. We can then execute this sequence at any time by referring to the name. Python Programming, 2/e3

4 Functions The part of the program that creates a function is called a function definition. When the function is used in a program, we say the definition is called or invoked. Python Programming, 2/e4

5 Programming Design: Temperature Converter Problem – the temperature is given in Celsius by the user; it should be converted and displayed in degrees Fahrenheit. Specification – Input – temperature in Celsius – Output – temperature in Fahrenheit – Output = 9/5(input) + 32 Python Programming, 2/e5

6 Example Program: Temperature Converter Design – Input, Process, Output (IPO) – Prompt the user for input (Celsius temperature) – Process it to convert it to Fahrenheit by calling a function that implements F = 9/5(C) + 32 – Output the result by displaying it on the screen Python Programming, 2/e6

7 Creating a Function When we create a function we need to give it a name. Then we need to determine what information will be needed for the function to do its job. We can pass this information to the function as a list of parameters. Python Programming, 2/e7

8 Creating a Function A function declaration looks like the following: def celsius2farh(c): This function is named celsius2farh and takes a variable which will be called c inside the function The variable could be named something else when the function is called. Python Programming, 2/e8

9 Creating a Function We then indent the commands that will make up the function. – This is called the body of the function. def celsius2fahr(c): f = 9/5 * c + 32 return f To report the result of the function we use a return statement. Python Programming, 2/e9

10 Calling a Function To call our function we need to supply a value for c. This can be anything that has an appropriate value. – celsius2fahr(5) has the value 41.0. – If x = 100 then celsius2fahr(x) has the value 212.0. You do not need to know what the value is called inside the function in order to use it! Python Programming, 2/e10

11 Example Program: Temperature Converter #Convert Celsius to Farhenheit def celsius2fahr(c): f = 9/5 * c + 32 return f celsius = float(input("What is the Celsius temperature? ")) fahrenheit = celsius2fahr(celsius) print("The temperature is", fahrenheit, "degrees Fahrenheit.") Python Programming, 2/e11

12 Example Program: Temperature Converter When we write a program, we should test it! What is the Celsius temperature? 0 The temperature is 32.0 degrees Fahrenheit. What is the Celsius temperature? 100 The temperature is 212.0 degrees Fahrenheit. What is the Celsius temperature? -40 The temperature is -40.0 degrees Fahrenheit. Python Programming, 2/e12

13 Passing Parameters Not all functions need parameters. def instructions(): print(“The purpose of this program… Even if a function doesn’t have parameters it still needs parentheses. In Python a function may “just do something” and so not need a return value. Python Programming, 2/e13

14 Passing Parameters A function may have several parameters. def coneVol(h, r): return (math.pi * r**2 * h / 3) If there are multiple parameters their values are determined by order. For example if we call this using v = coneVol(5, 9) In the function h=5 and r=9. Python Programming, 2/e14

15 Objects Some functions “belong” to a special variable called an object. These are called methods of the object. A Large Motor would be an example of an object. m = LargeMotor() m.timed_run() We use a. to show the object to which the function belongs. Python Programming, 2/e15

16 Functions that “Do Things” To have our robot drive forward there are several commands we need to issue. We might want to combine these into a single function. We could then call the function whenever we need to drive forward. Python Programming, 2/e16

17 Functions that “Do Things” We will need to tell the function some information. – Which motors are involved (left and right). – How long to drive. – What speed to use. def speed_drive(ml, mr, t, v): This function doesn’t need a return value. Python Programming, 2/e17

18 Functions that “Do Things” def speed_drive(ml, mr, t, v): ml.speed_regulation_enabled = 'on' mr.speed_regulation_enabled = 'on' ml.speed_sp = v mr.speed_sp = v ml.time_sp = t mr.time_sp = t ml.stop_command = 'brake' mr.stop_command = 'brake' ml.run_timed() mr.run_timed() Python Programming, 2/e18

19 Activities Write functions to do the following: Turn Right 90 degrees Turn Left 90 degrees Repeatedly call these along with the speed_drive function to navigate a maze. Python Programming, 2/e19

20 Sensors Using sensors is similar to using motors. You first need to name the sensor. t = TouchSensor(‘in1’) We can then check the value the sensor is currently seeing. t.value() If the button on the touch sensor is pressed this will return 1 otherwise it will return 0. Python Programming, 2/e20

21 Temperature Sensor The Temperature Sensor did not come with the EV3 set and the software that comes with ev3dev does not support this sensor. We have written a binding for this sensor for us to use, but right now it reports a number whose meaning is not clear. We also want to be able to convert to different measures of temperature. Python Programming, 2/e21

22 Temperature Sensor Here is how it should work. from ev3 import * temp = TemperatureSensor(‘in2’) temp.mode = ‘TEMP-F’ temp.value() This should report the temperature in degrees Fahrenheit. The conversion function is broken! We need to fix it. Python Programming, 2/e22

23 Temperature Sensor Modes The temperature sensor is supposed to have the following modes. – ‘TEMP-F’ – degrees Fahrenheit – ‘TEMP-C’ – degrees Celsius – ‘TEMP-K’ – degrees Kelvin Note, with the other sensors, if there is only one, we can skip naming the input port. With the Temperature Sensor it is always needed. Python Programming, 2/e23

24 Conversion Functions In our next robot challenge we will write the code to implement these conversions functions. We will base these on experimental data that we will gather. We can convert to one temperature measure and then use known conversion formulas to convert to the others. This uses composition of functions. Python Programming, 2/e24


Download ppt "Python Programming Module 3 Functions Python Programming, 2/e1."

Similar presentations


Ads by Google