Programming Primer
Posted on Sunday, March 1, 2009
Modern programming has a set of terms that every wannabe programmer must know. Master these terms first. Once you get these terms down pat, it is easier to understand the jargon of each specific programming language.
- Variable:
-
A variable is a named location in memory. We create variables to hold data that we need to access again at some later point. The compiler keeps a list of memory addresses to access these variables, but we use spoken-language-based names since most people would have a hard time memorizing hexadecimal codes.
As part of the declaration of a variable, we must define the data type and the name of the variable. Optionally, we can also define the accessibility level and the initial value (or values, in the case of arrays) of the variable.
Examples:
private int iVariable = 1;
double dVariable = 2.45643;
DIM fVariable AS float = 1.78
- Function:
-
(1) A function is a named piece of executable code.
(2) A function is a distinct chunk of code that we can call multiple times within our main program.
One of the primary benefits of function calls is that we do not have to type and retype a useful section of code.
Some languages use different terms for functions, depending on whether or not calling the function results in a value being returned to the calling program code. For instance, Visual Basic refers to "void" functions (functions that do not return a value) as subroutines. The C# and Java languages use the same concept as C/C++ (that is, that every callable piece of code is a function) but use the term "method" instead.
As part of the function definition, we must define the return type, name, and any parameters. Optionally, we can also specify the accessibility level. Additionally, C++ requires that we define the scope of the function if we are creating a class.
Examples:
public int FirstFunction( void )
double SecondFunction( int iFirstParam, single sSecondParam )
Public Sub MyVBSubroutine ( ByVal fParam1 As Float )
Public Function MyVBFunction ( ByVal fParam1 As Float ) As String
- Function call:
-
When programmers use the term "function call" what they are really saying is "I am now executing the function named func_name," where func_name would be replaced by the name of the function. To the compiler, this means that control of the flow of execution is passed from the main (or "calling") program code to the "called" function code.
- Function header:
-
A function header defines the various aspects of the function, such as the name, the scope, the return type, and any parameters necessary for proper execution of the function's code.
- Method:
-
See Function
- Subroutine:
-
See Function
- Parameter:
-
A parameter is a special type of variable used within the declaration of a function. This variable defines the type and order of values that can be "passed" to the function. A parameter only exists in the definition of the function; when we call the function, the value passed into the function is then considered to be an argument.
- Argument:
-
An argument is the actual value being passed into a function call. The data type of the argument must match the data type specified in the function header, or must be of a type that is capable of being promoted to the correct type.
- Promotion:
-
Promotion is an implicit conversion of a variable from one data type into another. Promotion only occurs if the original type is considered to be a sub-set of the type to be converted to. For instance, since the data type
double
can hold the entire scope of variables available to the data typesingle
, a variable that was defined as asingle
can be promoted to a variable of typedouble
. The opposite is NOT true, however.
- Passing data:
-
When we use the phrase "passing a variable," what we are saying is that we are calling a function with some value as part of the function call, as defined by the parameters in the function definition. There are two ways to pass data to a function: by value or by reference. When we pass a variable by value, we are actually giving the function a copy of a variable. Any code that manipulates that variable is actually manipulating the copy. If we pass a variable by reference, we are giving the function the memory location of the variable. Any code that changes the data we passed in is actually changing the original variable. Be very aware of which style you are using, since the results of passing by value and passing by reference can (and probably will) be very different.
- Accessibility level:
-
The term accessibility level defines what level of code can access a variable or function definition. The two most common levels are
public
andprivate
. Variables and functions defined as public can be accessed by any other chunk of code that can execute the main program code. Private variables and functions can only be accessed by code within the same level as they are declared. This is an important part of a paradigm known as OOP, or Object Oriented Programming. In a nutshell, we can use accessibility levels to hide variables or functions from code that we do not want to give direct access to those variables or functions.
- Compiler
-
A compiler is a specialized program that generates machine-readable (executable) code from program code.
- Scope:
-
The term "scope" refers to the valid lifetime of a variable within a program. There are (in general) only two levels of scope: global and local.
Global variables exist for the lifetime of the program itself. They are created outside of any method, and as such are accessible by every method. As a general rule, global variables should be avoided in anything but the most simple programs, with the exception of constant variables.
Local variables have a very limited existence. Depending on when a variable is created, it can be local to a class, method, or a control statement such as a for() or while() loop. When the class definition ends, when the method completes and returns control to the calling code, or when the control statement completes, any variables defined within them are said to be "out of scope." When a variable goes out of scope, most programming languages are set up to de-allocate any memory (or other resources) that variable previously controlled.