Functions in Processing CSE 120 Spring 2017

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
Structures Spring 2013Programming and Data Structure1.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 26, 2013 Marie desJardins University.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
1 Lab Session-VI CSIT-120 Spring 2001 Let us look at C++ syntax rules in brief Exercise VI-A (Demo Required) Lab Assignment#4 Due May 1st, 2001 (No Lab.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Introduction to Methods
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
Why to Create a Procedure
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Assignment 6.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
CPS120: Introduction to Computer Science Functions.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Python Functions.
Chapter 4 Introduction to Classes, Objects, Methods and strings
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter 6 Methods Chapter 6 - Methods.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Methods CSCI 1301 What is a method? A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Nested Loops & Arrays CSE 120 Spring 2017
Expressions & Control Flow CSE 120 Spring 2017
Recursion II CSE 120 Spring 2017
User-Written Functions
User Interaction and Variables
Basic Input and Output CSE 120 Spring 2017
Chapter 6: User-Defined Functions I
Processing Introduction CSE 120 Spring 2017
Recursion I CSE 120 Spring 2017 Instructor: Teaching Assistants:
Chapter 7 Top-Down Development
Developing an App CSE 120 Spring 2017
Lightbot and Functions CSE 120 Winter 2017
Week 8 - Friday CS 121.
Lecture 10: More on Methods and Scope
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Layering: Building Functions out of Functions
User-Defined Functions
Functions in Processing CSE 120 Winter 2018
Basic Input and Output CSE 120 Winter 2018
Variables & Datatypes CSE 120 Winter 2018
Processing and Drawing CSE 120 Winter 2018
Expressions & Control Flow CSE 120 Winter 2018
Binary Numbers CSE 120 Spring 2017
Variables & Datatypes CSE 120 Spring 2017
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Lightbot and Functions CSE 120 Winter 2018
CSE 115 September 29 – October 3, 2008.
Nested Loops, Arrays CSE 120 Winter 2018
Announcements Mid-Term Exam!! Quiz 5 Again + Quiz 6 Exercise 5
CSE 115 September 29 – October 3, 2008.
Functions in Processing CSE 120 Winter 2019
Basic Input and Output CSE 120 Winter 2019
Variables & Datatypes CSE 120 Winter 2019
C++ Constructor Insanity CSE 333 Summer 2018
Loops & Nested Loops CSE 120 Winter 2019
Parameters and Arguments
Presentation transcript:

Functions in Processing CSE 120 Spring 2017 Instructor: Teaching Assistants: Justin Hsia Anupam Gupta, Braydon Hall, Eugene Oh, Savanna Yee When Pixels Collide For April Fool's Day, Reddit launched a little experiment. It gave its users, who are all anonymous, a blank canvas called Place. The rules were simple. Each user could choose one pixel from 16 colors to place anywhere on the canvas. They could place as many pixels of as many colors as they wanted, but they had to wait a few minutes between placing each one. Over the following 72 hours, what emerged was nothing short of miraculous. http://sudoscript.com/reddit-place/

Administrivia Assignments: Custom Logo due today (4/7) Lego Family due Sunday (4/9) Make sure to take advantage of office hours and Piazza!

Drawing a Square with Functions [See Demo on Panopto]

Donatello as a Function What’s wrong with this code? What happens if we call donatello() twice?

Donatello Function Parameterized Can now call donatello() function with different x_pos

Return Type What the function sends back to whoever called it Can be any of the datatypes: int, float, color, etc. If not returning anything, then we use void return type

Function Name Does not matter to computer, but does to humans Should describe what the function does Must start with a letter, but can contain numbers and underscores Why not hyphen? No two functions (or variables) can have the same name function name

Parameters Required part of every function definition Must be surrounded by parentheses If no parameters, parentheses are left empty Datatype and name for every parameter must be specified Separate parameters with commas parameters

Function Body body

F.turn_around() Right, Right. F.turn_around() Right, Right. Lightbot Functions Lightbot functions had a different syntax, but similar parts: F.turn_around() Right, Right. function name parameters body F.turn_around() Right, Right.

Parameters vs. Arguments

Parameters vs. Arguments When you define a function, you specify the parameters Use parameters for values that you want to be different on different calls to this function When you call a function, you pass arguments The order of the arguments must match the order of the parameters We define a function once, but can call it as many times as we want!

Variable Scope When an argument is passed to a function, what does the function actually get? Internal variables (i.e. parameters) get a copy of the argument value Internal variables only exist within the function they are declared The variables “cease to exist” when the function finishes “Scope” of a variable is the part(s) of code where that variable name binding is valid (i.e. where it exists)

Question If you’re dreaming and someone in your dream hands you a turnip, do you wake up with a turnip in your bed? Yes No I will report back on Monday Variable scope demo in Processing: [see Panopto] Demo: addOne(int val) function that adds one. Call from draw(), use noLoop(). Print onto drawing canvas using text().

Parameter Example

Parameter Example

Solving Problems Understand the problem What is the problem description? What is specified and what is unspecified? What has been given to you (e.g. starter code)? Break the task down into less complex subtasks Example: Make a function that draws a row of five mice with their ears touching/overlapping. The mice should all be the same color except for the middle one, which should be red.

Looking Forward Lego Family Events Animal Functions Design an abstracted family Create functions for drawing each family member, including variables for position/movement Have family members start at corners, then move into place Events Introduce user interactions! Due Tuesday (4/11) Animal Functions Start in lab on Tuesday, due Wednesday (4/12) Design your own animal (like the mouse shown here)