Function prototype: Difference between revisions

Added Kotlin
(Added FreeBASIC)
(Added Kotlin)
Line 498:
l.length; // 2
</lang>
 
=={{header|Kotlin}}==
The order of declarations in Kotlin is unimportant and so forward declaration of 'top level' functions is neither needed nor supported.
 
The only place where function (or property) prototypes are needed is for abstract members of classes or interfaces whose implementation will be provided by overriding those members in a derived or implementing class or object.
 
Here's an example of this. Note that since Kotlin allows arguments to be passed either by name or position for all functions, there is no separate prototype for this situation. Moreover, since arguments may be passed by name, it is strongly recommended (but not obligatory) that the parameter names for overriding members should be the same as for the functions they override. The compiler will issue a warning if this recommendation is not followed.
<lang scala>// version 1.0.6
 
interface MyInterface {
fun foo() // no arguments, no return type
fun goo(i: Int, j: Int) // two arguments, no return type
fun voo(vararg v: Int) // variable number of arguments, no return type
fun ooo(o: Int = 1): Int // optional argument with default value and return type Int
fun roo(): Int // no arguments with return type Int
val poo : Int // read only property of type Int
}
 
abstract class MyAbstractClass {
protected constructor()
abstract fun afoo() // abstract member function, no arguments or return type
abstract var apoo: Int // abstract read/write member property of type Int
}
 
class Derived : MyAbstractClass(), MyInterface {
override fun afoo() {}
override var apoo: Int = 0
 
override fun foo() {}
override fun goo(i: Int, j: Int) {}
override fun voo(vararg v: Int) {}
override fun ooo(o: Int):Int = o // can't specify default argument again here but same as in interface
override fun roo(): Int = 2
override val poo : Int = 3
}
 
fun main(args: Array<String>) {
val d = Derived()
println(d.apoo)
println(d.ooo()) // default argument of 1 inferred
println(d.roo())
println(d.poo)
}</lang>
 
{{out}}
<pre>
0
1
2
3
</pre>
 
=={{header|Lua}}==
<lang Lua>function Func() -- Does not require arguments
9,490

edits