|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Operators seem similar to functions. They both do something to either
arguments or operands, but are different in their syntax. Operators seem to be like built-in C functions. It would seem that there is very important reasons for having operands, and not just functions. Maybe they are just really powerful and can be used in expressions? Or they are somehow executed quicker than a function call? I am a newbie, and curious.. thanks for all the insights to this newsgroup.. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
In article <04ceca48-1c77-41be-8d6a-65710b0560da@f3g2000hsg.googlegroups.com>,
vlsidesign <fordgwf@gmail.com> wrote: >Operators seem similar to functions. They both do something to either >arguments or operands, but are different in their syntax. Operators >seem to be like built-in C functions. It would seem that there is very >important reasons for having operands, and not just functions. Maybe >they are just really powerful and can be used in expressions? Or they >are somehow executed quicker than a function call? I am a newbie, and >curious.. thanks for all the insights to this newsgroup.. If you did not have an operator '+' then you would find it difficult to write a function that did addition (excluding a - (-b)). Some operators provide fundamental features that would be difficult or impossible to replace by a function. Other operators such as ++ and += are there for convenience. And yes, on *most* architectures, an operator is faster than a function call... sometimes *much* faster [though how much faster is circumstantial and depends on the architecture and the program flow.] -- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
vlsidesign wrote:
> Operators seem similar to functions. They both do something to either > arguments or operands, but are different in their syntax. Operators > seem to be like built-in C functions. It would seem that there is very > important reasons for having operands, and not just functions. Maybe > they are just really powerful and can be used in expressions? Or they > are somehow executed quicker than a function call? I am a newbie, and > curious.. thanks for all the insights to this newsgroup.. It's about convenience, and about readability. Not all languages have found these to be as important: (setq x (/ (- (sqrt (* 4 a c)) b) (* 2 a))) .... is (unless I've botched something) the way the grade-school "quadratic formula" appears in one fairly durable language that does not distinguish "functions" from "operators." -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
vlsidesign <fordgwf@gmail.com> writes:
> Operators seem similar to functions. They both do something to either > arguments or operands, but are different in their syntax. Operators > seem to be like built-in C functions. It would seem that there is very > important reasons for having operands, and not just functions. Maybe > they are just really powerful and can be used in expressions? Or they > are somehow executed quicker than a function call? I am a newbie, and > curious.. thanks for all the insights to this newsgroup.. You're right, operators (most of them) are conceptually similar to functions, and in some languages (but not in C), operators really are treated as functions. An operator takes operands and yields a result; a function takes arguments and returns a result. One difference is syntax; operators such as "+" and "*" mimic common mathematical notation. Addition *could* have been defined using functional syntax, so you'd have to write ``add(x, y)'' rather than ``x + y''; the latter is just more convenient. Would you rather write a + b + c + d or add(a, add(b, add(c, d))) ? Another difference is that operators are built into the langauge, which means that the compiler has to know exactly how they're implemented -- and can take advantage of that knowledge. Normally ``x + y'' will be implemented in the generated code by something like an ADD instruction, not by a subroutine call. (But a compiler can generate inline code for explicit function calls, and it can generate a function call for an operator that isn't directly implemented as a CPU instruction.) Also, the compiler is allowed a bit more freedom to rearrange expressions involving operators than function calls, which tends to make for more efficient generated code. Finally, some operators can do things that couldn't be expressed in a function definition. For example, a function that takes two arguments always evaluates both of them; you can't write the equivalent of "&&" or "||" as a function (at least not in C). The "+" operator can operate on any numeric type; to do the equivalent with functions, you'd need a separate function for each type. "sizeof" is actually an operator (despite being spelled as a keyword rather than as a punctuation symbol); there's no way to write a function that does the same thing. -- Keith Thompson (The_Other_Keith) <kst-u@mib.org> Looking for software development work in the San Diego area. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
vlsidesign wrote:
> Operators seem similar to functions. They both do something to either > arguments or operands, but are different in their syntax. Operators > seem to be like built-in C functions. It would seem that there is very > important reasons for having operands, and not just functions. Maybe > they are just really powerful and can be used in expressions? Or they > are somehow executed quicker than a function call? I am a newbie, and > curious.. thanks for all the insights to this newsgroup.. The distinction between operators and function calls is entirely a matter of syntax. Any operator could have been implemented by a function, and any standard library function could have been defined as an operator. It doesn't really affect the efficiency - a sufficiently good compiler would generate the same actual machine code, whether C was defined as supporting a+b or add(a,b). The key difference is that operators take at most two or three characters to type, making them easier to type and harder to understand. Functions, on the other hand, have readable names which are typically a bit longer than operators, which s you remember what they do, at the expense of more typing. Operators are used for the most basic and frequently used operations, so that the shorter size saves you a lot of space, and the frequent use makes them easy to memorize. Functions are used for the more complicated and less frequently used operations, where the extra size of the function name costs less, and the infrequent use makes the advantage of a readable name more important. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
James Kuyper <jameskuy...@verizon.net> wrote:
> vlsidesign wrote: > > Operators seem similar to functions. ... > > The distinction between operators and function calls is > entirely a matter of syntax. Any operator could have been > implemented by a function, ... True, but it's not a simple mapping. The following operators would require special kinds of functions: ||, &&, ?: A void cast of an integer expression evaluating to zero could not precisely emulate a null pointer constant since it would no longer be a constant expression. -- Peter |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Peter Nilsson wrote:
> James Kuyper <jameskuy...@verizon.net> wrote: >> vlsidesign wrote: >>> Operators seem similar to functions. ... >> The distinction between operators and function calls is >> entirely a matter of syntax. Any operator could have been >> implemented by a function, ... > > True, but it's not a simple mapping. > > The following operators would require special kinds of > functions: ||, &&, ?: > > A void cast of an integer expression evaluating to zero > could not precisely emulate a null pointer constant > since it would no longer be a constant expression. We're talking fundamental re-design of the language here; and(a,b) could have been defined as having the same special characteristics as a&&b has in the actual C language. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
vlsidesign wrote:
> Operators seem similar to functions. They both do something to either > arguments or operands, but are different in their syntax. Operators > seem to be like built-in C functions. It would seem that there is very > important reasons for having operands, and not just functions. Maybe > they are just really powerful and can be used in expressions? Or they > are somehow executed quicker than a function call? I am a newbie, and > curious.. thanks for all the insights to this newsgroup.. Also one more thing. A function allows you to encapsulate and reuse pieces of code and thus encourages structured programming; a language with operators alone would find this difficult to do, early dialects of BASIC without subroutine support come to mind. |
|
![]() |
| Outils de la discussion | |
|
|