COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Similar documents
COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

1 Getting started with Processing

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

CISC 1600 Lecture 3.1 Introduction to Processing

CSE 230 Intermediate Programming in C and C++ Functions

Language Fundamentals

Fast Introduction to Object Oriented Programming and C++

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Chapter 1 Histograms, Scatterplots, and Graphs of Functions

Class #1. introduction, functions, variables, conditionals

Object-Oriented Programming in Processing

L E S S O N 2 Background

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Lab Instructor : Jean Lai

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

PROGRAMMAZIONE I A.A. 2017/2018

Solution Notes. COMP 151: Terms Test

Variables One More (but not the last) Time with feeling

Basic Computer Programming (Processing)

ACTIVE PROCESSING Summary Learning Objectives How to Proceed Check your Understanding Learning Objectives 412

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

Variables. location where in memory is the information stored type what sort of information is stored in that memory

Chapter 6 Introduction to Defining Classes

Processing/Java Syntax. Classes, objects, arrays, and ArrayLists

Outline. Turtles. Creating objects. Turtles. Turtles in Java 1/27/2011 TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

What can we do with Processing? Let s check. Natural Language and Dialogue Systems Lab Guest Image. Remember how colors work.

LOON. Language Reference Manual THE LANGUAGE OF OBJECT NOTATION. Kyle Hughes, Jack Ricci, Chelci Houston-Borroughs, Niles Christensen, Habin Lee

C Programming for Engineers Functions

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

Lecture 5: Methods CS2301

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Downloaded from Chapter 2. Functions

CS240: Programming in C

Functions. Functions. nofill(); point(20, 30); float angle = map(i, 0, 10, -2, 2); parameters return values

User Defined Functions

The way I feel about music is that there is no right and wrong. Only true and false. Fiona Apple. true false false

What is a variable? A named locajon in the computer s memory. A variable stores values

Exploring Processing

Introduction. C provides two styles of flow control:

Lecture 04 FUNCTIONS AND ARRAYS

Using Methods. Writing your own methods. Dr. Siobhán Drohan Mairead Meagher. Produced by: Department of Computing and Mathematics

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Notes from the Boards Set # 5 Page

Static Methods. Why use methods?

CIS 110 Introduction to Computer Programming Summer 2014 Midterm. Name:

Building Java Programs Chapter 2

COMP 110 Programming Exercise: Simulation of the Game of Craps

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

QUIZ. What is wrong with this code that uses default arguments?

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

CS 302: INTRODUCTION TO PROGRAMMING IN JAVA. Chapter 5: Methods. Lecture 10

Basics of Java: Expressions & Statements. Nathaniel Osgood CMPT 858 February 15, 2011

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Syntax and Variables

Array. Prepared By - Rifat Shahriyar

Creating objects TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

EC 413 Computer Organization

What Is a Function? Illustration of Program Flow

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Lecture 04 FUNCTIONS AND ARRAYS

[ the academy_of_code] Senior Beginners

5.1. Examples: Going beyond Sequence

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 2

1 Getting started with Processing

Fundamental Concepts and Definitions

Program Fundamentals

egrapher Language Reference Manual

CS240: Programming in C

OpenGL and Qt Creator: a Gentle Introduction

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

MOVING A VERTEX. How must Kirsten move vertex A to be sure that the area of her new triangle is

C++ Quick Guide. Advertisements

CS-201 Introduction to Programming with Java

Course Outline. Introduction to java

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Scheme as implemented by Racket

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

COMP : Practical 8 ActionScript II: The If statement and Variables

Language Reference Manual

Methods: A Deeper Look

Course contents. Overview: Goodbye, calculator. Lesson 1: Get started. Lesson 2: Use cell references. Lesson 3: Simplify formulas by using functions

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CS 170 Java Programming 1. Week 5: Procedures and Functions

Expressions and Casting

Introduction to Modules. Chapter 3. Defining and Calling a Module. Introduction to Modules. 5 Benefits of Modules. Modules CSUS, Spring 2016

Lecture 2 Tao Wang 1

Transcription:

COMP 1010- Summer 2015 (A01) Jim (James) Young young@cs.umanitoba.ca jimyoung.ca

Hello! James (Jim) Young young@cs.umanitoba.ca jimyoung.ca office hours T / Th: 17:00 18:00 EITC-E2-582 (or by appointment, arrange by email)

User-defined functionsmake your own commands

user-defined functions user-defined functions: code which the user can write once and then use over and over. E.g., random was written once and you can use it over and over by calling random(number) create once and use many times!

Example: draw random squares If the mouse is pressed, clear to white first If a key is pressed, clear to black first Random place, random size, random color Global: max size Tedius repetitive Let s make a new command called drawrandomsquare();

user-defined functions require: function name: the keyword used to invoke (use) the function. e.g., ellipse is a function name some code: the processing code to run every time the function is invoked (called).

user-defined functions: syntax void functionname () { //code;

user-defined functions: syntax void functionname () { //java code; void sayhello() { text( Hello,100,100);

user-defined functions: syntax void sayhello() { text( Hello,100,100); this is a the name of a new command we just created! in your other code sayhello();

create outside the draw and start blocks! note: user-defined functions created outside the other blocks you can place them before or after them, it is a matter of style

jump around when a function is called, Processing remembers where it left off, and jumps to the function. When the function is done, it jumps back.

program flow void sayhello() { text( Hello,100,100); to run this command, jump up here! To the compartment! program starts here as usual void draw() { sayhello(); sayhello(); and run the function again! run this command first, as usual when the function is done, come back here and continue where we left off run each command in the function (compartment) in order

Example: draw random squares Let s make a new command called drawrandomsquare();

functions and scope note: scope is the range within which a variable exists. Outside of that scope, you cannot access or work with that variable. blocks define scope! Functions each have their own scope data in one method is not visible to other methods try it

variable scope is limited to its function void sayhello() { text(100,100,message); void draw() { String message = Hey there! ; sayhello(); note: a variable declared in one function is not accessible from another function! you cannot read it, or change it. No nesting of scopes here

the same variable name? void sayhello() { String message = say hi ; println(message); void draw() { String message = Hey there! ; sayhello(); println(message); variables in different scopes can have the same name, but they do not share data they are completely separate. vocab. variables within a function are called local variables

function parameters

Reusing code drawrandomsquares great! what if we want variation? draw different sizes? draw different colors? We need to give parameters to the function.

Send data to a function-> parameters instead of drawrandomsquares(); You want to do: drawrandomsquares(20); // max size Or drawrandomsquares(20, 255); // max size, color the numbers are parameters

Send data to a function-> parameters void functionname (parametertype parametername) You can add parameters to the function void drawrandomsquares() {.. void drawrandomsquares(int size) {..

user-defined functions: syntax void methodname (parametertype parametername) { //processing code; function header function body

How to use parameters inside a function? void drawrandomsquares(int size) { println(size); the parameters become local variables data from outside is copied into the size variable and you can use it within the function just as any other variable. Update drawrandomsquares

function parameters and data is always copied void changedata(int data) { data = data * 2; the information is copied here. inside the function you only have a copy. changes here do not reflect back! void draw() { int data = 4; changedata(data); println(data);

Variant - keep tunnel vision!!!!!! void changedata(int data) { data = data * 2; this variable name data is important for inside the function only. not related to how function is used void draw() { int number = 4; changedata(number); println(number); because information is copied, the caller can give a literal, a variable, do calculations, etc NAME is irrelevant

another example make a function called statusprint which prints a String message surrounded by -- and with its length appended in parenthesis. Print it in he bottom left corner : e.g., if you call: statusprint ( Hi! ); it will print: -Hi!- (3)

multiple parameters a function can have as many parameters as you like, and they can be of different types!! Let s update drawrandomsquare to also take the color

multiple parameters just use commas. void drawrandomsquare(int maxsize, int color) { you can mix types, e.g., have a double, a String, etc. you can have as many parameters as you want, usually <= ~3

Another example Let s make a program with squares moving randomly around the screen bad guys! 1 bad guy: Global variables finals: max move size (20), bg color bad guy color, size, x and y

Make random moves Separate x and y - can move left or right by max move - so range of movement is max max - If 20, then 40 possible positions (41?) - how to generate this as a random number? - random(2*max)-max -> -max...max See the repetition? Hmm.

Make sure doesn t move off the edges of the screen Use ifs, or min and max Draw the badguy set the stroke and fill colors, too

Scale to three bad guys!!! What a mess

Use functions to simplify and reduce repeated code New function! drawbadguy(x, y, size, color); Draw block is now a little simpler but still a lot of code being repeated..

Can we make a command like.. movebadguy(x, y, xmin, xmax, ymin, ymax) movebadguy(badguy1x, badguy1y, 0, width-1, 0, height-1) No since data is only copied in, any changes that happen in that function are not reflected back in our variables. It is thrown away.

What functions give us data back? What do they look like? max, min, random int result = max(10,4);

Functions can only return one piece of data They can give you an integer, A float A string Etc.

Send data back from a function: let s make a function mymax(int a, int b) which gives us an integer to represent the largest of the two: int bigger = mymax(5, 2); // expect 5 to be the answer first let s implement this using the tools we already have we can calculate important information but how do we send it back?

Send data back from a function: returntype functionname (parametertype parametername) int mymax(int a, int b) { int result = a; if (b>a) result = b; return result; the return command does two things: - it ends the function and returns to where it was called from - it passes data along from the function to the caller

user-defined functions: syntax int mymax(int a, int b) { int result = a; if (b>a) result = b; return result; these are local variables, only exists within the function. this name is not related to how you can use the function int max = mymax(10,20);

user-defined functions: syntax int mymax(int a, int b) { int result = a; if (b>a) result = b; return result; int max = mymax(10,20);