Announcements Mid-Term Exam!! Quiz 5 Again + Quiz 6 Exercise 5

Slides:



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

DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later.
Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.
 Communication:  Provide context, e.g., s …  TA Office hour for next Tuesday: CANCELLED  My name is: ??  Show  Scores on-line, Quiz-1 results.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Changing Control.
 Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 
Method exercises Without IF. Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your.
Introduction to Methods
COMP More About Classes Yi Hong May 22, 2015.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Assignment 6.
Applications Development
Python Functions.
1 CS161 Introduction to Computer Science Topic #9.
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 …
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
 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.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Functional Abstraction Reduces Complexity.
Methods.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
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.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 9: Value-Returning Functions
4. Java language basics: Function
User-Written Functions
COMP 170 – Introduction to Object Oriented Programming
Chapter 8 Objects.
Programmazione I a.a. 2017/2018.
Layering: Building Functions out of Functions
Engineering Innovation Center
Announcement Tomorrow’s office hr: CANCELLED! Show work!!
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Variables & Datatypes CSE 120 Winter 2018
To do … Web Exercise: Quiz-3, HW#2 Score: update
Expressions & Control Flow CSE 120 Winter 2018
Teaching London Computing
Variables & Datatypes CSE 120 Spring 2017
Light Bot Solutions: an analysis
Light Bot Solutions: an analysis
Announcement Function ppt: download again
Announcements How’s it going? My internet/power supply?
Chapter 8 Objects.
Testing and Repetition
Announcements: Reading materials are posted on Discussion Board.
Announcements Survey … no really, you will be polled
Variables & Datatypes CSE 120 Winter 2019
Variables, Assignments Testing + Iteration
Fundamentals of Software Development 1
Lecture 6 Methods and Classes
Method exercises Without IF
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
Digital Thinking: Animation, Video Games, and the Social Web
Lawrence Snyder University of Washington
Methods/Functions.
Announcements Survey feedback: summary Quiz: HW 7:
CPS125.
Lecture 6 Methods, Classes, Inheritance
Today: to list: Syllabus change: Grade on-line
Announcements Jack group member by Thursday!! Quiz-8? HW10
Presentation transcript:

Announcements Mid-Term Exam!! Quiz 5 Again + Quiz 6 Exercise 5 Next Thursday!! In class, one-page note Quiz 5 Again + Quiz 6 Answers Exercise 5 Answer Homework 8 Mid-Term Exam: Next Thursday! 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Datatypes and Functions Kelvin Sung University of Washington, Bothell (* Use/Modification with permission based on Larry Snyder’s CSE120 ) © Lawrence Snyder 2004

Plan For Today Two ideas – data types and functions – are already familiar to you, because you’ve been using them Today, we teach their details Data types Functions Also, today, we’ll cover some handy “tricks” using those ideas 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Data Types Information has certain properties … we group information with similar properties into “types” -- integers, or whole numbers floating point, usually called decimal numbers colors, a triple of numbers for R, G and B Etc. In order for computers to process data, they need to know its type So, we always specify the data’s type 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Give Datatypes in Declarations Processing has a series of datatypes The most important datatypes for us are int, float, boolean and color Find details in the references When declaring variables we list them after the type, as in int x, y, z; float half_step = 0.5, whole = 1.0; color yellow = color(200,200,0); 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Examples: At Top of a Program 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

At The Top of Functions 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Global Variable Preserve Info 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Hiding In Another Function … 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Functions, A Review Functions have been used in Lightbot 2.0: F1 Functions in HW 03: F.turn( ), HW 05: Cols … 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 … declaration of its instructions function call … a request to run the function Let’s get the details down now … 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Functions In Processing The form of function definition in Processing <return type> <name> ( <param list> ) { <body> } 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); or 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Functions In Processing The form of function definition in Processing <return type> <name> ( <param list> ) { <body> } 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); or 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Functions In Processing: Result Functions that do something, but do not return a value, have void as their <return type> Functions that return a value must say the datatype of the value returned void draw_a_box (int x_pos, int y_pos) { rect(x_pos, y_pos, 20, 20); } color pink ( ) { return color(255, 200, 200); 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Functions In Processing: Params 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); 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Functions In Processing: Args An argument is the input value given to a parameter when a function is called, as in draw_a_box(50, 200); The value of the argument becomes the value of the corresponding parameter: draw_a_box(50, 200); void draw_a_box (int x_pos, int y_pos) { rect(x_pos, y_pos, 20, 20); } 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Functions In Processing: Return 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); 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Plan The Function in Declaration The function has a name, parameters, def Name: drawColumn Parameters: int offset; Definition: Nothing has to be returned rect(20+offset, 250, 60, 20); rect(30+offset, 270, 40, 10); ellipse(30+offset, 275, 10, 10); ellipse(70+offset, 275, 10, 10); rect(35+offset, 280, 30, 60); 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

The Function Declaration & Calls The result: The calls 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Parameters Parameters are automatically declared (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, e.g. o-f-f-s-e-t that are meaningful to you I chose offset as the orientation point of the figure in the x direction Notice that I used that name a lot, and the meaning to me remained the same 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Arguments Become Parameters Notice that if the DEFINITION has n parameters, the CALL needs n arguments The parameters and arguments correspond 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) 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Using Functions + Global Variables Mouse clicks control the direction … dir flips from 1 to -1 on each click 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)

Summary on Functions When we have something we have to produce and perhaps use repeatedly, we need a function We need two things: a declaration, and calls The declaration is the function “package” The call is the use, where we ask to “run” it So we declared the function, and called it Need to follow the rules on form: “void”, parens, give type of parameters, curly braces, indent, comment, … Use parameter values where changes are needed 12/28/2018 Kelvin Sung (Use/Modify with permission from © 2010-2012 Larry Snyder, CSE)