PROCESSING Methods. Objectives Be able to define methods that return values Be able to define methods that do not return values Be able to use random.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Modular Programming With Functions
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 13 Fall 2010.
Recursion October 5, Reading Read pp in the text.
© Calvin College, Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: Ch. 8 #1-9 exercises:
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
CS 117 Spring 2002 Functions Hanly - Chapter 5 Friedman-Koffman - Sections & Chapter 6.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
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 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Lesson Three: Organization
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
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.
Computer Science: A Structured Programming Approach Using C1 4-6 Scope Scope determines the region of the program in which a defined object is visible.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Review Loops – Condition – Index Functions – Definition – Call – Parameters – Return value.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
CSE202: Lecture 11The Ohio State University1 Functions.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Computer Science I Recap: variables & functions. Images. Pseudo-random processing.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Functional Abstraction Reduces Complexity.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Java – Methods Lecture Notes 5. Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address,
Review Expressions and operators Iteration – while-loop – for-loop.
CHAPTER 8 Scope, Lifetime, and More on Functions.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Lecture 7: Variable Scope B Burlingame March 16, 2016.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
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:
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Building Java Programs
Writing Reuseable Formulas
Chapter 4: Writing Classes
20 minutes maximum exhibits
Chapter 7 Functions.
Chapter 4 Procedural Methods.
Chapter 7 Functions.
Scope of Variables.
6 Chapter Functions.
Topics discussed in this section:
Separating Definition & Implementation
Writing Functions.
Chapter 7 Procedural Methods.
Chapter 9: Value-Returning Functions
Scope.
LCC 6310 Computation as an Expressive Medium
Scope Rules.
Presentation transcript:

PROCESSING Methods

Objectives Be able to define methods that return values Be able to define methods that do not return values Be able to use random and color Be able to trace the flow of control during method calls Be able to mark valid scope of variables in a program

3 Methods In Processing, programmers add new operations by defining methods. Using methods helps us to: Modularize the program; Create helpful procedural abstractions; Improve readability; Encourage reuse. To build new methods, we’ll use the same IID approach we’ve used before.

Method Definition Pattern returnType methodName([parameterDeclarations]){ statements } returnType is the type of value returned by the method or void if the method does not return a value; methodName is the identifier that names the method; parameterDeclarations is a comma-separated list of parameter declarations of the form type identifier that is omitted if there are no parameters; statements is a set of statements that define the behavior of the method. 4

5 Parameters Parameters are variables provided by the calling context and used in the method. Parameter List Pattern: type identifier, type identifier, …

Circles void setup(){ size(250, 250); fill(0,0,255); drawCircle(50, 75, 100); drawCircle(140, 160, 50); } void drawCircle (int x, int y, int diameter){ ellipse(x, y, diameter, diameter); }

Example Frogger random color

8 Returning Values The return type indicates the type of result the method produces. Methods that don’t return values specify void as the return type. Return pattern: return expression; expression is a valid expression whose type is the same as (or is compatible with) the specified return type of the containing method

9 void setup() {... // code setting up float diameter = 2 * computeDistance(midpointW, midpointH, random(scrnW), random(scrnH)); fill(255); drawCircle(midpointW, midpointH, diameter); } float computeDistance(float x, float y, float x2, float y2) { float dx = x – x2; float dy = y - y2; return sqrt(dx*dx + dy*dy); }

Documenting Methods If we expect other programmers to use our methods, we need to document them clearly. Document the purpose and assumptions of each method. 10

11 /** * Compute the Euclidean distance from (x, y) to (x2, y2). * (This partially re-implements Processing's dist() method). * x the x coordinate of position 1 y the y coordinate of position 1 x2 the x coordinate of position 2 y2 the y coordinate of position 2 the distance from (x, y) to (x2, y2) */ float computeDistance(float x, float y, float x2, float y2) { float dx = x - x2; float dy = y - y2; return sqrt(dx*dx + dy*dy); }

12 Scope All identifiers have a scope. An identifier can be reused to identify two different variables but only one will be visible at any time. Scoping rules: Variables are in scope from where they are declared to end of that block. Parameters are in scope throughout the method in which they are declared.

13 void setup() {... // missing code float diameter = 2 * computeDistance(midpointX, midpointY, randomX, randomY); fill(255); drawCircle(midpointX, midpointZxzY, diameter); } void drawCircle(float x, float y, float diameter) { ellipse(x, y, diameter, diameter); } float computeDistance(float x, float y, float x2, float y2) { float dx = x - x2; float dy = y - y2; return sqrt(dx*dx + dy*dy); }

Global Variables External to any method Can be used throughout the program final float E = ; void setup() { int initialPopulation = 20; float rateConst = 2.3; float hours; println(“The number of bacteria after “ + hours + “ hours will be: “ + computePopulation(initialPopulation, rateConst, hours)); } float computePopulation (int initPop, float rate, float hours){ return initPop * pow(E, rate * hours); }

Scope Exercise What does this code do? 15 int z; void setup(){ z = 10; } void draw(){ noLoop(); int x = 4; int y =2; println(sub1(x, y)); } int sub1 (int p, int q){ p = p + 3; q=sub2(p, q); return p + q; } int sub2 (int x, int y){ x = y + 3; y = 4; int z = 12; return x + y + z; }