user2421111
user2421111

Reputation: 109

Java: What is the difference between subroutine and methods?

What is the difference?

public write(){
// dostuff
}

Is the code above a subroutine, a method, or a program block?

I don't understand these terms because the meaning of them are all so similar?

So, if I was being very strict and pedantic, then which one would it be? I prefer calling them subroutines, is this STRICTLY correct?

Programming language: JAVA

EDIT: Thanks, I understand now but what would be the safest way to call it? Block? I use multiple programming languages, would the term "block" suffice?

Can I call a function a block?

So can I refer everything as a block? It will be easier for me, right?

I won't call a block a function or subroutine, but I will call a function, methods and whatever as a block. Would that be fine, calling them as a block?

So, does this mean this is somewhat ACCURATE? A local variable is not accessed outside of the program block (yes, there are a few circumstances that this is not true).

Upvotes: 4

Views: 21631

Answers (4)

Bill K
Bill K

Reputation: 62789

They are just terms to identify different types of callable structures. Subroutine was not really a term that was "Ported" to Java, in fact it's rarely used any more. In older languages it was often used to say "execute a reusable piece of code and return here". This is a bit general, it doesn't specify where parameters are passed or returned. This term is mostly used in systems with shared memory (public variables in Basic, system memory in assembly, ...), but I think it can really apply to any reusable code.

A more specific term that is often used is "Function". Technically a (pure) function must always return the same result for a given set of parameters. This was good for non-oo languages as a replacement for "Subroutine", it is a clearer term than subroutine because it specifies how values are passed and implies you aren't just accessing arbitrary memory, technically it's probably still a subroutine too.

The term Function isn't always accurate for OO languages though--In Object Oriented Languages we add in the idea of "Objects" to encapsulate data and do work on that data for you. When you call a method like atomicInteger.incrementAndGet(), it might (will!) return a different value each time by mutating something inside the object. This no longer fits the definition of a Function (Returns different results for the same parameters), so they call it a method. It could still fall under the general term "Subroutine", but it would be confusing and less specific to do so in OO languages, so we just don't.

So Java can have both Functions and Methods. Every routine on "String" is a function because none of them modify the state of the underlying object (and therefore a given function called on a string always returns the same value. A Method can modify the state of the object.

These differences become pretty important when you are trying to make objects safe for multi-threading and sharing--You shouldn't call a method from multiple threads at once, but functions are fine.. Note that this makes any use of String safe for threading use because it's all Functions (An object that only has functions is also called Immutable).

Upvotes: 0

user207421
user207421

Reputation: 310980

  1. A subroutine is something you can call that returns to you.
  2. A function is a subroutine that returns a a value.
  3. A method is a subroutine or function that you can call on an object in an OO language.

Upvotes: 5

Daniel Conde Marin
Daniel Conde Marin

Reputation: 7742

In the first place, what you have there is a constructor, and not a method, a proper method returning void(nothing) would be one like this(according to C# or Java syntax):

public void write()
{}

Now, regarding to what you initially asked: What's the difference between subroutine and method?, Well, there is basically none. But if you want to go deeper, then we'll have to go to assembly programming IMO. Subroutines in assembly, are in fact different from methods in two things:

  1. They don't get parameters passed
  2. They don't return anything

And, you probably are asking yourself: Then how do I process anything at all, how this subroutine works? In assembly, here is where registers do their role, instead of passing a parameter to your subroutine, like you do with methods in every high level programming language, you have to save the value(parameter) you are going process before calling the subroutine, making sure that this value won't get affected before you reach the subroutine. In the same fashion, you'll keep the resulting value in another register for using it later. There are other ways to do this in assembly, commonly used too, by using the stack pushing and popping values from it, but I think I made my point clear enough. If serves for anything at all, I'll post you a piece o code in assembly using a simple subroutine to add two numbers:

org 00H

mov R3,#10                 ;save the first parameter
mov R4,#20                 ;save the second parameter

call Sum                   ;execute the subroutine

mov A,R3                   ;mov 30 to accumulator

Sum:                       ;subroutine declaration
add R3,R4
ret

end

Upvotes: 1

duffymo
duffymo

Reputation: 308938

None of the above, because it doesn't have a return value. It's only "correct" if it's a no-argument constructor for a class named write, in a file named write.java. And even that violates the Sun Java coding standards.

Subroutine is a term from functional/procedural languages like FORTRAN and COBOL. Those languages keep data and methods separate, with methods operating on the data they are given.

Objects encapsulate data and methods into instances. Most object-oriented programmers would prefer method. Your "preference" for subroutine will mark you as quaint and out of step.

I don't hear "subroutine" much anymore. I'd get used to "method" if I were you.

Upvotes: 3

Related Questions