Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Functional Abstraction Reduces Complexity.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Spring Semester 2013 Lecture 5
Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 13 Fall 2010.
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Changing Control.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Sep 26, Fall 2006IAT 800 Lecture 6 Methods and Classes.
1 Gentle Introduction to Programming Session 2: Functions.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 201 Functions Debzani Deb.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Announcements Attendance sheet is going around – be sure you sign it! First part of.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Introduction to Methods
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Classes / Objects An introduction to object-oriented programming.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
chap13 Chapter 13 Programming in the Large.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
L-1 Lecture 13: Functions and Design © 2000 UW CSE University of Washington Computer Programming I.
Loops and Iteration for Statements, while Statements and do-while Statements.
Computation as an Expressive Medium Lab 3: (Methods and) Classes Jason Alderman.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Assignment 6.
Functions. Functions are named blocks of code. Functions allow complex programs to be broken down into smaller, simpler tasks. Functions allow commonly.
ECE122 Feb. 22, Any question on Vehicle sample code?
CIS 3.5 Lecture 2.2 More programming with "Processing"
1 CS161 Introduction to Computer Science Topic #9.
Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 More details and explanation …
CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous.
 Functions package up computation … when do we use them? All the time.  Write some simple code to achieve a goal … 12/20/2015© 2010 Larry Snyder, CSE1.
Repetition 8/9/2009. Learning to Program -- Suggestions Spend a lot of time fiddling around with code –Programming is something you have to learn by doing.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
Week 8 - Friday.  What did we talk about last time?  Static methods.
Review Expressions and operators Iteration – while-loop – for-loop.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Making an example of Pacman.
L-1 Lecture 12: Functions and Design © 2000 UW CSE University of Washington Computer Programming I.
Functions. 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are:
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
User-Written Functions
CSE / ENGR 142 Programming I
Layering: Building Functions out of Functions
Functions in Processing CSE 120 Winter 2018
Static Methods 14-Nov-18.
Object Oriented Programming (OOP) LAB # 5
Announcements Mid-Term Exam!! Quiz 5 Again + Quiz 6 Exercise 5
Announcements How’s it going? My internet/power supply?
Chapter 8 Objects.
Testing and Repetition
Lawrence Snyder University of Washington, Seattle
Announcements How to save your work? Quiz solution 5/5/2019
Announcements: Questions: Names on the board? Why?
Basic Processing … Drawing pictures … It’s not art, it’s fun
Lawrence Snyder University of Washington
Methods/Functions.
Chapter (3) - Procedures
CPS125.
Presentation transcript:

Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Functional Abstraction Reduces Complexity

 Today we talk about functions in Processing and use them in a layering technique to build a timer  Introduce Functions  Draw digital timer elements  Assemble elements into digits  Light digit segments to create numbers  Select number based on a digit 2/1/2016© 2010 Larry Snyder, CSE2

 We’ve used functions, also known as  procedures  methods  subroutines in all of our Processing code: size(200, 200)  Recall that functions have two parts:  function definition … a statement of how it works  function call … a request to have it performed 2/1/2016© 2010 Larry Snyder, CSE3

 Form of function definition in Processing ( ) { } as in void draw_a_box (int x_pos, int y_pos) { rect(x_pos, y_pos, 20, 20); } color pink ( ) { return color(255, 200, 200); } 2/1/2016© 2010 Larry Snyder, CSE4 or

 Functions that do something, but do not return a value, have void as their  Functions that return a value must say its type void draw_a_box (int x_pos, int y_pos) { rect(x_pos, y_pos, 20, 20); } color pink ( ) { return color(255, 200, 200); } 2/1/2016© 2010 Larry Snyder, CSE5

 Parameters are the values used as input to the function; parameters are not required, but the parentheses are  The type of each parameter must be given void draw_a_box (int x_pos, int y_pos) { rect(x_pos, y_pos, 20, 20); } color pink ( ) { return color(255, 200, 200); } 2/1/2016© 2010 Larry Snyder, CSE6

 A function returns its value with the return statement … the stuff following return is the result  The function is done when it reaches return void draw_a_box (int x_pos, int y_pos) { rect(x_pos, y_pos, 20, 20); } color pink ( ) { return color(255, 200, 200); } 2/1/2016© 2010 Larry Snyder, CSE7

 Processing function definitions are typically listed after the standard blocks: setup( ), draw( ), mousePressed( ), etc. 2/1/2016© 2010 Larry Snyder, CSE8 Function Call Function Definition

 Once defined, functions can be called repeatedly … it’s the point of writing them! 2/1/2016© 2010 Larry Snyder, CSE9 Function Calls Function Definition

 Notice that if the DEFINITION has n parameters, the CALL needs n arguments  The parameters and arguments correspond 2/1/2016© 2010 Larry Snyder, CSE10 Inside of the function, the parameter, e.g. xbase, is declared and initialized to the corresponding argument, e.g. 80. Then, the definition uses it, e.g. rect(80, 40+10, 20, 40) Inside of the function, the parameter, e.g. xbase, is declared and initialized to the corresponding argument, e.g. 80. Then, the definition uses it, e.g. rect(80, 40+10, 20, 40)

 Parameters are automatically instantiated (and initialized) on a call, and remain in existence as long as the function remains unfinished  When the function ends, the parameters vanish, only to be recreated on the next call  It is wise to choose parameter names that are meaningful to you  I chose xbase as the orientation point of the figure in the x direction 2/1/2016© 2010 Larry Snyder, CSE11

 Draw digital timer elements  Assemble elements into digits  Light digit segments to create numbers  Select number based on a digit 2/1/2016© 2010 Larry Snyder, CSE12

 Patter: Parameterize the functions by a consistent position – lower left corner is good 2/1/2016© 2010 Larry Snyder, CSE13

2/1/2016© 2010 Larry Snyder, CSE14

 Define the illumination of the digit  Must declare two color variables, initialize to proper colors, use them in fill, and check ‘em 2/1/2016© 2010 Larry Snyder, CSE15

 Light up the digit for each number: ^C ^P 2/1/2016© 2010 Larry Snyder, CSE16

 Given an integer, display it in lights 2/1/2016© 2010 Larry Snyder, CSE17 …

2/1/2016© 2010 Larry Snyder, CSE18 Here’s The Action

 2/1/2016© 2010 Larry Snyder, CSE19

 Review What We Did  The computation is ONLY drawing triangles and rectangles, but we don’t think of it that way … to us, it’s a timer 2/1/2016© 2010 Larry Snyder, CSE20 triangle, rect, triangle hexa rexa three_digit sel digitonetwothreefourfivesixseveneightninezero