Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 177 Week 2 Recitation Slides Variables, Files and Functions.

Similar presentations


Presentation on theme: "1 CS 177 Week 2 Recitation Slides Variables, Files and Functions."— Presentation transcript:

1 1 CS 177 Week 2 Recitation Slides Variables, Files and Functions

2 2 Announcements Get your I-Clickers to class next time.

3 3 ANY QUESTIONS?

4 4 Table of content Variables Files Functions  What is a function?  Why to use a function?  How to write a function in JES?  Functions with Parameters JES essentials JES (predefined) functions picture & sound  Picture & sound functions pickAFile() makePicture() makeSound() show() play()

5 Variables Are names for objects that have values in main memory. We define variables by assigning values to names: Example: >>>a = 9 >>>b = 13.54 >>>c = “Computer” Variables can change their values during the program. It’s important to choose good names that describes the variable. Example: firstName, courseNo, etc. 5

6 Files Are collection of larger objects and values (byte) on the hard disk of the computer. Files can contain text, picture, sound, video, etc. Files have names File names have suffixes (.jpg,.wav,.mp3, and so forth) 6

7 Functions Collection of instructions that perform a task as:  Printing your name and course.  Calculating the average of set of numbers.  Editing a picture or video. 7

8 Functions Like in algebra, a function is a kind of “box” into which you put one value and out comes another. We represent (“denote”) a function by a name (in math we use f or F). 8 F output Input

9 Why to use a function? If we define a function to perform a task, then we will write it once then we can use it (or call it) many times. 9

10 How to write functions in JES? 10

11 Example 1: a function with no parameters 11

12 Example 1: 12

13 Example 1: 13

14 Example 1: load your function in main memory 14

15 Example 2 15

16 Functions with Parameters When you write a function, you may want your function to apply not to a single, fixed specific value, but to any value you may decide. To do that, you add an input parameter to your function definition. 16 def square (x): print x*x The variable x is called parameter def square25 (): print 5*5

17 Quiz Write a function called CircleArea that has as input any diameter and then calculate and print the area of the circle having such diameter. NOTE: the diameter is twice the radius….. 17

18 JES Essentials Now we are going to use JES to work with Variables, Files, and Functions … 18

19 Show a Picture 19 myFile = pickAFile() myPicture = makePicture(myFile) show(myPicture) Q: Is it possible to write the program in a more concise way?

20 Show Picture 20 myPicture = makePicture( pickAFile()) show(myPicture) Q: Is it possible to write the program in a more concise way?

21 Show Picture 21 show( makePicture( pickAFile()))

22 A recipe for playing sound files def pickAndPlay(): myfile = pickAFile() mysound = makeSound(myfile) play(mysound) Bug alert!!! myfile and mysound, inside pickAndPlay(), are completely different from the same names in the command area. If you want to a name to refer to the same variable inside and outside a function, pass the variable as an argument

23 In JES, use copy, paste and edit to create similar functions def pickAndShow(): myfile = pickAFile() mypict = makePicture(myfile) show(mypict) def pickAndPlay(): myfile = pickAFile() mysound = makeSound(myfile) play(mysound) Useful function for sounds Analogous function for pictures

24 “Hard-coding” for a specific sound or picture def playSound(): myfile = "FILENAME" mysound = makeSound(myfile) play(mysound) def showPicture(): myfile = "FILENAME" mypict = makePicture(myfile) show(mypict) You can always replace data (a string of characters, a number…. whatever) with a name (variable) that holds that data …. or vice versa. Q: This works, but can you see its disadvantage?

25 Functions with inputs are more general- purpose def playNamed(myfile): mysound = makeSound(myfile) play(mysound) def showNamed(myfile): mypict = makePicture(myfile) show(mypict) Q: What functions do you need? Q: What (if any) should be their input(s)? A: In general, have enough functions to do what you want, easily, understandably, and in the fewest commands (i.e. by using more generic, less specific functions) Q: What functions do you need? Q: What (if any) should be their input(s)? A: In general, have enough functions to do what you want, easily, understandably, and in the fewest commands (i.e. by using more generic, less specific functions) Parameterized Python, Programmed like a pro! But these are only questions of style

26 26 Final QUESTIONS???


Download ppt "1 CS 177 Week 2 Recitation Slides Variables, Files and Functions."

Similar presentations


Ads by Google