World's most popular travel blog for travel bloggers.

[Solved]: What is the difference between operator and function?

, , No Comments
Problem Detail: 

We have operator and operands, function and formal arguments. Is the difference purely lexical (we use alphanumerics for funciton identifiers but identify operators with special characters, e.g. "+" and ">=") or it is syntactical, like I guessed here

f(a,b) -- prefix notation used for functions a.f(b) -- infix notation for method invocation (used in OOP) a f b  -- infix notation without dot and parenthesis used in operator invocations 

so that when we write a + b we have an operator but "+"(a, b) (both ways are acceptable in VHDL) makes it a function? Do tools treat them differently? Can you differentiate them? Can you say that this is not a function, it is an operator or vice-verse? When I read the definitions in wikipedia, I do not see that syntax is a distinguishing feature of a concept. So, is it right that operators and functions are synonyms, two different words for the same thing, same meaning, same notion? I have such guess but have never seen it stated explicitly. Please agree with my guess or say what is the difference.

Asked By : Val

Answered By : Raphael

Historically, my guess is that the difference is rooted in how things were (and still are) translated into machine code.

An operator such as addition of integers or exclusive-or of booleans would be translated into one (or few) assembler commands which would be directly executed by the processor (resp. its ALU). It would also be part of the syntax.

A function (or procedure, method), on the other hand, is a subprogram the (main) program would explicitly jump to during execution. That is, a new stack frame is allocated, register values are saved, parameters are passed and only then is the function executed. See here for details. Furthermore, functions are defined by the programmer.

Younger programming languages have introduced more abstraction, partially or totally removing the distinction. In C#, for instance, you can overload operators for your own types. In Scala, there are no operators but only syntactic sugar for binary methods. Once these languages are compiled, though, you can see which language elements are translated to flat instructions and which to subroutine calls. Keep in mind that optimising and/or just-in-time compilers may liberally move between worlds.

Best Answer from StackOverflow

Question Source : http://cs.stackexchange.com/questions/6067

0 comments:

Post a Comment

Let us know your responses and feedback