Skip to main content

Section 2.1 Variables and Functions

First of all let’s clarify a couple of terms that are fairly universal across almost all programming languages, and I assume you are familiar with already. In pretty much every program out there you will find these two elements:
  • Variables, which store values, and have a certain visibility to them, i.e. some parts of our code base can "see" them and others cannot. We typically think of these variables as representing a specially designated place in memory that contains the information, or points to the information. Almost every programming endeavor consists of doing something to or with the values stored in the variables.
  • Functions, which are pieces of code that take some inputs, in the form of parameters, and typically produce some output, in the form of a return value. They may also do other things along the way, often called side-effects. In Java we use a special kind of functions, namely functions associated to a class or object. we will be using the term method for these functions. By far the most important property that these functions/methods have is that they can be called from a very far away place in the code, in which case the flow of control of the program will go to the code in the function before returning to the function’s caller. Functions are a long-distance flow of control mechanism.
  • Functions typically use two kinds of variables to get their work done. The first is parameters. These are specified in parentheses next to the function name. Whoever calls our function is responsible for providing values for these parameters. These values are typically called the function call arguments. We also typically encounter local variables, which are declared within the body of our function and are initialized by the function itself. They are there to help it do its job, and they go away when the function returns.

Checkpoint 2.1.1.