3
$\begingroup$

I don't know if I should have posted this on StackOverflow or here but what's the difference between these terms? Are the definitions of these dependent on the programming languages or they're things independent of them?

$\endgroup$

1 Answer 1

3
$\begingroup$

Precise definitions are dependent on programming languages; if you are interested in a particular programming language, refer to the documentation of that language rather than to this answer.

Function, pure function, and procedure

A function is usually expected to return an output; a procedure, on the other hand, is expected to perform an operation with side-effects. For instance, a print function which displays its argument to screen is a procedure; a multiply_by_two function which returns the double of its argument is a function.

Following this distinction, a function usually has only read access to its parameters, whereas a procedure might have write access to its parameters. For instance, a function sort might take a list, and return a new sorted list; whereas a procedure sort might take a list, and modify that list in-place without returning a new list.

Note that the distinction between function and procedure is not necessarily clear-cut; a function might return a result and have side-effects. A function which doesn't have any side-effects is usually called a pure function. Functional programming languages like OCaml and Haskell make a huge deal of the distinction between functions and pure functions.

Most programming languages today use the word "function" indifferently and don't use the word "procedure" at all.

Function and method

In object-oriented programming, a method is a function defined inside a class; every object which is an instance of the class will have the method. Usually the method is called using the name of the object, followed by a dot ., followed by the name of the method. For instance, in python, the class List has a method sort so that if lst is an object of class List, you can write lst.sort() to sort lst.

The object is not passed as an explicit "argument" of the method, but the method implicitly has access to the object as if it were an argument. Languages often use a reserved keyword to designate this implicit argument in the code of the method; in C++ and Java, the keyword is this. In python, there is no reserved keyword, although self is almost always used. If you are programming in a non-object oriented language and want to simulate object-oriented behaviour, you can emulate methods by writing functions whose first argument is meant to be "the object".

Class method and instance method

Sometimes people define methods that don't actually need access to an object. Those are not as common; if they don't need access to an object, why define them as method rather than function? but they do exist. Depending on the language, those methods might be referred to as "static methods" or "class methods". Instead of calling them as <name of the object>.<name of the method>, you can call them as <name of the class>.<name of the method>. For instance, in the python library "pandas", the class DataFrame has a class method from_csv which can be used to create a new dataframe by reading a csv file. This class method is called as DataFrame.from_csv(filename), which you can contrast with the method sort from class list which is not called as list.sort(), but rather as x.sort() where x is a particular list.

Routine and subroutine

Those words have mostly disappeared in modern languages. They were used in older programming languages. See also this related question:

Subprogram

A subprogram is a part of a program that could be consider a program on its own. I don't think I've ever encountered a formal use of this word, so I won't attempt to give a formal definition.

Block

In flow-oriented "graphical programming" tools, subprograms are often called blocks. The reason is that they are represented by a little box on the screen, with incoming arrows for the input, and outgoing arrows for the output. Representation of a program using block is very useful in fields where non-programmers need to assemble a pipeline using existing subprograms. For instance, biologists who need to do image analysis, and use subprograms such as "denoise the image", "distinguish between tissue and background", "count number of cells in image", "measure area of tissue in image", etc. Now all they need to do is choose the blocks they want and plug the blocks together, without writing any code at all.

Task

The word "task" is mostly used in the context of scheduling, where it refers to an element with an identifier, a length of time, and some requirements such as a deadline or the use of a particular resource. In computing, scheduling happens all the time; for instance, the operating system of your computer must schedule a lot of tasks so that they complete smoothly. The precise meaning of "task" in the context of computing is not particularly precise, but is closer to words like "thread" or "process" than "function" or "procedure".

$\endgroup$
7
  • $\begingroup$ Very well. The language I'm interested in Is python. $\endgroup$
    – Emad
    Jul 6, 2021 at 11:15
  • $\begingroup$ @Emad Then the only two words you'll encounter will be "function" and "method"; "function" is the most general term, and a "method" is a function defined inside a class. $\endgroup$
    – Stef
    Jul 6, 2021 at 11:58
  • $\begingroup$ How about class method and instance method? How are they different? $\endgroup$
    – Emad
    Jul 6, 2021 at 12:11
  • $\begingroup$ @Emad Sometimes people define methods that don't actually need access to an object. Those are not as common (if they don't need access to an object, why define them as method rather than function?) but do exist. Depending on the language, those methods might be called "static methods" or "class methods". Instead of calling them as <name of the object>.<name of the method>, you can call them as <name of the class>.<name of the method>. For instance, in the python library "pandas", the class DataFrame has a class method `from_csv' which can be used to create a new dataframe ... $\endgroup$
    – Stef
    Jul 6, 2021 at 13:41
  • $\begingroup$ ... by reading a csv file. This class method is called as DataFrame.from_csv(filename), which you can contrast with the method sort from class list which is not called as list.sort(), but rather as l.sort() where l is a particular list. $\endgroup$
    – Stef
    Jul 6, 2021 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.