Function parameters

Function parameters (also known as arguments) are data of a predetermined type that can be supplied to a function to customise its behaviour. A value must be provided for every parameter when the function is called; however, most programming languages also support null parameter values.

The below code demonstrates how to write a Kotlin function that accepts a String parameter called name and an Integer parameter called age. The function uses the parameter values to return a String in the format "{name} is {age} years old."

fun outputNameAndAge(name: String, age: Int): String {
	return "$name is $age years old."
}

The below code shows you how to create the same function as above but using Java code instead of Kotlin.

public static String outputNameAndAge(String name, Integer age) {
	return name + " is " + age + " years old.";
}

Vararg modifier

The vararg modifier allows you to provide an unspecified number of parameters of the same type. The function body can then iterate through the varargs parameter and perform any required actions on each element. Each function may only possess one vararg parameter and the vararg parameter must be the last parameter in the function signature.

The below code demonstrates how to create and run a method that uses a vararg String parameter in Kotlin.

fun outputNameAndAge(age: Int, vararg names: String) {
	println("The following people are $age years old:")
	names.forEach { println(it) }
}

fun main(args: Array<String>) {
	/*
	Prints: The following people are 20 years old:
	Adam
	*/
	outputNameAndAge(20, "Adam")
	
	/*
	Prints: The following people are 30 years old:
	John
	Elizabeth
	Sophie
	*/
	outputNameAndAge(30, "John", "Elizabeth", "Sophie")
	
	/*
	Prints: The following people are 200 years old:
	*/
	outputNameAndAge(200)
}

The below code shows you how to create the same function as above but using Java code instead of Kotlin.

public void String outputNameAndAge(Integer age, String... names) {
	System.out.println("The following people are " + age + " years old:");
	for (String name : names) {
        System.out.println(name);
    }
}

public static void main(String[ ] args) {
    /*
	Prints: The following people are 20 years old:
	Adam
	*/
	outputNameAndAge(20, "Adam");
	
	/*
	Prints: The following people are 30 years old:
	John
	Elizabeth
	Sophie
	*/
	outputNameAndAge(30, "John", "Elizabeth", "Sophie");
	
	/*
	Prints: The following people are 200 years old:
	*/
	outputNameAndAge(200);
}

Named parameters

When calling a function that features parameters, we can identify each parameter by name. In doing so, we can supply parameter values in a different order in which they appear in the function signature. Also, it helps distinguish different parameters of the same type. You can name some parameters and leave other parameters unnamed; however, as soon as you name one parameter, all subsequent parameter values in the function call must also be named.

An example of named parameters in action is provided in the below Kotlin code:

fun joinFirstMiddleLastNames(firstName: String, middleName: String, lastName: String): String {
	return "$firstName $middleName $lastName"
}

fun main(args: Array<String>) {
	// Prints: John Arthur Smith
	println(joinFirstMiddleLastNames("John", lastName = "Smith", middleName = "Arthur"))
}

Default parameter values

If a function features parameters, it is possible to assign default values to those parameters. The default value will be used unless an alternative value is supplied when the method is called.

For example, in the below Kotlin code, the joinFirstLastNames function will always assign the lastName parameter a value of Smith, unless directed otherwise:

fun joinFirstLastNames(firstName: String, lastName: String = "Smith"): String {
	return "$firstName $lastName"
}

fun main(args: Array<String>) {
	// Prints: John Smith
	println(joinFirstLastNames("John"))
	
	// Prints: John Arthur
	println(joinFirstLastNames("John", "Arthur"))
}