The difference between functions and methods
steverydz.com
The difference between functions and methods
1–7 of 7 posts
Re: The difference between functions and methods
#2> A method is a property on an object that behaves as a function
That's not the tl;dr, that's literally the entire "blog post". He's right though :)
Re: The difference between functions and methods
#3> A function is an encapsulated block of code > A method is a property on an object that behaves as a function That's not the tl;dr, that's literally the entire "blog post". He's right though :)
Not in the world at large. There’s considerable variation in what different programming languages/paradigms call ‘code units’ with varying properties.
For example, pascal has functions that cannot change the values of their arguments, and procedures that cannot return values. Both would be called functions in C and JavaScript.
Common Lisp has defun to define functions because it is a lisp, but introduces defmethod to make something that’s highly similar, except [1] that it can be called when code calls a generic function defined by defgeneric (https://gigamonkeys.com/book/object-reorientation-generic-fu...; https://opendylan.org/intro-dylan/methods-generic-functions.... may be easier to understand because it, IMO, is better written and because, for many, Dylan’s pascal syntax is easier to understand)
Also, functional language purists will argue that functions must be pure (https://en.wikipedia.org/wiki/Pure_function), as they are in mathematics.
[1] I probably am ignoring/forgetting details, possibly even essential ones, here.
Re: The difference between functions and methods
#4> A function is an encapsulated block of code > A method is a property on an object that behaves as a function That's not the tl;dr, that's literally the entire "blog post". He's right though :)
> He's right though :) Not in the world at large. There’s considerable variation in what different programming languages/paradigms call ‘code units’ with varying properties. For example, pascal has functions that cannot change the values of their arguments, and procedures that cannot return values. Both would be called functions in C and JavaScript. Common Lisp has defun to define functions because it is a lisp, but…
'pure' macros -> no state, everything registers as global.
functions -> stack state, everything's local. non-locals get what ever maxwell's deamon decides to provide.
methods -> heap state, methods to madness of functional local globals. at runtime, allows for everything to be routine or not.
Re: The difference between functions and methods
#5Earlier quoted context omitted.
> He's right though :) Not in the world at large. There’s considerable variation in what different programming languages/paradigms call ‘code units’ with varying properties. For example, pascal has functions that cannot change the values of their arguments, and procedures that cannot return values. Both would be called functions in C and JavaScript. Common Lisp has defun to define functions because it is a lisp, but…
from a assembly language structure point of view: 'pure' macros -> no state, everything registers as global. functions -> stack state, everything's local. non-locals get what ever maxwell's deamon decides to provide. methods -> heap state, methods to madness of functional local globals. at runtime, allows for everything to be routine or not.
- Macros: arguments are interpreted as syntax, produces syntax, typically runs at compile-time. Their use case is anything functions can't do, e.g. define things or interpret syntax "abnormally" (different than parent language)
- Function: arguments are interpreted "normally" as values, outputs a value, usually runs at runtime.
- Method: function where one of the inputs is denoted as "self". Semantically there's no difference between `a.f(...)` and `f(a, ...)`, and some languages let you do both.
- Fexprs (https://en.wikipedia.org/wiki/Fexpr): weird macro/function hybrids, arguments are interpreted as syntax, but it produces a value and runs at runtime. I only know they exist in LISP and R, and only know how they work in R: certain operations (like assigning to a variable) evaluates the argument, the argument won't be evaluated if execution never reaches these operations (like if the assignment is in an `if` which doesn't get triggered; in other words, arguments are lazy), and special functions like `substitute` will extract the argument's syntax (e.g. if `⟦f⟧ = function(a) subsitute(a)`, `⟦f(foo(bar))⟧ = `; now if ⟦g⟧ = function(a) f(a)`, `⟦g(foo(bar))⟧ = `). Personally I don't like these, and prefer separate macros and functions, which I believe can accomplish anything fexprs can with less confusing semantics and accidental bugs. But they're an interesting concept I just listed here because they're like a function or macro, but neither.
Re: The difference between functions and methods
#6Earlier quoted context omitted.
from a assembly language structure point of view: 'pure' macros -> no state, everything registers as global. functions -> stack state, everything's local. non-locals get what ever maxwell's deamon decides to provide. methods -> heap state, methods to madness of functional local globals. at runtime, allows for everything to be routine or not.
My definitions: - Macros: arguments are interpreted as syntax, produces syntax, typically runs at compile-time. Their use case is anything functions can't do, e.g. define things or interpret syntax "abnormally" (different than parent language) - Function: arguments are interpreted "normally" as values, outputs a value, usually runs at runtime. - Method: function where one of the inputs is denoted as "self". Semantica…