Erik de Castro Lopo wrote:
Hi Mark,

Mark Sargent wrote:

Hi All,

last night we touched on methods(functions equivalent for other languages?)

Sorry, but no, functions are not really equivalent to methods.

From:

    http://en.wikipedia.org/wiki/Function

    "A subroutine which has no side-effects; see functional programming."

By "has no side-effects" they mean that a function can have zero or
more paramenters and return a result that depends on nothing other than the input parameters. In addition, the function does not change
the state of any of its input parameters or of any other data outside
the function. This is what is sometimes called a pure functions.
A good example of a pure function is the sine function (usually abbreviated to "sin") in languages like C, Ocaml, Python etc. The sine of any number X depends on the value of X and nothing else.
Many languages also have impure functions, functions which change the
state of data outside their scope. A good example is the puts function
you have been using in Ruby. When you call puts, it writes the data
to stdout, thereby changing the state of stdout.

A method OTOH is an OO term, where a method is defined as part of the definition of a class and is (usually) designed to operate on
objects that are instances of that class (or releated classes via
inheritance etc). Methods, when acting on a object often change
the state of the object they were called with.

Anyway, here's the wikipedia [0] entry:

    http://en.wikipedia.org/wiki/Method_%28computer_science%29

Just one more note, programming languages like Java sometimes confuse the issue. For instance, in Java, which has no functions,
only methods, the sine function is part of class Math and is defined
as a static method, that is, a method which does not operate on an
object. I'm not sure if Ruby has the same concept.

Erik

[0] Wikipeida is a really good first port of call programing related
    information. If you find anything there that you don't understand
    feel free to ask questions here.

Ruby Programming Documentation has these to say about Methods.

http://www.ruby-doc.org/docs/ruby-doc-bundle/ProgrammingRuby/book/tut_methods.html

with a very simple definition, namely, '...expression that returns a value'. This definition
has to be understood in the context of the entire section, of course.

Anyway, I found the foregoing section enligthening.

O Plameras




O Plameras
_______________________________________________
coders mailing list
coders@slug.org.au
http://lists.slug.org.au/listinfo/coders

Reply via email to