Methods:
functions & procedures
Top-down programming
Divide-and-conquer technique
Problem is broken down into a series of
smaller problems which are solved.
Lemma
(from http://mathworld.wolfram.com/Lemma.html)
lemma = a short theorem used in proving a larger theorem
The late mathematician P. Erdos has often been associated with
the observation that “a mathematician is a machine for converting
coffee into theorems.”
However, this characterization appears to be due to his friend, Alfred Rényi.
This thought was developed further by Erdos' friend and Hungarian
mathematician Paul Turán, who suggested that weak coffee was suitable “only
for lemmas.”
Program composition mechanisms
1. Structured statements
if, for, switch, while
Program composition mechanisms
2. Data structures
Simple (atomic)
Objects
boolean, int, double, float, char
Scanner, System, Math, String
Text files
Program composition mechanisms
3. Methods
Procedures
Functions
Do work but don’t return anything.
Do work and return some result.
AKA routines, subroutines, etc.
Problem
Say we wish to write a function of our
own.
It should return true if a given character is
lower case, and false otherwise.
How would we do this in one of our
programs now (w/out a function)?
We could use an ‘if’ or a ‘switch.’
How would we do this in one of our
programs now (w/out a function)?
We could use an ‘if.’
boolean isLC = false;
if (ch=='a' || ch=='b' || … || ch=='z')
isLC = true;
Why use a function at all?
W/out function we would have to cut/copy/paste
the code to everywhere we wish to use the
code. If the variable names at that point in your
code change (and they probably will), then you
will have to change the names after the paste.
If the function is long, your programs will
dramatically increase in size.
If we make a mistake, we’ll have to correct the
mistake in many different places.
Just imagine if instead of pressing the sine
button on our calculator, we have to press 1000
buttons and do the actual calculations!
Steps to define your own
function.
1.
2.
3.
4.
Decide on a name for your function.
Determine what arguments your
function needs to do its job.
Determine the return type of your
function.
Code your function.
Steps to define your own
function.
Decide on a name for your function.
2. Determine what arguments your
function needs to do its job.
3. Determine the return type of your
function.
4. Code your function.
Before we code our own function, how
would we apply these steps to create
something familiar like sine?
1.
Back to our original problem
Say we wish to write a function of our
own.
It should return true if a given character is
lower case, and false otherwise.
Steps to define your own
function.
Step 1. Decide on a name.
isLowerCase
Steps to define your own
function.
Step 2. Determine what arguments
your functions needs to do its job.
What does isLowerCase need to do its
job?
1.
One character to check.
ch
After steps 1 and 2.
isLowerCase ( char ch ) {
…
}
Step 3. What type of thing does our function return?
After steps 1, 2, and 3.
static boolean isLowerCase ( char ch ) {
…
}
Now all we have to do is fill in the body of our
functions.
Step 4. Fill in the body of the
function.
static boolean isLowerCase ( char ch )
{
…
}
Now all we have to do is fill in the body of our
functions.
Recall that a l.c. character is one that is in the interval
[‘a’.. ‘z’].
Step 4. Fill in the body of the
function.
static boolean isLowerCase ( char ch )
{
//use a switch
}
Now all we have to do is fill in the body of our
functions.
Recall that a l.c. character is one that is in the interval
[‘a’.. ‘z’].
Step 4. Fill in the body of the
function.
static boolean isLowerCase ( char ch ) {
//use a switch
boolean isLC = false;
switch (ch) {
case 'a' :
case 'b' :
…
case 'z' :
isLC = true;
break;
}
return isLC;
}
Step 4. Fill in the body of the
function.
static boolean isLowerCase ( char ch ) {
//use a switch
switch (ch) {
case 'a' :
case 'b' :
…
case 'z' :
return true;
break;
default :
return false;
break;
}
}
Step 4. Fill in the body of the
function.
static boolean isLowerCase ( char ch )
{
//use an if
}
Now all we have to do is fill in the body of our
functions.
Recall that a l.c. character is one that is in the interval
[‘a’.. ‘z’].
Step 4. Fill in the body of the
function.
static boolean isLowerCase ( char ch )
{
//use an if
if (ch=='a' || ch=='b' || … || ch=='z')
return true;
return false;
}
Any other way to use an if?
Step 4. Fill in the body of the
function.
static boolean isLowerCase ( char ch )
{
//use an if
if (ch<'a' || ch>'z')
return false;
return true;
}
Step 4. Fill in the body of the
function.
static boolean isLowerCase ( char ch )
{
//just use a return
}
Step 4. Fill in the body of the
function.
static boolean isLowerCase ( char ch )
{
//minimal
return ('a'<=ch && ch<='z');
}
Using our function
if (isLowerCase('c')) …
if (isLowerCase(c1)) …
if (isLowerCase(c1) || isLowerCase(c2)) …
Architecture of a function
Definition of function
static boolean isUpperCase (char p )
{
if ('A'<=p && p<='Z')
return true;
return false;
}
Function heading or signature (or
prototype)
static boolean isUpperCase (char p )
{
if ('A'<=p && p<='Z')
return true;
return false;
}
Function body
static boolean isUpperCase (char p )
{
if ('A'<=p && p<='Z')
return true;
return false;
}
Function parts
static boolean isUpperCase (char p )
{
if ('A'<=p && p<='Z') return true;
return false;
}
Function type
Function name
Formal parameters (function parameters)
Formal parameters
static boolean isUpperCase ( char p ) {
if ('A'<=p && p<='Z') return true;
return false;
}
Formal parameters
0 or more of them
Local variables
Hold values of actual parameters
What happens when a function is
called/used/invoked?
1.
2.
3.
Formal parameter(s) is created as a new
variable. New variable is initialized to
actual parameter value.
Execute function body. Return function
value.
Program continues after function
designator (invocation).
More functions
Say we wish to define a function
that calculates the square of a
number.
What should we call it?
What does it need (formal parameters) to do
the calculation?
What types of numbers does it need?
How will we give back the result?
1.
2.
3.
4.
Does it give back a result?
What is the type of the result (if any)?
5.
We use the keyword void to indicate that we will
not return anything.
Say we wish to define a function
that calculates the square of a
number.
1. What should we call it?
Say we wish to define a function
that calculates the square of a
number.
1. What should we call it?
square
Say we wish to define a function
that calculates the square of a
number.
2. What does it need (formal parameters)
to do the calculation?
Say we wish to define a function
that calculates the square of a
number.
2. What does it need (formal parameters)
to do the calculation?
A number to square.
Say we wish to define a function
that calculates the square of a
number.
3. What types of numbers does it need?
Say we wish to define a function
that calculates the square of a
number.
3. What types of numbers does it need?
double
Say we wish to define a function
that calculates the square of a
number.
After steps 1 to 3, we have:
square ( double x )
Say we wish to define a function
that calculates the square of a
number.
4. How will we give back the result?
Does it give back a result?
Say we wish to define a function
that calculates the square of a
number.
4. How will we give back the result?
Does it give back a result?
Yes.
Say we wish to define a function
that calculates the square of a
number.
5. What is the type of the result (if any)?
We use the keyword void to indicate that we
will not return anything.
Say we wish to define a function
that calculates the square of a
number.
5. What is the type of the result (if any)?
We use the keyword void to indicate that we
will not return anything.
Double
So now we have:
static double square ( double x ) {
…
}
Say we wish to define a function
that calculates the square of a
number.
static double square ( double x ) {
//let’s define the body of the function.
}
Say we wish to define a function
that calculates the square of a
number.
static double square ( double x ) {
double result = x*x;
return result;
}
Did you write this in your notes?
Areas from geometry
Asquare = length * length
Arectangle = width * height
Atriangle = ½ * base * height
Aparallelogram = base * height
Can you define functions that perform
each of these calculations?
© Copyright 2026 Paperzz