Functions and Methods

We would have used the terms functions and methods in programming. I have seen some developers use these terms in a right way and some use it in random. In this post, I’ll be discussing about this difference.

In a nutshell, both are the same. The only difference is, in an Object Oriented Programming environment, we call functions as methods.

Why? lets dig a bit deeper.

Functions

  • Functions are mathematical constructs.
  • A function exists individually, probably outside a class. It doesn’t depend on a class for it’s existence.
  • It is self defining.
  • Data required for a function execution are passed as parameters.
  • It may or may not return a value.
  • Functions are called by their name.

One of my previous post about stateless programs would bring in more context here as I have explained the stateless model using functions. There are more about functions which I’ll be discussing in these posts about programming paradigms and functional programming (content work in progress).

Methods

  • Methods are same as functions with the only difference being it is inside a class.
  • Functions are called as method in Object Oriented Programming
  • Like functions, methods are also called by their name.
  • Unlike function, a method can be called using a reference variable. Since it is present inside a class, we should use the class name (in case of static method) or class’s instance object to access a method.
  • In C++, methods are called as member functions.
  • They can manipulate instance variable of the class it is in.
  • They can have side effects.

The examples

Below is an example for methods.

public class MethodsExample {
    public static void main(String[] args) {
        final MathUtil mathUtil = new MathUtil();
        final int sum = mathUtil.add(1, 2);
        System.out.println("Sum: " + sum);
    }
}

class MathUtil {
    int add(int firstValue, int secondValue) {
        return firstValue + secondValue;
    }
}

What have I done here?

  • I have created two classes MethodExample and MathUtil.
  • MathUtil has a method add that will return the sum of two given values.
  • In the main method, I have created an instance for MathUtil class.
  • In the next line, I call the instance method add passing two inputs 1 and 2. I assign the return value to a variable sum.
  • In the next line, I print the variable sum.

As you can see, the call to add method is tied with the instance variable mathUtil. Without this instance variable, add method cannot be called. Wherever I need to call the method add, I have to get or create an instance of MathUtil class.

Below is an example for function.

const add = (a, b) => a + b;
console.log(`1 + 2 = ${add(1, 2)}`);
console.log(`3 + 4 = ${add(3, 4)}`);

What have I done here?

  • I defined a function add that would get two variables a and b as inputs. This function will return their sum as output.
  • I called the method add twice in two different console.log. In one call, I have passed 1 and 2 to print 3. In the other call, I have passed 3 and 4 to print 7.

In the above example, we did not require a class or instance variable to access the add function. We defined what the function add should do once and we called it independently wherever required. All the data required by the function add is passed as inputs and the output is obtained. This is a stateless model and also doesn’t have side effects.

I hope this post was informative. If this was helpful, please share it with your friends and support us 😁

error

Enjoy this blog? Please spread the word :)