IntroCS Chapter 2.1 Functions
San Skulrattanakulchai
October 10, 2016
Functions & Procedures (static methods)
- Functions & procedures are an abstraction of computational process.
- A function returns some value (can be of primitive or reference type). It correspond to the mathematical concept of functions. E.g.,
Math.max
, Math.sin
.
- A procedure returns no value but causes some useful side effects, e.g.
System.out.println
.
- One purpose of a class is as a module for collecting together functions, procedures, and constants. E.g., the
Math
class.
- Naming convention. Functions and procedures are conventionally named exactly like variables. Constants are named in all caps, with underscores separating the individual words.
Functions (continued)
- We have used functions, procedures, and constants from various classes written by other people, e.g.
StdIn
, StdDraw
, StdAudio
. We’ll now learn how to write such functions, procedures, and constants ourselves.
- Functions & procedures as control flow construct.
- syntax for writting functions
- parameter list
- arguments
- parameter passing by value
- two kinds of return statement
- overloaded functions