Functions
> Click or hit Control-Enter to run the code above
What Are Computers Good At?
-
Basic math
-
Simple decision making
-
Doing things over and over again very, very fast
-
And storing data
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 are built of 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
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…
> Click or hit Control-Enter to run the code above
> Click or hit Control-Enter to run the code above
Functions Frequently Implement Algorithms
Announcements
-
MP1 is out and due on Friday. Please get started! Today is the best day to come to office hours.
-
EMP (Even More Practice) continues tonight from 5–7PM in Siebel 1404. This is where it will be held for the rest of the semester. If you struggled on MP0, Quiz 1, or TC 1 or TC2, be there.
-
The next set of Turing’s Craft exercises (TC3) are due tomorrow at midnight.