Programmer dictionary: Parameter vs Argument, Type parameter vs Type argument
Parameter and argument are often confused, but they are totally different concepts. Let’s discuss what they are and what are the differences. We will also understand what is type parameter and type argument.
Parameter vs Argument
Parameter is variable defined in function definition, while argument is actual value passed to the function. To understand the difference, let’s first see an example function and its usage:
fun randomString(length: Int): String {
// ....
}randomString(10)
In this example length
is a parameter, and 10
(used in function call) is an argument. Here are common definitions:
Parameter is variable defined in function declaration. Argument is the actual value of this variable that get passed to the function.
Conversation example:
randomString
function is throwing an error. What argument are you passing tolength
parameter?- It is
-1
- And this is the reason.
randomString
is not prepared for negative values oflength
parameter!
Type parameter vs Type argument
Distinction between parameter and argument is universal, and it is applicable for all types of functions: methods, constructors etc. It is also applicable for wording used in generic. Let’s discus example generic class:
class Box<T>val a: Box<Int> = Box()
Here Box
is generic class, which defines T
type parameter. In use, we specifies Int
as a type argument (see here why type, not class). Common definitions are following:
Type parameter is blueprint or placeholder for a type declared in generic. Type argument is actual type used to parametrize generic.
I would like to thank Dmitry Jemerov for proofing.
This post is first part of the Kotlin programmer dictionary. To stay up-to-date with new parts, just follow this medium or observe me on Twitter. If you need some help then remember that I am open for consultations.
If you like it, remember to clap. Note that if you hold the clap button, you can leave more claps.
Here are other published parts of the Kotlin programmer dictionary:
- Statement vs Expression
- Function vs Method vs Procedure
- Field vs Property
- Class vs Type vs Object
- Object expression vs Object declaration
- Receiver
- Implicit receiver vs Explicit receiver
- Extension receiver vs Dispatch receiver
- Receiver type vs receiver object
- Function Type vs Function literal vs Lambda expression vs Anonymous function
- Higher-order function
- Function literal with receiver vs Function type with receiver
- Invariance vs Covariance vs Contravariance
- Event listener vs Event handler
- Delegation vs Composition