CS1315: Introduction to Media Computation Making sense of functions.

Slides:



Advertisements
Similar presentations
CS1315: Introduction to Media Computation Making sense of functions.
Advertisements

Function in Notepad def drawSquare(myTurtle): myTurtle.forward(100) myTurtle.right(90) # side 1 myTurtle.forward(100) myTurtle.right(90) # side 2 myTurtle.forward(100)
By Now, 1. Your Student ID should open the classroom door 2. You should have C:\100 folder Desktop shortcuts to Command Prompt and Notepad 3. You should.
Multiplication Staff Tutorial. In this tutorial we run through some of the basic ideas and tricks in multiplying whole numbers. We do a bit of stuttering.
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.
Movie of picture negatives # Pseudo Code def movieNeg(directory, pic): for each column of ‘pic’ across the entire width for each pixel from top of the.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
Binary “There are 10 types of people in the world… those that understand binary and those that don’t.”
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 3: Modifying Pictures using Loops.
+ Introduction to Programming My first red-eye removal.
Programmer Defined Functions Matthew Verleger. Windows It’s estimated that Window’s XP contains 45 million lines of code (and it’s over 10 years old).
Picture Color Manipulation. Using a Loop Our first picture recipe def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5)
CS 101: Introduction to Computing Programming picture manipulations Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Program Design CMSC 201. Motivation We’ve talked a lot about certain ‘good habits’ we’d like you guys to get in while writing code. There are two main.
Replacing colors using if We don’t have to do one-to-one changes or replacements of color We can use if to decide if we want to make a change.  We could.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Python programming Introduction to the JES environment and basics of Python Reading: Chapters 1, 2 from “Introduction to Computing and Programming in Python”
Manipulating Pixels by Range and More on Functions.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
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.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Function Abstraction Black Box Container for a sequence of actions Call, execute, invoke the function by name Good for abstraction of key operations Good.
CS 100 Introduction to Computing Introduction to JES Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 3: Modifying Pictures using Loops.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
Program Design and Debugging. How do programmers start? How do you get started with a program? “Programming is all about debugging a blank piece of paper.”
CS 101: Introduction to Computing Color replacements and targeted color replacement (if statement) Developed by Mark Guzdial, Georgia Institute of Technology,
ECS 15 Variables. Outline  Using IDLE  Building blocks of programs: Text Numbers Variables!  Writing a program  Running the program.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 3: Modifying Pictures using Loops.
1 CS161 Introduction to Computer Science Topic #9.
CS1315: Introduction to Media Computation Color replacements and targeted color replacement (IF)
CRE Programming Club Class 3 (Install Small Basic on the new computers! Double-click on I:/smallbasic.msi)
CRE Programming Club - Class 2 Robert Eckstein and Robert Heard.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
CS1315: Introduction to Media Computation Using Loops for Pictures.
Chapter 3: Modifying Pictures using Loops. Chapter Learning Objectives.
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.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
CS1315: Introduction to Media Computation Introduction to JES.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
CS 101: Introduction to Computing Programs that change Pictures Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
CS1315: Introduction to Media Computation Making sense of functions.
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.
2.1 First C Program. First Program Open visual studio, click new file Save as “programName.c” – Program must start with letter and have no spaces – Must.
Multimedia Summer Camp
Chapter 4: Modifying Pictures using Loops
CS1315: Introduction to Media Computation
Chapter 4: Modifying Pictures using Loops
Functions, Procedures, and Abstraction
Chapter 3: Modifying Pictures using Loops
CS1315: Introduction to Media Computation
Chapter 3: Modifying Pictures using Loops
Agenda – 1/31/18 Questions? Group practice problems
A Media Computation Cookbook
CS 177 Week 3 Recitation Slides
Chapter 3: Modifying Pictures using Loops
CS1315: Introduction to Media Computation
CS1315: Introduction to Media Computation
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Functions, Procedures, and Abstraction
Presentation transcript:

CS1315: Introduction to Media Computation Making sense of functions

Questions on functions How can we reuse variable names like picture in both a function and in the Command Area? Why do we write the functions like this? Would other ways be just as good? Is there such a thing as a better or worse function? Why don’t we just build in calls to pickAFile and makePicture?

One and only one thing We write functions as we do to make them general and reusable  Programmers hate to have to re-write something they’ve written before  They write functions in a general way so that they can be used in many circumstances. What makes a function general and thus reusable?  A reusable function does One and Only One Thing

Compare these two programs def makeSunset(picture): for p in getPixels(picture): value=getBlue(p) setBlue(p,value*0.7) value=getGreen(p) setGreen(p,value*0.7) def makeSunset(picture): reduceBlue(picture) reduceGreen(picture) def reduceBlue(picture): for p in getPixels(picture): value=getBlue(p) setBlue(p,value*0.7) def reduceGreen(picture): for p in getPixels(picture): value=getGreen(p) setGreen(p,value*0.7) Yes, they do exactly the same thing! makeSunset(somepict) has the same effect in both cases

Observations on the new makeSunset It’s okay to have more than one function in the same Program Area (and file) makeSunset in this one is somewhat easier to read.  It’s clear what it does “reduceBlue” and “reduceGreen”  That’s important! Programs are read by people, not computers! def makeSunset(picture): reduceBlue(picture) reduceGreen(picture) def reduceBlue(picture): for p in getPixels(picture): value=getBlue(p) setBlue(p,value*0.7) def reduceGreen(picture): for p in getPixels(picture): value=getGreen(p) setGreen(p,value*0.7)

Considering variations We can only do this because reduceBlue and reduceGreen, do one and only one thing. If we put pickAFile and makePicture in them, we’d have to pick a file twice (better be the same file), make the picture—then save the picture so that the next one could get it! def makeSunset(picture): reduceBlue(picture) reduceGreen(picture) def reduceBlue(picture): for p in getPixels(picture): value=getBlue(p) setBlue(p,value*0.7) def reduceGreen(picture): for p in getPixels(picture): value=getGreen(p) setGreen(p,value*0.7)

Does makeSunset do one and only one thing? Yes, but it’s a higher-level, more abstract thing.  It’s built on lower-level one and only one thing We call this hierarchical decomposition.  You have some thing that you want the computer to do?  Redefine that thing in terms of smaller things  Repeat until you know how to write the smaller things  Then write the larger things in terms of the smaller things.

Are all these pictures the same? What if we use this like this in the Command Area: >>> file=pickAFile() >>> picture=makePicture(file) >>> makeSunset(picture) >>> show(picture) def makeSunset(picture): reduceBlue(picture) reduceGreen(picture) def reduceBlue(picture): for p in getPixels(picture): value=getBlue(p) setBlue(p,value*0.7) def reduceGreen(picture): for p in getPixels(picture): value=getGreen(p) setGreen(p,value*0.7)

What happens when we use a function When we type in the Command Area >>>makeSunset(picture) Whatever object that is in the Command Area variable picture becomes the value of the placeholder (input) variable picture in def makeSunset(picture): reduceBlue(picture) reduceGreen(picture) makeSunset’s picture is then passed as input to reduceBlue and reduceGreen, but their input variables are completely different from makeSunset’s picture.  For the life of the functions, they are the same values (picture objects)

Names have contexts In natural language, the same word has different meanings depending on context.  Time flies like an arrow  Fruit flies like a banana A function is its own context.  Input variables (placeholders) take on the value of the input values only for the life of the function Only while it’s executing  Variables defined within a function also only exist within the context of that function  The context of a function is also called its scope

Input variables are placeholders Think of the input variable as a placeholder  It takes the place of the input object During the time that the function is executing, the placeholder variable stands for the input object. When we modify the placeholder by changing its pixels with setRed, we actually change the input object.

Input variables as placeholders (example) Imagine we have a wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2 So, how do we marry Ben and J.Lo?

Input variables as placeholders (example) Imagine we have a wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2

Input variables as placeholders (example) Imagine we have a wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2

Variables within functions stay within functions The variable value in decreaseRed is created within the scope of decreaseRed  That means that it only exists while decreaseRed is executing If we tried to print value after running decreaseRed, it would work ONLY if we already had a variable defined in the Command Area  The name value within decreaseRed doesn’t exist outside of that function  We call that a local variable def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5)

Writing real functions Functions in the mathematics sense take input and usually return output.  Like ord(character) or makePicture(file) What if you create something inside a function that you do want to get back to the Command Area?  You can return it.  We’ll talk more about return later—that’s how functions output something

Consider these two functions def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) def decreaseRed(picture, amount): for p in getPixels(picture): value=getRed(p) setRed(p,value*amount) First, it’s perfectly okay to have multiple inputs to a function. The new decreaseRed now takes an input of the multiplier for the red value. decreaseRed(picture,0.5) would do the same thing decreaseRed(picture,1.25) would increase red 25%

Names are important This function should probably be called changeRed because that’s what it does. Is it more general?  Yes. But is it the one and only one thing that you need done?  If not, then it may be less understandable.  You can be too general def decreaseRed(picture, amount): for p in getPixels(picture): value=getRed(p) setRed(p,value*amount) def changeRed(picture, amount): for p in getPixels(picture): value=getRed(p) setRed(p,value*amount)

Understandability comes first Consider these two functions  They do the same thing! The first one looks like the other increase/decrease functions we’ve written.  That may make it more understandable for you to write first. But later, it doesn’t make much sense to you  Why multiply by zero? The result is always zero!  Clearing is a special case of decreasing, so a special function is called for. def clearBlue(pic): for p in getPixels(pic): setBlue(p,0) def clearBlue(pic): for p in getPixels(pic): value = getBlue(p) setBlue(p,value*0) Trying to be too general Short and sweet, but specific

Always make the program easy to understand first Write your functions so that you can understand them first  Get your program running ONLY THEN should you try to make them better  Make them more understandable to other people E.g. set to zero rather than multiply by zero Another programmer (or you in six months) may not remember or be thinking about increase/decrease functions  Make them more efficient The new version of makeSunset (I.e. the one with reduceBlue and reduceGreen ) takes twice as long as the first version, because it changes all the pixels twice But it’s easier to understand and to get working in the first place