By: Erik D. Gross, Co-Founder of The Tech Academy
There is a set of terms that can be confusing in learning computer programming. These terms all relate to a fundamental element of all computer programs: sub programs.
A sub program in the most basic sense is this: a set of computer instructions, separate from the main program, that can be executed on demand by the main program.
Sub programs were an early development in computer programming, and their creation was driven by one basic factor: Programmers quickly found that they were often having a program do certain exact things many times as the program was being executed. This meant that the exact same set of programming instructions had to be entered in the computer program every time that thing was needed.
This activity had two fundamental weaknesses. First, the process of typing the same exact instructions over and over again was time-consuming and error-prone for programmers.
Second, if a programmer wanted to make a change to how that thing was done, they had to find every place in the program where they had entered the duplicate instructions and make the change in every one. In other words, these commonly-used chunks of computer code were hard to create and maintain.
The answer to this problem was a sub program. Here, the programmers would type in the set of instructions that performed the needed action, and give that set of instructions a unique name. This set of instructions was called a sub program, and was not considered part of the main program that the computer was going to execute. Instead, the main program was modified so that it would just execute that sub program whenever that particular action was needed.
This is indeed a basic concept. In the early years of computer programming languages, where programmers worked with various low-level languages barely a step removed from machine language, the actual implementation of the concept stayed quite simple and very much as described above. However, as higher-level programming languages were developed, this concept began to develop variations and nuances. The central concept of pre-made sets of code that can be used as needed has remained, but the designers of the various languages have often implemented the concept in different ways, depending on the design objectives of the language.
Because of this, you will find many related terms in this area. Some of the terms in use are:
If you try to clear up the definitions of these various terms, you can quickly become confused. Some sources will tell you that they are all basically the same thing. Other sources will articulate specific differences and similarities between two or more of the terms. It can be difficult to nail down.
This is because of a few factors. First, some programming languages use the same term to mean slightly different things. Second, some programming languages are designed to implement this concept in two related ways, so the language designers needed two different terms.
What this means in terms of your study of various computer programming languages is that you’ll often have to research what a specific term means in that language, ignoring source data that’s related to other languages.
This concept is best illustrated through an example. We’ll use the popular programming language JavaScript to do this.
JavaScript uses two related terms: function and method. They are very similar, in that they both involve the creation of a pre-made set of instructions that can be made use of by other program elements. They do have some subtle but important differences, however. Let’s explore them.
First, some basic definitions - and remember, these definitions are for the use of these terms in JavaScript.
We’ll start with the definition for the term “object”, as a clear understanding of this is needed in order to understand the other two terms.
In JavaScript, an object is a type of data that represents a thing through its various properties (characteristics - what it looks like) and behavior (what it can do).
An example of an object would be a “vehicle”. Thinking of this common real-world thing as an “object” that will be stored in computer memory and kept track of as a program is executed, we can think of various properties and behavior for it. Under properties, we could have things like “chassis type”, “engine type”, “number of doors”, “speed”, etc. Under behavior, we could have things like “accelerate”, “deccelerate”, “turn right”, “turn left”, etc.
An example of creating an object in JavaScript might look like this:
var student = {
firstName: “Jane”,
lastName: “Smith”,
age: 28,
gradeAverage: 3.5
};
In this example, we have only specified properties for the “student” object, and not specified any behavior. We will look at that in a moment.
So let’s look at how this applies to methods and functions in JavaScript.
Function: In JavaScript, this is simply a block of code designed to perform a particular task. This block of code gets executed when another piece of code calls it.
An example could look like this:
function add(num1, num2) {
return num1 + num2;
}
Other JavaScript code elements could call this “add” function by specifying its name and passing it two numbers. That could look like this:
var sum = add(5, 7);
Here, the code would create the variable called “sum”, call the function “add” and pass it the two numbers 5 and 7, and take the result (12) and assign that value to the variable “sum”.
Method: In JavaScript, a method is a set of code associated with an object that is designed to change the state of that object when it executes. In other words, the method is performed on the object.
You create these methods when you create the object. Let’s look at how we might do that with our previous example of a “student” object:
var student = {
firstName: “Jane”,
lastName: “Smith”,
age: 28,
gradeAverage: function(avg) {
return avg;
}
};
Here, we aren’t setting the property “gradeAverage” to a fixed number of 3.5. Instead, we are setting that property to the value returned by a set of code. Specifically, that code will take in a number (the variable “avg”) and set the value of the “gradeAverage” property to the value of that variable.
Executing that code could look like this:
student.gradeAverage(3.4);
Here, we are telling the computer to make use of the object called “student”. Specifically, the computer is to run the function “gradeAverage” that is defined in the “student” object. Since that function needs an input (the variable “avg”), we give it the number 3.4.
So here’s the distinction: In this specific situation, that function “gradeAverage” is called a method. Yes, it’s confusing - until you recall that in JavaScript, a method is a set of code, associated with an object, that is performed on the object itself. So we have the confusing situation of a property of an object that we are defining using a function - but we’re calling the action performed by the function a method.
In actual practice, there is very little danger in using these terms interchangeably - and you’ll often find technical sources will do so. But occasionally you’ll find a source that uses the terms in their strictest sense.
The lessons here are these: First, there are many varied terms in use for the concept of “a block of code that can be executed on demand by other code”. Second, in order to clarify any potential difference between these various terms, you’ll need to investigate their exact meaning in the language you’re concerned with. Any attempt to give them a blanket, universal meaning ignores the subtleties described here.
About the author: Erik has been working in technology since the 1980s. As a nuclear reactor operator and teacher in the Navy, he mastered a multitude of technologies. His years of experience as a senior-level software developer were key to the development of The Tech Academy’s boot camps.
As the other Co-Founder of The Tech Academy, Erik assists in curriculum development and oversight.
The Tech Academy offers a wide range of services including:
Comments