4

Ive just started year three of my computing degree and have been looking through the course material. My application development module is based around c#. Throughout the material the lecturer refers to what I know to be called 'methods' as 'functions'. I am aware that the term 'functions' is used (for example) c++ for as code block but I thought that OOP used 'method' to distinguish between the two different types of programming.

So, are the two interchangeable or should the lecturer be using the term 'methods'?

Dylan Jackson
  • 801
  • 1
  • 17
  • 33
  • AFAIK the term function is valid for scripted-languages and methods are indeed used in the OOP-languages. I don't have no source for it though. – Daan Timmer Oct 06 '11 at 10:12
  • 2
    Maybe he refers to methods that return value as "functions"? – Cipi Oct 06 '11 at 10:13
  • 3
    check out this link :) http://stackoverflow.com/questions/155609/what-is-the-difference-between-a-method-and-a-function – user979014 Oct 06 '11 at 10:15

2 Answers2

8

The C# 3.0 spec says

1.3

A class type defines a data structure that contains data members (fields) and function members (methods, properties, and others).

1.6.7

Members that contain executable code are collectively known as the function members of a class. The preceding section describes methods, which are the primary kind of function members. This section describes the other kinds of function members supported by C#: constructors, properties, indexers, events, operators, and destructors.

And of course "anonymous functions" are called by exacly that term. The phrase "function member or anonymous function" occurs a lot in the spec, there apparently being no accurate shorter way to say "lump of executable code".

Community
  • 1
  • 1
AakashM
  • 62,551
  • 17
  • 151
  • 186
2

Well, by definition a function is something that returns a value and should not have any side effects. Functional programming takes this paradigm to the extreme.

In contrast an operation or sub routine may have no return value and instead have some side effect. Imperative programming relies on things like this. The only language I worked with that actively forced you to declare this was Visual Basic (sub and function). In C# you just declare void as return type if you do not have a function in the classic sense.

The term method was AFAIK coined to distinguish object oriented programming from other styles. A function which is part (member) of an object would be a method then.

I hope that helps.

Christoph Grimmer
  • 4,210
  • 4
  • 40
  • 64
  • +1 The *no side effects* part is crucial. That is what a function makes a Function per definition, as opposed to a member that makes a class function (verb). – Gert Arnold Oct 06 '11 at 12:13