Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 171: Functions, for loops and range John Barr Section 03 Slides by Toby Dragon.

Similar presentations


Presentation on theme: "COMP 171: Functions, for loops and range John Barr Section 03 Slides by Toby Dragon."— Presentation transcript:

1 COMP 171: Functions, for loops and range John Barr Section 03 Slides by Toby Dragon

2 What is a function? a block of code that we can call with a single name.  "Black Box" defined in order to provide functionality without dealing with how it works Some functions are provided to you:  input (), print () Some functions you have written:  drawRect(length, width)

3 Why functions? break code into smaller, functional pieces  testing parts before trying to solve everything  Make pieces that can be reused for multiple purposes simplify (abstract) to make your program easier to understand and to use

4 Functions: defining the "Black Box" def drawInitials (): Tell python we're defining a function "Name" that we decide place for parameters (empty if none) tells python that the function is starting, everything indented after this will be part of the function

5 Parameters def drawInitials (): def drawSquare (size): def drawRect (length, width): No parameters One parameter Two parameters

6 Parameters Parameters answer questions the function has.  e.g., drawRect needs to know the length and width of the rectangle you want drawn Use parameters only when necessary!

7 Practice Write a function that takes the temperature in Fahrenheit and prints the temperature in Celsius.  To calculate Celsius from Fahrenheit: subtract 32 multiply by 5/9

8 Practice Write a function that takes the temperature in Fahrenheit and prints the temperature in Celsius.  To calculate Celsius from Fahrenheit: subtract 32 multiply by 5/9

9 Functions: using the "Black Box" when we use a function, we say we are calling the function. Always end in parentheses!  Otherwise, python treats it as a variable Between parentheses you give values to the parameters

10 Calling functions and sending Parameters drawInitials () drawSquare (100) sides=100 ends = 200 drawInitials (sides, ends)

11 the for loop for num in range(0, 10, 1): indented code below it will happen multiple times, each with num = a number from the range. variable you create range function

12 Repetition: the range function range (10) – give each number, starting at 0 to less than 10 range (3, 10) – give each number, starting at 3 to less than 10 range (2, 20, 2) – give each number starting at 2, to less than 20, and count by 2 name of the range function Parameter(s)

13 Repetition: range practice [0, 1, 2, 3,..., 19] [2, 4, 6,..., 46, 48] [50, 48,..., 0]

14 Repetition: for loop practice Write a for loop that prints: 3 6 9 … 18

15 for loop practice Write a program to print the Celsius equivalent of every 10 degrees Fahrenheit between 0 and 100. Use your function from earlier!


Download ppt "COMP 171: Functions, for loops and range John Barr Section 03 Slides by Toby Dragon."

Similar presentations


Ads by Google