operators similar to functions?

V

vlsidesign

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..
 
W

Walter Roberson

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.]
 
E

Eric Sosman

vlsidesign said:
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."
 
K

Keith Thompson

vlsidesign said:
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.
 
J

James Kuyper

vlsidesign said:
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 helps 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.
 
P

Peter Nilsson

James Kuyper said:
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.
 
J

James Kuyper

Peter said:
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.
 
S

santosh

vlsidesign said:
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.
 
C

Chris Dollin

vlsidesign said:
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..

(a) operators are typically [not just in C] just special syntax for
applying operations to values.

(b) they are useful for this because they are (1) concise and (2) similar
to the operators +-*/ we encounter early in our education.

(c) traditionally /in C/ operators correspond to small numbers of inline
machine instructions.

(d) in some other languages -- for example, C's <adjective> sibling C++ --
it's possible to attach user-defined functions to operators. Opinions
differ on whether this is a Good Thing.

(e) some operators are really special, in that they don't just operate on
the values of their operands -- they control the order of evaluation
of those operands, and may choose not to evaluate some operands at all
(which you can't, typically, do with a function). The C operators
||, &&, ?:, and , are like this.
 
P

Philip Potter

Eric said:
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."

ITYM:
(setq x (/ (- (sqrt (- (* b b) (* 4 a c))) b)(* 2 a)))

I don't know how to give the other root, I don't know enough LISP to
know what the negation operator is!
 
J

jacob navia

vlsidesign said:
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..

Yes, conceptually operators are exactly like functions. You could
rewrite all C with

int a,b,c,d;

a = div(add(b,c),sub(c,d));

instead of writing

c = (b+c)/(c-d);

It would be much more cumbersome to write but it would work.

Since basically there is no difference between operator usage
and a function call, there are heretics that propose to use the
simpler operator notation instead of the function call notation
to call functions.

Those heretics (that are looked upon with disdain by the law-abiding
regulars of this group) propose a sin called "operator overloading"
where you can use the operator notation to call your own functions,
imagine that.

You write this absolutely heretical notation:

result_type operator+(arg_type a,arg_type b);

Then, with suitable definitions of arg_type, the compiler will call
this function instead of doing a normal addition.

The you can use:

arg_type a,b,c,d;

a = (b+c)/(c-d);

instead of using the cumbersome

a = div(add(b,c),sub(c,d));


This is of course more practical than the current situation, since
it is easier to read and write. But "it is an heresy" and you should not
speak about it here, where only the law abiding regulars decide what
should be discussed.

Forget all the stuff above.

P.S. Of course, if you want a compiler that does this within the
framework of C, you look at the signature below and you can
download it for free.
 
P

Philip Potter

vlsidesign said:
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..

I agree with all of the other responses, but:

One thing which hasn't been mentioned so far is operators which change
the values of their operands. For example, it is impossible in C to
write a function:
int increment(int x);
which has the same effect as x++ (or ++x). This is because x is passed
by value to the function, so increment() cannot change the value of x in
the caller's scope. The "obvious" implementation:

int increment(int x) { return x++; }

doesn't work because:

#include <stdio.h>
int main(void) {
int i = 0;
increment(i);
printf("%d\n",i); /* i is still 0 */
return 0;
}

[OT: In C++ this problem is solved by introducing reference types which
allow a pass-by-reference mechanism. Then 'int increment (int &x)
{return x++;}' does exactly what you want. This is an example of how
introducing one feature to a lanugage (operator overloading) may require
the introduction of another feature (reference types).]
 
M

Mark McIntyre

jacob said:
Since basically there is no difference between operator usage
and a function call, there are heretics that propose to use the
simpler operator notation instead of the function call notation
to call functions.

Those heretics (that are looked upon with disdain by the law-abiding
regulars of this group)

Sanity check: Many of the regulars here programme in C++ too, and have
no problem with this approach - when programming in C++.

What they _do_ have is a problem with people trying to discuss it in a C
programming group, as if it were topical.
But "it is an heresy"

Only in /your/ mind.
and you should not speak about it here,

correct, since it's C++.
P.S. Of course, if you want a compiler that does this within the
framework of C, you look at the signature below and you can
download it for free.

Ps, advertising is not permitted in CLC, as you full well know, you
spamming bar steward.
 
J

James Kuyper

Philip said:
vlsidesign said:
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..

I agree with all of the other responses, but:

One thing which hasn't been mentioned so far is operators which change
the values of their operands. For example, it is impossible in C to
write a function:
int increment(int x);
which has the same effect as x++ (or ++x). This is because x is passed
by value to the function, so increment() cannot change the value of x in
the caller's scope. The "obvious" implementation:

int increment(int x) { return x++; }

doesn't work because:

#include <stdio.h>
int main(void) {
int i = 0;
increment(i);
printf("%d\n",i); /* i is still 0 */
return 0;
}

[OT: In C++ this problem is solved by introducing reference types which
allow a pass-by-reference mechanism. Then 'int increment (int &x)

C++'s reference types are merely syntactic sugar for something that can
be done in a slightly more cumbersome fashion in C:

int increment(int *px) { return (*px)++;}


{return x++;}' does exactly what you want. This is an example of how
introducing one feature to a lanugage (operator overloading) may require
the introduction of another feature (reference types).]
 
P

Philip Potter

James said:
Philip said:
vlsidesign said:
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..
I agree with all of the other responses, but:

One thing which hasn't been mentioned so far is operators which change
the values of their operands. For example, it is impossible in C to
write a function:
int increment(int x);
which has the same effect as x++ (or ++x). This is because x is passed
by value to the function, so increment() cannot change the value of x in
the caller's scope. The "obvious" implementation:

int increment(int x) { return x++; }

doesn't work because:

#include <stdio.h>
int main(void) {
int i = 0;
increment(i);
printf("%d\n",i); /* i is still 0 */
return 0;
}

[OT: In C++ this problem is solved by introducing reference types which
allow a pass-by-reference mechanism. Then 'int increment (int &x)

C++'s reference types are merely syntactic sugar for something that can
be done in a slightly more cumbersome fashion in C:

int increment(int *px) { return (*px)++;}

In the context of replacing operators with functions, it's not the same
thing at all. Firstly, it requires you to replace x++ with increment(&x)
rather than increment(x) - so at best you can replace an operator with a
function call and another operator. Secondly, even increment(&x) doesn't
work everywhere x++ does:

void f(void) {
int i;
register int j;
i++; /* fine */
j++; /* fine */
increment(&i); /* fine */
increment(&j); /* constraint violation */
}

Having said that, I'm not sure if you can initialize a reference type
with a register lvalue in C++. Whether or not this is the case, it
should another difference between operators and functions in C.
 
E

Eric Sosman

Philip said:
ITYM:
(setq x (/ (- (sqrt (- (* b b) (* 4 a c))) b)(* 2 a)))

I don't know how to give the other root, I don't know enough LISP to
know what the negation operator is!

When I wrote "unless I've botched something," I must
have been imbued with the prophetic spirit. ;-)

Lisp's negation construct is the same as subtraction,
but with just one argument: (- x). So

(setq x1 (/ (+ (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2 a)))
(setq x2 (/ (- (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2 a)))
(setq disclaimer "Unless I've botched something")

My point was that not all languages work hard to imitate
familiar "formula" notations, and that the O.P.'s realization
that operators and functions are similar is correct: the
differences are linguistic, not "essential."

There's a wide family of languages that imitate a formula
notation (C,FORTRAN, Algol, Pascal, ...), but examples of
"non-formulaic" languages are not hard to find. Lisp is one,
assemblers usually focus on the operations rather than on
formulas, what little I saw of COBOL looked like formulas
transliterated into pidgin English, and somewhere in the high
holy Himalayas of ratiocination you'll find the monasteries
wherein dwell the lamas who can actually read APL.

Let's not poke too much fun at Lisp, by the way. It's
challenging to read (although one gets better with practice),
but there are *no* rules of precedence or associativity (or
their formal-grammar counterparts) to confuse newbies, the
"operator overloading" diatribes never erupt, and indeed it's
trivial to introduce brand-new "operators" at need. That's
a lot of power; the obfuscation may be a worthwhile price.
 
C

CBFalconer

Mark said:
Sanity check: Many of the regulars here programme in C++ too,
and have no problem with this approach - when programming in C++.

What they _do_ have is a problem with people trying to discuss it
in a C programming group, as if it were topical.


Only in /your/ mind.


correct, since it's C++.

So far, fine.
Ps, advertising is not permitted in CLC, as you full well know,

and even here.
you spamming bar steward.

But this last piece is unnecessary and apes Jacobs own diatribes.
 
P

Philip Potter

Eric said:
When I wrote "unless I've botched something," I must
have been imbued with the prophetic spirit. ;-)

Or experience? :)
Lisp's negation construct is the same as subtraction,
but with just one argument: (- x). So

So (- x) doesn't follow the same pattern as (- x y) (- x y z) and so on
do? I guess it makes sense.
(setq x1 (/ (+ (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2 a)))
(setq x2 (/ (- (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2 a)))
(setq disclaimer "Unless I've botched something")

My point was that not all languages work hard to imitate
familiar "formula" notations, and that the O.P.'s realization
that operators and functions are similar is correct: the
differences are linguistic, not "essential."

Hear, hear. What I have been saying elsethread about operators and
functions being fundamentally different only applies to the C language.
There's a wide family of languages that imitate a formula
notation (C,FORTRAN, Algol, Pascal, ...), but examples of
"non-formulaic" languages are not hard to find.

I knew someone who had a scientific pocket-calculator which used reverse
polish notation. So the key combination to calculate the quadratic
formula would (modulo botching) be:

b - b ^2 4 a c * * - sqrt - 2 a * /

[here the first - is negation, while the rest are subtraction. ^2 is one
operator to do squaring.]

This doesn't even require the parenthesis that Lisp uses! (And is even
less readable.)

The only problem with Reverse Polish notation is that it doesn't allow
Lisp-style variadic functions, though it does allow C-style ones.
 
C

CBFalconer

Eric said:
.... snip ...

Let's not poke too much fun at Lisp, by the way. It's
challenging to read (although one gets better with practice),
but there are *no* rules of precedence or associativity (or
their formal-grammar counterparts) to confuse newbies, the
"operator overloading" diatribes never erupt, and indeed it's
trivial to introduce brand-new "operators" at need. That's
a lot of power; the obfuscation may be a worthwhile price.

Actually I was quite surprised some years ago, when I found that
people thoroughly conversant with Lisp could write amazingly clear
source code in it.
 
C

Coos Haak

Op Thu, 29 Nov 2007 14:38:05 +0000 schreef Philip Potter:
Eric said:
When I wrote "unless I've botched something," I must
have been imbued with the prophetic spirit. ;-)

Or experience? :)
Lisp's negation construct is the same as subtraction,
but with just one argument: (- x). So

So (- x) doesn't follow the same pattern as (- x y) (- x y z) and so on
do? I guess it makes sense.
(setq x1 (/ (+ (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2 a)))
(setq x2 (/ (- (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2 a)))
(setq disclaimer "Unless I've botched something")

My point was that not all languages work hard to imitate
familiar "formula" notations, and that the O.P.'s realization
that operators and functions are similar is correct: the
differences are linguistic, not "essential."

Hear, hear. What I have been saying elsethread about operators and
functions being fundamentally different only applies to the C language.
There's a wide family of languages that imitate a formula
notation (C,FORTRAN, Algol, Pascal, ...), but examples of
"non-formulaic" languages are not hard to find.

I knew someone who had a scientific pocket-calculator which used reverse
polish notation. So the key combination to calculate the quadratic
formula would (modulo botching) be:

b - b ^2 4 a c * * - sqrt - 2 a * /

[here the first - is negation, while the rest are subtraction. ^2 is one
operator to do squaring.]

As, like you wrote below, RPN languages don't usally have variadic
functions, the first - is written as NEGATE in Forth.
This doesn't even require the parenthesis that Lisp uses! (And is even
less readable.)

The only problem with Reverse Polish notation is that it doesn't allow
Lisp-style variadic functions, though it does allow C-style ones.

Agreed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top