Functions (M Ethods) : An Object Has
Functions (M Ethods) : An Object Has
An object has
Properties
Methods
Functions (M ethods)
Functions Methods
A function is a struct
A function is usually defined in
a class
Decisions, loops, and methods
change the execution flow.
Functions (M ethods)
Key point. Functions provide a new way
Functions (M ethods)
Math functions
x f(x) = 5 x
x, y, z g(x,y,z) = x y/z
f and g are function names
x,y,z are arguments
5x and xy/z are function (return)
values.
Functions (M ethods)
Java functions
A function has a name (identifier)
A function takes zero or more
arguments
A function has a body
A function returns a value or void (no
returns)
Functions (M ethods)
Defining a function
Many commonly-used functions are
Functions (M ethods)
Defining a function
Functions (M ethods)
Examples of defining a function
void setup() --- the function named setup
Functions (M ethods)
Calling a function
FunctionName( actual
arguments );
if the function returns void
or
someVar = FunctionName( actual
args);
if the function returns a value
Functions (M ethods)
Examples in Processing:
double randomNumber =
random(255.0);
rect(0.0,0.0,100,100);
Example in Java:
System.out.print(Hello);
value = inp.nextDouble();
Functions (M ethods)
The number and types of the actual
Functions (M ethods)
Value variable: variable of a primitive
Functions (M ethods)
Reference variable: variable of an object
Functions (M ethods)
Scope (of a name). The code that
Functions (M ethods)
Local and class variables:
Local variable: a variable defined in a
Functions (M ethods)
Examples:
Swap function to demonstrate passing
by value.
Display digit in a number