PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > operators similar to functions?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
operators similar to functions?

Réponse
 
LinkBack Outils de la discussion
Vieux 29/11/2007, 02h35   #1
vlsidesign
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut operators similar to functions?

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..
  Réponse avec citation
Vieux 29/11/2007, 02h44   #2
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operators similar to functions?

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
  Réponse avec citation
Vieux 29/11/2007, 02h50   #3
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operators similar to functions?

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
  Réponse avec citation
Vieux 29/11/2007, 03h51   #4
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operators similar to functions?

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"
  Réponse avec citation
Vieux 29/11/2007, 04h01   #5
James Kuyper
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operators similar to functions?

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.
  Réponse avec citation
Vieux 29/11/2007, 05h25   #6
Peter Nilsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operators similar to functions?

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
  Réponse avec citation
Vieux 29/11/2007, 05h37   #7
James Kuyper
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operators similar to functions?

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.
  Réponse avec citation
Vieux 29/11/2007, 07h29   #8
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operators similar to functions?

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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h35.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,29145 seconds with 16 queries