4

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

3
  • 1
    I think this might be a candidate for communiwikification... Commented Oct 7, 2010 at 20:05
  • are you asking for an exhaustive list in one answer or just everyone give their favorite example?? Commented Oct 7, 2010 at 20:08
  • What I would love would be a single answer that names all languages that do allow this with syntax examples. In retrospect, yes this would be a great candidate for wikification. Commented Oct 7, 2010 at 20:14

14 Answers 14

11
  • almost all Lisps
  • Ruby
  • Python
  • C++
  • C#
  • Visual Basic.NET
  • Tcl
  • Visual Basic
  • Ioke
  • Seph
  • Cobra
  • Nemerle
  • Mirah
  • Delphi
  • Groovy
  • PHP
  • Fancy
  • Scala
Sign up to request clarification or add additional context in comments.

1 Comment

OCaml has default arguments as well (for named arguments).
8

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
}

2 Comments

This is exactly the kind of answer I'm looking for. The language and then an example in that langauge.
@Alaor Because this is not by any means an exhaustive list
8

Ones I can think of

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

Comments

3

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.

Comments

2

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

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

foo;
foo('baz');

Comments

2

Perl does with Method::Signatures.

Comments

2

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

Comments

2

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

Also C++, Ruby and VB

Comments

2

TCL provides this functionality

proc procName {{arg1 defaultValue} {arg2 anotherDefaultValue}} {
    # proc body
}

Comments

1

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
}

Comments

0

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;
}

4 Comments

The name for this is method overloading.
Sure, but since he didn't asked about method overloading, but about method default parameters values, method overloading is a workaround.
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.
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.
0

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.

3 Comments

"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.
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.
@NullUserException: if you don't have arbitrary lists of one kind of argument, why is it a horrible idea? It's a simple tradeoff of performance vs. flexibility/robustness/expandability. When coding libraries I frequently find myself regretting not having chosen this approach. @meador RB was never a VB "clone". It was a completely OO language from day one (before VB.NET) which tried to allow relatively easy porting of VB projects. RB has never had the ugliness of VB.
0

You can do it in PL/SQL.

Comments

-2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.