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

Slides:



Advertisements
Similar presentations
ICDL Software Applications - Database Concepts. Unit 6 Data and Data Representation Database Concepts –File Structure –Relationships Database Design –Data.
Advertisements

1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
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.
CS1315: Introduction to Media Computation Introduction to Programming.
Created by Mark Guzdial, Georgia Institute of Technology; modified by Robert H. Sloan, University of Illinois at Chicago, For Educational Use. CS.
Media Computation in JES (Chapter 2) DM Rasanjalee Himali.
Chapter 2: Introduction to Programming. Chapter Learning Objectives.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Chapter 6: User-Defined Functions I
Chapter 6: User-Defined Functions I
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Microsoft PowerPoint Microsoft PowerPoint Introduction to PowerPoint Common User Interface Series of slides that include:  Clip art  Photographs.
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.
Computer Systems Week 10: File Organisation Alma Whitfield.
CS 102 Computers In Context (Multimedia)‏ 01 / 28 / 2009 Instructor: Michael Eckmann.
Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a.
CS0004: Introduction to Programming Variables – Strings.
Course A201: Introduction to Programming 11/04/2010.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Python programming Introduction to the JES environment and basics of Python Reading: Chapters 1, 2 from “Introduction to Computing and Programming in Python”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
CS1315: Introduction to Media Computation Introduction to Programming.
CS 177 Week 4 Recitation Slides Variables, Files and Functions.
CS 102 Computers In Context (Multimedia)‏ 01 / 26 / 2009 Instructor: Michael Eckmann.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
CS 100 Introduction to Computing Introduction to JES Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Changing colors in an area Part 3: Chromakey.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 3: User-Defined Functions I
CS1315: Introduction to Media Computation Introduction to JES.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
JavaScript Functions. CSS Inheritance Which formatting applies? x y z input { display: block; } input.pref { background:red; } If you have a selector.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
CS1315: Introduction to Media Computation Introduction to Programming.
Python programming Using the JES picture functions and defining new functions.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
CS1315: Introduction to Media Computation Introduction to JES.
CS1315: Introduction to Media Computation Making sense of functions.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
INC 161 , CPE 100 Computer Programming
Multimedia Summer Camp
Introduction to Programming
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Web Design II Flat Rock Community Schools
Chapter 3: Variables, Functions, Math, and Strings
CS1315: Introduction to Media Computation
JavaScript Functions.
Chapter 2: Introduction to Programming
Chapter 2: Introduction to Programming
Week 2 Computer Programming Learning Objective:
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Reviewing key concepts
Introduction to Programming
Elements of a Python Program
Workshop for Programming And Systems Management Teachers
CS 177 Week 3 Recitation Slides
Introduction to Programming
Presentation transcript:

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

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

3 ANY QUESTIONS?

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()

Variables Are names for objects that have values in main memory. We define variables by assigning values to names: Example: >>>a = 9 >>>b = >>>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

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

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

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

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

How to write functions in JES? 10

Example 1: a function with no parameters 11

Example 1: 12

Example 1: 13

Example 1: load your function in main memory 14

Example 2 15

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

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

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

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

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

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

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

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

“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?

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 Final QUESTIONS???