Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm wondering what are the specific differences in the terminology we use for grouping related parts of code. I've sometimes seen the terms used interchangeably: many OO languages even use the keyword "function" to define a method. (Why?)

If you wanted to be precise, what are the specific meanings of each? Or is it just whatever each language chooses to call it?

share|improve this question
    
The only two languages I'm aware of that use function for methods are JavaScript and PHP. Both use the same keyword for non-methods (free functions), and in the case of JavaScript there isn't really a distinction between functions and methods, at least not at language level. –  delnan Dec 4 '11 at 0:31
    
Examples: in VB.Net Sub is a Function without a return value. In Python you can have a function that is not part of any class, so it is not a method. You can also declare functions within functions, and you can do that in C# as well. Sometimes they have different names for the same reason that apples and pineapples do: because they are different. –  Job Dec 4 '11 at 0:34
add comment

2 Answers

up vote 13 down vote accepted
  • subprogram, subroutine "subroutine" probably comes from assembly language. Some processors include instructions to support subroutines as a way of organizing code and reusing common sections of code. For example, the 6502 processor had JSR (jump to subroutine) and RTS (return from subroutine) instructions. I remember it also being used a lot in structured programming, in which a program is a hierarchy of units of code which were sometimes called subroutines or subprograms. IMO, these are the most generic terms for some unit of code to which control is temporarily transferred for the purpose of completing a given task.

  • function, procedure These are often used interchangeably, but in some languages there's a distinction. In Pascal, a function is a subprogram that returns a value, while a procedure is a subprogram that doesn't. In C and related languages, every subprogram has a return type (even if it's void), so there's no distinction.

  • method, member function These are two names for the same thing -- essentially a function that's associated with a given class or object.

  • operator Every language has a set of built-in operators. In some languages, such as C++, operators are functions that can be overridden (i.e. replaced) and/or overloaded (i.e. defined for new types).

  • anonymous function As the name indicates, this refers to a function without a name. Anonymous functions are essentially blocks of code that can be assigned to variables or passed as parameters for subsequent use, for example as a completion routine.

  • closure, lambda expression, block A closure is a chunk of code that's bound to a set of variables. I think of closures as anonymous functions plus context.

share|improve this answer
    
Not all languages come with built-in operators. Some, like Scheme and Haskell, just let you define functions with names like '+' or '>>='. Also, you should probably add that in some languages (like Scheme), "function" implies referential transparency (e.g. it always returns the same value for the same input with no side-effects) while procedure doesn't. Otherwise, great answer. –  Tikhon Jelvis Dec 4 '11 at 21:14
add comment

It depends on the languages, what you can do with them

  • do you need a distinction
  • is there a distinction in the syntax, or is it just convenience, when talking about code

AFAIK, in Basic, if you call something, which does not return anything, but performs a job with side effects, like printing, write to file or change a global variable, it was called a procedure.

If it returns something, it was called a function.

In an OOP-Language like Java I rarely met the term 'function', but 'method', and it was teached that functions are things which are global and can be called by everyone.

In the functional and OOP-language scala, a method isn't called function, but you can pass such a thing to another method, and then it becomes a function.

A subroutine is a routine, which is called by another routine, to my understanding.

In summary, I don't think there is a concise definition over all languages and more so paradigms. You have to consider the specific culture, you're in, if you use these terms - maybe you should introduce your definition first.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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