Procedure vs. Function vs. Method vs. ?

July 19, 2014 —John Koster

The field of programming and computer science is awash in technical terms with definitions for those terms that are only slightly different from each other. A good example of terms like this are procedure, function, method and the anonymous function.

With the exception of the anonymous function all of these terms are, for the most part, used interchangeably. However, they do have slightly different definitions and ultimately mean different things.

Before we get into what makes each term different, we should talk about what each of these things are actually doing, even if they do in a different way. Each of the above terms in programming is essentially a programming fragment, or a piece of a program that exists on its own. Let's look at this example:

1#include <stdio.h>
2 
3 
4int main()
5{
6 
7 float base = 10;
8 float height = 20;
9 
10 float area = ((base * height) / 2);
11 
12 printf( "The area of the triangle is %.2f\n", area );
13 
14}

The above piece of code is a simple C function that calculates the area of a triangle and returns the following output:

1The area of the triangle is 100.00

As it sits, the above code is fairly manageable. But what happens when we need to calculate the area of many triangles? We could do something like this:

1#include <stdio.h>
2 
3 
4int main()
5{
6 
7 // Triangle one.
8 float base = 10;
9 float height = 20;
10 
11 float area = ((base * height) / 2);
12 
13 // Triangle two.
14 float base2 = 5;
15 float height2 = 120;
16 
17 float area2 = ((base2 * height2) / 2);
18 
19 // ...potentially many other triangles...
20 
21 printf( "The area of the triangle is %.2f\n", area );
22 printf( "The area of the triangle2 is %.2f\n", area2 );
23 
24}

In all honesty, even that is slightly manageable. Let's say you work for a company that just calculates the areas of triangles. And you need to do it a lot in your program for some reason - the boss is addicted to triangles? Anyways, if you continued to calculate the area of a bunch of triangles like this, your team members will most likely hurt you. This is why we use functions - a kind of compiler/interpreter copy and paste trick:

1#include <stdio.h>
2 
3float triangleArea(float base, float height)
4{
5 // Calculate the area of a triangle and return it.
6 return ((base * height) / 2);
7}
8 
9int main()
10{
11 printf( "The area of the triangle is %.2f\n", triangleArea(10, 20) );
12 printf( "The area of the triangle2 is %.2f\n", triangleArea(5, 120) );
13 
14}

The above code is significantly shorter, and it accomplishes the same exact thing. Most importantly - your co-workers probably won't hurt you! And if you ever need to change how the area of a triangle is calculated (maybe Heron of ALexandria had it wrong?) you can do it one spot and not have to worry about if you updated every possible place that you are calculating the area of a triangle! Win!

Now, this example is using the area of triangles. A fairly trivial thing. The exact function doesn't matter; its the fact that we can use functions and function-like things to make code more maintainable and easier to read.

Now, on to the rant!

#Functions

Let's start with functions, because that will flow better with the previous examples. When we wrote our triangleArea function, that really was a function. Functions return a value to whatever code that called it. In our above example, some code in the main() function calls our triangleArea function and gets handed back some floating point result.

Summary: Function's return values.

#Procedures

Procedures are sort of like functions, in the fact that they are sort of a language copy and paste feature. The defining difference is that a procedure does not return a value. You can thing of it kind of like this: a function determines how something is done, and a procedure determines when something is done (since we are not returning a value).

Let's look at this example in C:

1#include <stdio.h>
2 
3 
4void sayHello()
5{
6 printf("Hello\n");
7}
8 
9int main()
10{
11 sayHello();
12}

It is fairly simple. We just wrote a procedure that says Hello and then terminates. Nothing to major here, and we even define it like a function! Confusing!

Summary: Procedures are like functions, but do not return a value, or alternatively, functions are procedures that define some process and return a value. Not a whole lof difference these days, really.

#Methods

Now we are on methods. And what an a mess we are about to get in. The term method falls out of object-oriented programming, of which we won't go into depth here. For now, an object is a collection of things that are related, or a program within a program, or a function on steroids.

Let's look at this C++ code sample:

1#include <iostream>
2using namespace std;
3 
4class Triangle {
5 
6 float base, height;
7 
8public:
9 void setBase(float);
10 void setHeight(float);
11 
12 float area()
13 {
14 return ((base * height) / 2);
15 }
16 
17};
18 
19void Triangle::setBase(float b)
20{
21 base = b;
22}
23 
24void Triangle::setHeight(float h)
25{
26 height = h;
27}
28 
29int main()
30{
31 
32 Triangle tria;
33 tria.setBase(10);
34 tria.setHeight(20);
35 
36 cout << "Area of triangle: " << tria.area();
37 return 0;
38}

Wow, that's a lengthy bit of code! What we are doing with it though is creating a class called Triangle. The triangle class appears to have three functions: setBase(float b), setHeight(float h) and area(). But wait! We are talking about a class here.

Looking at this part of the code we can notice a few things:

1int main()
2{
3 
4 Triangle tria;
5 tria.setBase(10);
6 tria.setHeight(20);
7 
8 cout << "Area of triangle: " << tria.area();
9 return 0;
10}

First thing, we are working with the Triangle class, which we are storing with the variable name tria. We call the setBase(float b), setHeight(float h) and area() on the variable tria. These are method calls. When we are concerned with object-oriented programming, methods are functions and procedures inside classes.

Summary: Methods are functions and procedures that belong to classes and instances of those classes.

#Anonymous Functions

Again, we might be in a world of hurt here. Anonymous functions are a kind of different breed of functions. Let's look at an example in JavaScript (ECMAScript for you technical type):

1(function() {
2 console.log('Hello, world!');
3})();

The above code runs in the context of the web browser (so we can use the console features, and what not. But it doesn't have to). When this code is ran, it will write "Hello, world!" to the console. But we didn't explicitly call a function here. This a function without a name, an anonymous function.

We could have written it like this if we wanted to do it without an anonymous function:

1function helloWorld() {
2 console.log('Hello, world!');
3}
4 
5helloWorld();

Both code samples accomplish the same exact thing. In the first one we don't define a name for the function, and we don't explicitly call it. In the second, we gave it a name helloWorld() and we had to explicitly call the function to get it to print Hello, world! to the console.

Summary: Anonymous functions are simply functions or procedures that do not have a name.

#Conclusion

If you have stuck around this long, congratulations! Someone out there still as a lengthy attention span! As a reward, you now know the very subtle differences between functions, procedures, methods and anonymous functions. What you do with your new knowledge is up to you, but I wouldn't make a point of correcting everyone that happens to use them interchangeably - after all, the differences are small.

The only exception to correcting people I would have to say is if they are writing a compiler, interpreter or crafting a programming language. If someone is doing those things then they really should know the difference, no matter how academic it may seem.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.