Functions. Functions are named blocks of code. Functions allow complex programs to be broken down into smaller, simpler tasks. Functions allow commonly.

Slides:



Advertisements
Similar presentations
Creative Computing. \\ aims By the end of the session you will be able to: 1.Move objects around 2.Write simple interactive programs 3.Use the mouse position.
Advertisements

Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming.
 Functions breakdown: Functions purpose Modularity Declaring and defining a function Calling a function Parameter passing Returning a value Reusability.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 13 Fall 2010.
Code Elements and Processing Coordinate System. Code elements: pages Comments: are documentation notes that are ignored by the computer but are.
Recursion October 5, Reading Read pp in the text.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 26, 2013 Marie desJardins University.
IAT 334 Lab 2 Computer Graphics: Rocket, PImage. June 4, 2010IAT 3342 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Translate, Rotate, Matrix Pages Function Function Definition Calling a function Parameters Return type and return statement.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
CS 201 Functions Debzani Deb.
IAT 800 Lecture 4. Sept 18, Fall 2006IAT 8002 Outline  Programming concepts –Methods –Classes  Talk about project 1  Reading: Read Chapters 1-4 of.
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
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.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Lesson Three: Organization
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
IAT 355 Lecture 4 Computer Graphics: Rocket. May 9, 2014IAT 3552 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
B.A. (Mahayana Studies) Introduction to Computer Science November March Logo (Part 2) More complex procedures using parameters,
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
Mathematical Expressions, Conditional Statements, Control Structures
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
2-D Shapes, Color, and simple animation. 7 Basic Shapes Ellipse :: ellipse() Arc :: arc() Line :: line() Point :: point() Rectangle :: rect() Triangle.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Getting started with the turtle Find the latest version of this document at
Computer Science I Variables. Methods. Classwork/Homework: Re-do or do new drawing using variables & function(s). Zip folder and upload to Moodle dropbox.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Functional Abstraction Reduces Complexity.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
Review Expressions and operators Iteration – while-loop – for-loop.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
Improving the Crab Mrs. C. Furman August 19, 2010.
GEOMETRIC STILL LIFE PAINTING PAINTING STUDIO. WHAT ARE YOU GOING TO BE DOING? You have recently completed a painting using acrylic paint that shows form.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.
Programming in Processing Taught by Ms. Madsen Assistants: Ms. Fischer and Ms. Yen Winsor School, 2/20/08.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
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.
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:
Computer Graphics: Rocket, Java: Class
Computer Science I Variables. Methods.
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 14, Translate & Rotate
Chapter 8 Objects.
LCC 6310 Computation as an Expressive Medium
Chapter 7 Functions.
Chapter 7 Functions.
CS 106A, Lecture 7 Parameters and Return
Code Elements and Processing Coordinate System
Programming for Artists
Chapter 10 Algorithms.
Chapter 5, Conditionals Brief Notes
Code Elements and Processing Coordinate System
Chapter 8 Objects.
Topics Introduction to Functions Defining and Calling a Void Function
OpenGL program.
Chapter 10 Algorithms.
Trigonometry & Random March 2, 2010.
How About Some PI? Trigonometry Feb 18,2009.
Presentation transcript:

Functions

Functions are named blocks of code. Functions allow complex programs to be broken down into smaller, simpler tasks. Functions allow commonly used code to be defined once and run again and again.

Think of functions like a black box

Using a Function A function is called using its name followed by a list of parameters. We’ve already been using functions that are included in Processing. line(0, 10, 10, 20); Function name Parameter values

Writing a Function A function is defined by specifying its name, a list of parameters and its return type. The definition of the line function included in Processing looks something like this: void line(float x1, float y1, float x2, float y2) { // line drawing code goes here... } return type function name parameters

A very simple function The function printHello() has no parameters and returns no value. When a function is defined with a return type of void it means that the function doesn’t return anything. void draw() { printHello(); } void printHello() { println(”Hello”); } Writing a functionCalling a function Is draw() a function???

Making a Tree background(0); fill(255); triangle(50,0,30,40,70,40); rect(50-5,40,10,10); Try this in Processing…

Making a Forest I want several trees in different locations on the screen. I could copy and paste the code but I would need to adjust all the numbers for the new locations. Or… I can just define a function…

Calling drawTree() void setup() { drawTree(); } void drawTree() { fill(255); triangle(50,0,30,40,70,40); rect(50-5,40,10,10) } Is setup() a function???

This didn’t seem to help much… Our function still just draws trees at one fixed location. We need parameters (sometimes called arguments) that allow us to tell the function where we want the tree! This means we need to figure out the relationship between the position of the tree and the location of its parts.

drawTree() With Parameters void drawTree(int topX, int topY) { int treeH = 40; int treeW = 40; triangle(topX, topY, (topX - treeW / 2), (topY + treeH), (topX + treeW / 2), (topY + treeH) ); rect( (topX-5), (topY + treeH),10,10); }

Putting It Together void setup() { background(0); fill(255); drawTree(50,0); drawTree(75,50); drawTree(20,40); } void drawTree(int topX, int topY) { int treeH = 40; int treeW = 40; triangle(topX, topY, (topX - treeW / 2), (topY + treeH), (topX + treeW / 2), (topY + treeH) ); rect( (topX-5), (topY + treeH),10,10); }

Return Values Functions such as radians(), sin() and random() return a value. How does that work? Here is a function max() that returns the max of the two int numbers it receives as parameter values: int max(int number1, int number2) { if(number1 > number2) { return number1 } else { return number2; }

In-class Exercise Rewrite the tree() function. Your new tree() function should enable you to specify the width and height of the tree that you create when you call the function. Write a function to draw a new shape on the canvas multiple times, each at a different position. Be sure to test your function. Turn your groundhog into a function