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:

Slides:



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

Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
 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.
© Calvin College, Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create.
Procedural programming in Java
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
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 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Lesson Three: Organization
Introduction to Methods
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
Chapter 6: Functions.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
Functions and subroutines – Computer and Programming.
Functions Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: Functions Starting Out with C++ Early Objects
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Functions. Functions are named blocks of code. Functions allow complex programs to be broken down into smaller, simpler tasks. Functions allow commonly.
CPS120: Introduction to Computer Science Functions.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Procedural programming in Java Methods, parameters and return values.
Review Loops – Condition – Index Functions – Definition – Call – Parameters – Return value.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
Lesson Two: Everything You Need to Know
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Lesson Two: Everything You Need to Know
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
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.
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.
Processing Variables. Variables Processing gives us several variables to play with These variables are always being updated and you can assume they have.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
JavaScript Modularity. Goals By the end of this lecture, you should … Understand why programmers use modularity. Understand how to create a function in.
Variables. Something to mention… void setup(){ size(200, 200); background(255); smooth(); } void draw() { stroke(0); strokeWeight(abs(mouseX-pmouseX));
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
CS0004: Introduction to Programming
Chapter 7 Functions.
Chapter 7 Functions.
Chapter 4 void Functions
Programming for Artists
Chapter 10 Algorithms.
6 Chapter Functions.
Chapter 10 Algorithms.
Lecture 7: Methods AP Computer Science Principles
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chapter 10 Algorithms.
ALGORITMA PEMROGRAMAN 2 PROCESSING
CPS125.
Presentation transcript:

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: setup()background() draw()ellipse() mousePressed()rect() Some functions are ‘built-in’ You can write your own functions (user-defined functions).

3 Why use functions? Is your draw() method getting a bit long? Two key principles of good programming: 1) Modularity Break down code into smaller parts More manageable and readable Reduce the number of local variables inside a module 2) Reusability Duplicated code (copy/paste?) is not good Must maintain in multiple places Better to put duplicate code in a new function and ‘call’ it from multiple places

The Function ‘Definition’ Make a named ‘block’ with the following parts: Return type Function name Parameters area start block void drawCircle ( ) { // Variables and code go here } // end drawCircle This is also called ‘declaring’ the function. Other Rules: Define function outside all other functions. Name with the same rules as variables (letters, digits, _) Do not use ‘reserved’ words or already used names 4

Example void drawCircle() { fill(0); ellipse(mouseX, mouseY, 50, 50); } 5

Calling a Function Now that we have a function, let’s use it: void draw() { background(255); drawCircle(); } You ‘call’ functions from ‘inside’ other functions In this case, inside draw() 6

Bouncing Ball without Functions Group code into related ‘chunks’ 7

Bouncing Ball with Functions Name the ‘chunks’, declare functions, call them 8

Code is well-organized and readable. Greater ease in debugging. void draw() { background(255); // move(); // bounce(); display(); } 9

Functions with Arguments and Parameters What if you wanted to use a function to do slightly different things? Some examples you have seen that pass arguments to functions include: size(200,200); color(100,150,0); ellipse(x, y, width, height); What if you wanted to write your own function that receives parameters? 10

Arguments (Local Variables) 11 // define the function void drawCircle(int diameter) { fill(0); ellipse(mouseX, mouseY, diameter, diameter); } // call the function drawCircle (50); drawCircle (25); Question: How can you draw the circle with random size?

Multiple Arguments 12 void setup() { size (200, 200); } void draw() { background(255); // Pass drawCircle four arguments drawCircle ( 100,100,64, color(200,200,0) ); drawCircle ( 50,75,32, color(0,200,100) ); drawCircle ( 80,175,40, color(200,0,0) ); } // drawCircle receives four parameters void drawCircle(int x, int y, int diameter, color c) {... }

Keys to Passing Arguments You must pass the same number of arguments as defined in the function parameter list. When an argument is passed, it must be of the same type as declared within the parameters in the function definition. An integer must be passed into an integer a floating point into a floating point.. The value you pass as an argument to a function can be a: Literal value (20, 5, 4.3, etc.), Variable (x, y, size, etc.) The result of an expression (8 + 3, 4 * x/2, random(0,10), etc.) Parameters act as local variables inside the receiving function They are only accessible within that function 13

Return types void setup() { void draw() { void drawCircle(int x, int y, int diameter, color c) { Here is an example that ‘return’ an int: int sum(int a, int b, int c) { int total = a + b + c; return total; } As soon as the return statement is executed, the program exits the function and sends the returned value. 14

A useful example: Distance between 2 pts 15 float distance(float x1, float y1, float x2, float y2) { float dx = x1 – x2; // one side of the right triangle float dy = y1 – y2; // other side of the right triangle float d = sqrt(dx*dx + dy*dy); // hypotenuse length return d; } Processing has a dist() function that does the same thing.

Exercise Use the distance function to calculate a brightness value for each quadrant. 16

Exercise: Bouncing Circle 17 int circleX = 25; int circleY = 25; int speedX = 1; int speedY = 1; void setup() { size(600,200); } void draw() { background(255);... } void moveCircle(){... } void drawCircle(int x, int y, int diameter) {... }