Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Perl

Idiom #225 Declare and use an optional argument

Declare an optional integer argument x to procedure f, printing out "Present" and its value if it is present, "Not present" otherwise

sub f {
    my $x = shift;
    if (defined $x) {
        print("Present $x\n");
    }
    else {
        print("Not Present\n");
    }
}
(defn f 
  ([] (println "Not present"))
  ([x] (println "Present" x)))

Optional argument with pattern matching

New implementation...
< >
tkoenig