Next: , Previous: Tuples, Up: Extensions to C


2.1.2.3 Keyword and default arguments

L functions can have default arguments UNIMPLEMENTED, like C++ ones:

     Int foo(Int bar = 3)
     {
       bar
     }
     
     foo(5); //5
     foo();  //3

L functions argument passing may also be done using keyword arguments UNIMPLEMENTED:

     Int foo(Int bar, Int baz)
     {
       10 * bar + baz
     }

If you write:

     Int foo(Int bar = 1, Int baz)
     {
       10 * bar + baz
     }

Then foo can only be called like this:

     foo(3, 4)  //OK, 34
     foo(baz:5) //OK, 15
     foo(5)     //Wrong

Use of keyword arguments is a really good style when you create functions that create complex objects (structures and records), especially when they contain fields that have the same type.

For instance:

     let richards_shoes = Shoes(color:Green, sole_color:Brown)

Is much better style than

     let richards_shoes = Shoes(Green, Brown)

In general, it is better to use them when a function has several arguments of the same type, or several arguments at all. It makes your code more readable.

You can also use them for “cosmetic” usage, as in:

     foreach(a, in:list) {...}
     foreach(element:a, in:list) {...}
     divide(25, by:73);