Chapter 7 Functions.

Slides:



Advertisements
Similar presentations
 Functions breakdown: Functions purpose Modularity Declaring and defining a function Calling a function Parameter passing Returning a value Reusability.
Advertisements

IAT 334 Java using Processing ______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY.
Week 9: Methods 1.  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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
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.
Lesson Three: Organization
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
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,
Lesson Two: Everything You Need to Know
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Review Expressions and operators Iteration – while-loop – for-loop.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
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:
Some of Chap 17.
User-Written Functions
Lesson #6 Modular Programming and Functions.
Classwork: Examine and enhance falling drop(s) or make your own.
Lesson #6 Modular Programming and Functions.
Chapter 14, Translate & Rotate
Chapter 8 Objects.
CO1401 Programming Design and Implementation
Functions CIS 40 – Introduction to Programming in Python
20 minutes maximum exhibits
Functions.
Chapter 6 Loops (iteration).
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 7 Functions.
User Defined Functions
Chapter 5 Function Basics
Chapter 4 void Functions
Chapter 10 Algorithms.
6 Chapter Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
Chapter 5, Conditionals Brief Notes
Chapter 10 Algorithms.
Chapter 8 Objects.
Topics Introduction to Functions Defining and Calling a Void Function
In C Programming Language
Defining Classes and Methods
LCC 6310 Computation as an Expressive Medium
Predefined Functions Revisited
Chapter 10 Algorithms.
Chapter 4, Variables Brief Notes
Trigonometry & Random March 2, 2010.
ALGORITMA PEMROGRAMAN 2 PROCESSING
CPS125.
Chapter 9 Arrays.
Agenda for Unit 3: Functions
Presentation transcript:

Chapter 7 Functions

In this chapter you will learn: Modularity Declaring and defining a function Calling a function Arguments and parameters Returning a value Reusability

About Functions Functions provide a clean and elegant way to reuse code and reduce problems. They allow you to separate parts of programs into modular pieces. Functions should have a single logical task. Examples …

Definition A function is a routine or a set of instruction or code that performs a specific task and can be processed independently.

Deja vu all over again setup( ), draw( ), and mousePressed( ) are all functions. These are special functions that are part of the Processing huge library. The ones that you create are called user-defined functions.

Importance of user-defined functions Modularity – Break down programs into smaller and more manageable parts. Reusable – allow reuse of code without retyping it. Ability to call code multiple times and change values. Easier debugging – this is already implied in modularity. But the idea is that you can test one part at a time. No need to search to long code for single task or problem.

A function definition (AKA declaration) has 3 parts Return type Function name Arguments   General format:  returnType functionName( parameter-list ){ // function code }

Defining and calling a function. EXAMPLE: Defining and calling a function. (Note: This is only a part of the code) void setup() { size(300,300); } void draw() { //stickFig(); clouds(); void clouds() { int x = 40; noStroke(); fill(#66ccff); ellipse(x, 60, 50, 30 ); ellipse(x+10, 40, 60, 40 ); ellipse(x+40, 50, 50, 40 ); ellipse(x+30, 60, 60, 40 ); }

Example 7-1 and 7-2 from book: Will do with remix to add another function of spiraling dots. Nothing new to learn, but take time to build a strong foundation. //this is examples 7.1 and 7.2, REMIXED void setup(){ size(300,300); } void draw(){ background(255); drawCircle(); //mySpiral(); void drawCircle() { fill(#00ccff ); ellipse(width/2, height/2, 200, 200); //void mySpiral() { //translate(width/2, height/2); // for (float i = 0; i <width; i++) { // fill(random(200), 120, 175); // rotate(2); // ellipse(i, 0, 10, 10); // } //}

In-Class Exercise (participation grade) You will upload this to openprocessing.org Use the spiral function from the previous exercise and expand on it. Add two more display functions. Examples are new shapes such as rectangles and triangles. 3 examples shown below but do your own thing. Add it to the “collections” called Function with Spirals at https://www.openprocessing.org/class/56535 Then comment on at least one of classmates’ sketches.

At-home Challenge, but not for submission Simple Modularity: At-home Challenge, but not for submission Since we have done 3 function exercises, try doing example 7.4 without looking. This is not to turn in, but just to get so comfortable that you can create functions with your eyes closed.

Arguments Arguments and parameters make functions even more powerful. Remember that arguments are values that are passed into functions. Example: line(x1, y1, x2, x2) with parameters line(5, 10, 65, 10) with arguments Typing the actual values means passing arguments into the function. Likewise, we can create our own functions that expect/require an argument. Example would be: void clouds (color c) { fill(c); etcetera… void setup() { size(300,300); } void draw() { stickFig(); clouds(); translate(180, -20); translate(20, 50); void stickFig() { stroke(0); fill(255); ellipse(width/2,100, 60, 60); //HEAD line(width/2, 130, width/2, 220); //body line(width/2 - 30, 170, width/2 + 30, 170); //arms line(width/2 - 30, 290, width/2, 220); //legs line(width/2, 220, width/2+ 30, 290); void clouds() { int x = 40; noStroke(); fill(#66ccff); ellipse(x, 60, 50, 30 ); ellipse(x+10, 40, 60, 40 ); ellipse(x+40, 50, 50, 40 ); ellipse(x+30, 60, 60, 40 );

We can also call functions inside of a loop as shown below: EXAMPLE: We can also call functions inside of a loop as shown below: //same example using entire stick figure int x = 150; void setup() { size(300,300); noLoop(); } void draw() { for (x = 0; x<width; x += 80) { stickFig(); void stickFig() { stroke(0); color c = color (random(255), random(255), random(255)); fill(c); ellipse(x, 100, 60, 60); //HEAD line(x,130, x , 220); //body line(x-30, 170, x + 30, 170); //arms line(x-30, 290, x , 220); //legs line(x,220,x + 30, 290);

Page 126, play with car for a minute void setup() { size(200,200); } void draw() { background(255); drawCar(100,100,64, color(200,200,0) ); drawCar(50, 75, 32, color(0, 200, 100) ); drawCar(80, 175, 40, color(200, 0, 0) ); void drawCar(int x, int y, int thesize, color c ) { int offset = thesize/4; //main car body rectMode(CENTER); stroke(200); fill(175); rect(x,y, thesize, thesize/2); // the 4 wheels fill(c); rect(x- offset, y - offset, offset, offset/2); rect(x+ offset, y - offset, offset, offset/2); rect(x -offset, y + offset, offset, offset/2); rect(x + offset, y + offset, offset, offset/2); Note clever use of relative values Challenge: - Add 5th car - Allow each car to be different color.

Passing Arguments, important facts Must pass same number of arguments as defined in parameters. Argument must be same type as declared within definition. Functions accept literal values, variables, and results of expressions. Parameters act as local variables to a function and are only accessible within that function.

Happy Kids Fashion Show /*the sketch allows jumping girls to be displayed with matching dress and hat.*/ float x = 50; float y = 50; void setup(){ size(600, 200); frameRate(20 ); } void draw() { background(#EAF58A); displayGirl(#AABBFF, 550); //kidJump(10); void displayGirl(color clothes, int x) { //kid's dress fill(clothes); triangle(x, y, x+40, y+80, x-40, y+80); //kid's face fill(#ecc19c); ellipse(x, y+5, 40, 40); //I NEED YOU TO DRAW EYES & LIPS //kid's hat arc(x, y, 50, 50, PI, TWO_PI, PIE); void kidJump(float howHigh ) { y = y + random(-howHigh,howHigh) ; //PLEASE CONSTRAIN KIDS TO KEEP THEM ON SCREEN

Passing a Copy AKA “pass by value” means the parameter of called functions will be a copy of the argument that is passed. The original value remains unchanged. Note: In this case, we’re passing a variable (num) into a function (randomizer).

A good time to notice the flow of a program Recall that code is executed in the order in which they are written… …but when a function is invoked, the code leaves the current line and executes that functions, then returns to where it left off.

Return Type A return type is the data type that the function returns. Void means no return type random, distance, etc. don’t require type. It’s understood.

Return Type When return statement is executed, the program exits the function and sends the return value back to location in the code where the function was called. That value can be used in a variety of ways: Assign value to a variable Within an expression