views:

401

answers:

12

IIRC, vb6 allowed function calls with no (). IIRC, it was allowed whenever the func didn't have a return value, and you could always use func(same, params, here).

What other languages allow this? What do you think func with no parentheses should mean? What are the rules for them?

Disclaimer: I am designing a language, so if you are and will be upset if I took the idea, then please don't write it.

A: 

A couple of examples of non-traditional or no parenthesis use:

Objective-C: [object functionNamePart1:arg1 functionNamePart2:arg2]
or: [object function]

F-Script (based on objective-C): object functionNamePart1:arg1 functionNamePart2:arg2
or: object function

Lisp: (+ 2 2)

cobbal
A: 

Haskell does not require parentheses too.

Artur Soler
+4  A: 

Pretty much all functional languages from the ML lineage (Miranda, Haskell, OCML, F#, SML, etc.) don't require parentheses for function calls.

For example, in Haskell:

Prelude> (\x->x) 1
1

The anonymous identity function is called with 1 and returns 1

Patrick
+1  A: 

Perl:

To call subroutines:

NAME(LIST);    # & is optional with parentheses.
NAME LIST;     # Parentheses optional if predeclared/imported.
&NAME(LIST);   # Circumvent prototypes.
&NAME;     # Makes current @_ visible to called subroutine.

http://perldoc.perl.org/perlsub.html

MaxVT
+1  A: 

Perl and Smalltalk are a couple of examples. (Note that both allow parantheses, but they do not require them in all situations.)

markusk
+1  A: 

F# doesn't need parameters - in particular when using the pipeline operator.

a(b(c(d(10)))) ;;

is equivalent to:

10 |> d |> c |> b |> a ;;

(a similar effect to extension methods, really)


In Ruby, the brackets are always optional (similar to VB) - but in Ruby they are even optional when declaring a method:

def echo( foo )
    return foo
end

is identical to:

def echo foo
    return foo
end
Marc Gravell
+2  A: 

Tcl

puts "Hello, world!"
Nick D
+1  A: 

Ruby is another language that allows, but does not require (in most cases), parentheses on method calls.

They are also optional when defining a method.

def say_hi your_name
  puts "Hello, #{your_name}!"
end

When a method is an argument for another method, Ruby 1.8.* gives a warning about requiring parentheses in a future version. Try to make sense of this, then try it in irb:

puts "guess what this does?".slice 6, 4
dmnd
+3  A: 

Ruby

def hello_world name
   puts "Hello world, #{name}."
end

hello_world 'Hal'

If you want you can but you're not forced to use parentheses, which is great in the cases where it increases readability and decreases ambiguity.

j t
You require parentheses sometimes, but only in cases where it would be otherwise impossible for the interpreter to figure out the correct ordering of arguments in a long chain of argument calls.
workmad3
A: 

JavaScript is an example for what a function "call" without parens can mean: reference. In languages that allow treating functions as objects, a function name is just another variable.

Also, in PHP language constructs (like echo or exit) don't need parens even if they take arguments, so "echo $foo" means the same as "echo($foo)" and "exit $foo" means the same as "exit($foo)".

Alan
+1  A: 

Forth

: STAR          [CHAR] * EMIT ;
: STARS         0 DO STAR LOOP CR ;
: SQUARE        DUP 0 DO DUP STARS LOOP DROP ;
: TRIANGLE      1 DO I STARS LOOP ;
: TOWER ( n -- )  DUP TRIANGLE SQUARE ;

Parenthesis are comments.

HVS
+2  A: 

In functional programming languages (ML, Haskell, F#, OCAML), any function just has one argument - Multi-parameter-functions are represented by passing tuples or Currying. Thus there is no need for a function to enclose the parameter in parentheses.

Ex #1:

inc x = x + 1
res = inc 42

Ex #2:

sum a b = a + b
res = sum 1 2

In #2, you pass 1 to sum and then call the resulting function with 2 (Currying)

Additionally, you can use mechanims like function composition, pipelines or the $-operator (point-free style).

Just compare the two examples.

a = f(g x)
b = f $ g x

print(square(parse "Hello"))
"Hello" |> parse |> square |> print

Another way of reducing parentheses in other languages is introducing extension methods (similar to pipelines) or the arrow-operator.

Compare:

ToString(Parse(42))
"42".Parse().ToString()

(*(*SomePointer).SomeMember).Invoke();
SomePointer->SomeMember->Invoke();

Ruby and Perl allow subroutines to be called without parentheses, VBC-style languages (Visual Basic, VBScript) do this too. Lisp/Scheme don't need parentheses for their function calls too (but there are many parentheses anyway^^)

Dario