Functions
> Click or hit Control-Enter to run the code above
This is How We Do This
It won’t get easier, but it also won’t get much worse.
Your Week in CS 125
-
Sunday/Monday: finish MPN - 1, start MPN.
-
Monday: MPN. Quiz. Class. Homework. Office Hours.
-
Tuesday: MPN. Quiz. Lab. Homework.
-
Wednesday: MPN. Quiz. Class. Homework. Office Hours.
-
Thursday: MPN. Homework. Office Hours.
-
Friday: MPN. Quiz. Class. Homework. Office Hours.
-
Weekend: MPN.
What Are Computers Good At?
-
Basic math
-
Simple decision making
-
Doing things over and over again very, very fast
-
Storing data
-
And communicating (we’ll get there)
What’s Next?
These are the three main challenges that will occupy us for the remainder of the semester:
-
Algorithms: How do we use computers to solve problems?
-
Data structures: How do we structure information to enable efficient algorithms?
-
Software development: How do we write, debug, test, and publish good computer software?
-
And we’re going to have a lot of fun continuing to learn everything and anything about how computers and computer systems work.
How Do We Structure Good Computer Programs?
-
Break our code into reusable, testable, and understandable pieces
-
Combine state and behavior
-
Document our code appropriately
-
Reuse preexisting solutions as much as possible
-
Share our code with others!
Functions (Or Subroutines)
Function: a sequence of program instructions that perform a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.
-
In Java, a function takes zero or more inputs and produces no or one output.
-
Functions create a block containing the imperative programming building blocks we have already seen: variables, loops, conditional statements.
Functions (Or Subroutines)
A good function:
-
Does one thing well
-
Can be easily tested
-
Can be reused in multiple places
Function Declaration
/**
* Add two numbers together.
*
* @param firstNumber the first number to add
* @param secondNumber the second number to add
* @return the sum of the two numbers
*/
int add(int firstNumber, int secondNumber) {
return firstNumber + secondNumber;
}
-
In Java every function has a name, a list of arguments, a return type, a Javadoc description, and a return statement.
-
The name and description are for you, the programmer
-
The arguments and return type are both for you and for Java
-
The Javadoc is for you and for others
Calling Functions
To call a function we provide the arguments it requires and expect a result of the type it is declared to return.
-
The code that calls the function is referred to as the caller.
-
When a function is called, the caller waits until the function returns
-
The result can be saved, used like a literal of the return type, or ignored
add
Example
/**
* Add two numbers together.
*
* @param firstNumber the first number to add
* @param secondNumber the second number to add
* @return the sum of the two numbers
*/
int add(int firstNumber, int secondNumber) {
return firstNumber + secondNumber;
}
int result = add(3, 4);
System.out.println(add(4, 5));
int biggerResult = add(10, 20) + add(20, 30) + 10;
System.out.println(biggerResult);
add(6, 7); // This is dumb code but will compile and run
> Click or hit Control-Enter to run the code above
Good Variable Function Names
Just like variables, choosing good function names will make your life a lot easier as a programmer.
Good function names are:
-
Descriptive
-
Indicative of the function’s function—what it does
-
As succinct as possible…
-
But see #2 above
Function Arguments
When a function begins executing the code has access to the variables declared as arguments, which will contain the values passed by the caller.
You can think of these variables as pre-declared and pre-initialized.
add
Example
/**
* Add two numbers together.
*
* @param firstNumber the first number to add
* @param secondNumber the second number to add
* @return the sum of the two numbers
*/
int add(int firstNumber, int secondNumber) {
System.out.println(firstNumber);
System.out.println(secondNumber);
return firstNumber + secondNumber;
}
System.out.println("Start");
int result = add(3, 4);
> Click or hit Control-Enter to run the code above
return
and Return Type
A return
statement immediately exits the function and returns a result.
-
return
statements can appear anywhere inside a function: inside a loop or conditional statement -
A function can include multiple return statements
-
Regardless of where they appear or how many are used, the function returns as soon as the first
return
statement is reached -
A function must return a value of the type it declared
Javadoc Documentation
Good code requires documentation. This is for the humans that use it.
In Java, we utilize the Javadoc tool to turn comments into our code into structured online documentation.
That allows this:
/**
* Example Javadoc.
*
* @param firstArgument first argument to my function
* @return 0
*/
…to become this…
Functions Frequently Implement Algorithms
Review: Algorithms
Algorithm: a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer.
As computer scientists, we implement algorithms by having computers:
-
Perform simple calculations
-
Store the results
-
Make simple decisions
-
Do things over and over again as fast as possible
Consecutive Identical Values
Given an array of chars, find all cases where consecutive elements are the same.
First, what is our algorithm?
-
Examine each value in the array
-
Compare it with the next value—but how do we get at that?
-
Print it out if they are the same
> Click or hit Control-Enter to run the code above
HW13 Review: Array Maximum
Given an array of integers, print the maximum value.
First, what is our algorithm?
> Click or hit Control-Enter to run the code above
Array Maximum as a Function
Let’s turn the previous code into a function.
-
What should we call the function?
-
What is the return value?
-
What arguments does it take, and what should we call them?
> Click or hit Control-Enter to run the code above
Questions?
Announcements
-
MP Checkpoint 0 (MP0) is out! Please get started! Note that there are 10 points on MP0 for earning 40 points by 8PM on your deadline day this coming weekend (Sunday or Monday).
-
Full 12–8PM office hours start today and continue through the end of the semester.
-
My office hours today will be from 4–5PM only.
-
Daily homework continues today and is now due the day it is assigned.
-
Quiz 2 will cover everything up through today, including some multiple-choice questions on functions.