views:

218

answers:

14

I'm curious which languages allow you to do something like this:

method foo(String bar = "beh"){

}

If you call foo like this:

foo();

bar will be set to "beh", but if you call like this:

foo("baz");

bar will be set to "baz".

A: 

I know Python allows this, while C,C++ don't.

highBandWidth
C++ does allow it.
Jon Purdy
C++ does support this.
Hugo Peixoto
+3  A: 

as of c# 4.0 you can now have default params. Finally!

Also C++, Ruby and VB

Climber104
+8  A: 

Ones I can think of

  • C# 4.0
  • C++
  • VB.Net (all versions)
  • VB6
  • F# (members only)
  • Powershell
  • IDL
  • Ruby
JaredPar
+6  A: 

PHP:

function foo($var = "foo") {
    print $var;
}

foo(); // outputs "foo"
foo("bar"); // outputs "bar"

Python:

def myFun(var = "foo"):
    print var

Ruby:

def foo(var="foo")
    print var
end

Groovy:

def foo(var="foo") {
    print var
}
NullUserException
This is exactly the kind of answer I'm looking for. The language and then an example in that langauge.
cmcculloh
Why C# is not there?
Alaor
@Alaor Because this is not by any means an exhaustive list
NullUserException
+2  A: 

Delphi has allowed this since about version 5 - released in 1999

procedure foo(const bar: string = 'beh');
begin
...
end;

foo;
foo('baz');
Gerry
+2  A: 

Perl does with Method::Signatures.

CanSpice
A: 

Java has a workaround.

You can have the foo method with no parameters that calls the foo method with parameters setting the default value, like this:

void foo() {
   foo("beh");
}

void foo(String bar) {
   this.bar = bar;
}
pablosaraiva
The name for this is method overloading.
JaredPar
Sure, but since he didn't asked about method overloading, but about method default parameters values, method overloading is a workaround.
pablosaraiva
And it's super-fun writing eight overloaded methods to reproduce three defaultable arguments, eh. Especially when Policy demands each needs to have its own lengthy XML javadoc. aaargh.
bobince
I'm not defending Java here. I'm just posting the workaround. And it's not necessary to write 8 methods for 3 default arguments, only 4.
pablosaraiva
+1  A: 

Python:

def foo(bar = value):
    # This function can be invoked as foo() or foo(something).
    # In the former case, bar will have its default value.
    pass
Santiago Lezica
A: 

Add to the list Realbasic (indeed Realbasic has almost every nice feature of every language I can think of, including introspection and sandboxed scripting).

Python. (But watch out for mutables per http://effbot.org/zone/default-values.htm )

And plenty of languages, including C, allow variable numbers of parameters which effectively lets you do the same thing.

In many modern scripting languages, including PHP, JavaScript, and Perl, a better idiom for handling such things is to allow an associative array or object as a parameter, and then assign defaults if needed.

e.g.

function foo( options ){
  if( options.something === undefined ){
    options.something = some_default_value;
  }
  ...
}

This eliminates the necessity of putting the defaultable values at the end of the list of parameters and remembering all the stuff you don't want to override.

As always -- use in moderation.

podperson
"allow an associative array or object as a parameter" Unless you have a large list of arguments with no consistent usage frequency, this is a horrible idea.
NullUserException
IIRC, REALbasic started out life as a Visual Basic clone for the Mac. Given this pedigree, I'll respectfully disagree with your assertion that it "has almost every nice feature of every language". I won't downvote you, though.
rmeador
+2  A: 

TCL provides this functionality

proc procName {{arg1 defaultValue} {arg2 anotherDefaultValue}} {
    # proc body
}
TokenMacGuy
+7  A: 
  • almost all Lisps
  • Ruby
  • Python
  • C++
  • C#
  • Visual Basic.NET
  • Tcl
  • Visual Basic
  • Ioke
  • Seph
  • Cobra
  • Nemerle
  • Mirah
  • Delphi
  • Groovy
  • PHP
  • Fancy
  • Scala
Jörg W Mittag
OCaml has default arguments as well (for named arguments).
sepp2k
+1  A: 

D:

void foo(int x, int y = 3)
{
   ...
}
...
foo(4);   // same as foo(4, 3);

Fantom:

class Person
{
  Int yearsToRetirement(Int retire := 65) { return retire - age }

  Int age
}
Corbin March
I misread "D:" as `D:` (horrified face).
Jon Purdy
A: 

You can do it in PL/SQL.

Nils Weinander
+3  A: 

Racket provides this, as well as keyword arguments:

(define (f x [y 0]) (+ x y))
(f 1) ; => 1
(f 10 20) ; => 30

(define (g x #:y [y 0]) (- x y))
(g 1) ; => 1
(g 10 #:y 20) ; => -10

They're described in the documentation.

Sam TH