Functions
- statements, variables, loops and branching are all primitive.
- What you spend most of your time doing is writing functions.
- They use the primitives and call other functions.
- You can call them functions, methods, or procedures.
public int sum(int i, int j) {
int result = i + j;
return result;
}
- You need to name the function (sum).
- It has a parameter list (which may be empty). In this case it's i and
j.
- If you want a return value, you need to say the type (in this case int).
- If you don't want a return value, the type is void.
- If you have a return value, you need to return on all paths through
the function. (That's the return statement. Hey another type of
statement).
- Functions should be relatively small. They should fit on one
page or less.