Parentheses in function calls

B

BartC

Ben Bacarisse said:
I don't think you've expressed this very well. The () "operator" of a
function call does not need to dereference anything (though it may well
do so). The point is that F and F() are different in C but without ()s
in a call you need a way to distinguish between a call and a reference
to the function (which becomes a pointer, just like most mentions of
arrays do). I.e.

x = F;

is ambiguous -- it could be a call or an assignment of a function
pointer.

OK. I'm used to a rather difference point of view, where functions and
function pointers are distinct.

So if a function name F is used in an expression, it will be called. If a
function pointer P is used in an expression, you just get the pointer value,
unless explicitly dereferenced, say using *P. Then you get a function, and
that will then be called.

This is different from C where both F() and P() result in a call, and both F
and P result in a function pointer. Dereferencing is not needed to elevate P
to the same status as F. Neither is 'referencing' (&F) needed to obtain
function addresses. That's quite a neat model.

But get rid of (), and that model no longer works, and you might end up
needing that dereferencing:

F Call F (because F is a function)
&F Address of F
P Address of some function
*P Call that function (after dereferencing, because *P is a function)
 
R

riccardo

It doesn't look "cluttered" to me. I actually sort of like it for the
clarity.

True.. you can easily distinguish between functions and variables
because of that.. which is something that can be bothering in other
languages
 
M

Marcin Grzegorczyk

Kenneth said:
On 8/31/2010 11:29 AM, BartC wrote:
[function call by juxtaposition]
How well does it work with Perl?

Reasonably well, as long as you don't nest function calls. It's worth
keeping in mind, though, that Perl does not have a formal syntax. It's
officially a DWIM language, which means the interpreter is supposed to
guess what the programmer meant.
Does Perl have any equivalent to C's comma operator?

Yes, although it's sometimes difficult to tell whether a particular
comma is the comma operator or a list argument separator.
How does Perl handle passing the results of a function to another
function? For example, given:

fopen "input", "rb";

can you instead call a function which returns the filename, and another
which returns the mode? ie: something like this in C:

f = fopen(WhichFile(foo,bar),WhichMode(baz,snarf));

In such case, you usually do need parentheses around the nested
functions' arguments:

f = fopen WhichFile(foo,bar),WhichMode(baz,snarf);

(though real Perl code would of course use $f, @f or %f, depending on
whether f is a scalar, array or associative array; likewise for function
arguments).

This all works mostly because Perl functions don't really have multiple
parameters - they have just one parameter of list type, so there's
really no difference between

xyzzy($a, $b); # two scalar arguments passed as a list

and

xyzzy(($a, $b)); # one list argument

or even

@list = ($a, $b);
xyzzy @list; # one array argument expanded into a list

(that is, unless xyzzy is prototyped such that the first argument is
evaluated in scalar context, or (in the last case) modifies elements of
the list it receives as its parameter... I think you can see now why I
agree with Seebs that Perl is not nicer to read than C).
 

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

No members online now.

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top