Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do you call them functions, procedures or methods?

Tags:

oop

theory

consider a standard c# 'function'

public void foo() { //some code }

In c or c++ this is called a 'function' - even if taking no parameters and returning no value. In another language maybe it would be a 'procedure'. In object orientation speak it would be called a 'method' if a class member. What would be the correct term to use in c#?

like image 442
lowlyintern Avatar asked May 07 '10 07:05

lowlyintern


People also ask

Are functions called methods?

A method, like a function, is a set of instructions that perform a task. The difference is that a method is associated with an object, while a function is not.

Is a method a function or a procedure?

Function and method are two commonly confused words. While every method is a function, not every function is a method. There is also another word that is erroneously used interchangeably: a procedure.

What is procedure method and function?

A procedure is a function that doesn't return a value. In particular, this means that a procedure can only cause side effects. (That might include mutating an input parameter!) A method is a function that closes over a set of variables, that is, a closure.

Why are functions called methods?

Java chose to call them "methods" because they fit the already-existing meaning of that word. Had they called them "functions" they would be introduced confusion because that word already has a different meaning.


3 Answers

  • Method : function of a class.
  • Function : function out of a class, possible only in full-object languages (like C++, C, etc.)
  • Procedure : function that return nothing/void. I personnaly don't like this word, I'd rather use 'function'

Make your choice =)

Edit : Just to be more precise, Method, function and procedure are OOP words to describe a subroutine. Some languages have their own vocabulary such as "predicate" in Prolog, or "constructor" in C++/C#/Java, or even "property" in C#.

like image 77
Clement Herreman Avatar answered Sep 28 '22 00:09

Clement Herreman


Just to confuse the issue futher: (from C# Language Specification)

7.4 Function members
Function members are members that contain executable statements. Function members are always members of types and cannot be members of namespaces. C# defines the following categories of function members:
* Methods
* Properties
* Events
* Indexers
* User-defined operators
* Instance constructors
* Static constructors
* Destructors

And

10. Classes
A class is a data structure that may contain data members (constants and fields), function members (methods, properties, events, indexers, operators, instance constructors, destructors and static constructors), and nested types. Class types support inheritance, a mechanism whereby a derived class can extend and specialize a base class.

So "function member" or "method" would be correct for C#.

like image 20
Cameron MacFarland Avatar answered Sep 28 '22 00:09

Cameron MacFarland


Method is OOP abstraction term. They describe behaviour(a verb) of an object. They are equivalent to some of the procedural programming's functions and procedures. (the properties etc are also function and procedures).

So, function is a program within program that returns some values. Procedure is a program within program that does something. Methods, Properties etc, etc are a next level of abstraction(used in OOP). They are wrapped around functions and procedures.

like image 44
Mike Avatar answered Sep 28 '22 01:09

Mike